简体   繁体   中英

How to return all points along a bezier curve?

I posted a previous question about generating a bezier curve based on only the start and end points, and I was able thanks to the answers in that create a bezier curve using the information I have.

This is the code that allows me to draw the types of curve that I want on a form.

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    Random rnd = new Random();
    Point startp = new Point(rnd.Next(0, this.Width), rnd.Next(0, this.Height));
    Point endp = new Point(rnd.Next(0, this.Width), rnd.Next(0, this.Height));
    int xMod = 0;
    int yMod = 0;
    if (startp.X > endp.X) {
        xMod = -1;
    } else {
        xMod = 1;
    }
    if (startp.Y > endp.Y) {
        yMod = 1;
    } else {
        yMod = -1;
    }
    Point control1p = new Point(endp.X + (rnd.Next(20, 50) * xMod), endp.Y + (rnd.Next(20, 50) * yMod));
    Point control2p = new Point(endp.X + (rnd.Next(5, 20) * xMod), endp.Y + (rnd.Next(5, 20) * yMod));
    Point[] pts = {
        startp,
        control1p,
        control2p,
        endp
    };
    Pen dashed_pen = new Pen(Color.Black, 0);
    dashed_pen.DashStyle = Drawing2D.DashStyle.Dash;
    for (int i = 0; i <= 2; i++) {
        e.Graphics.DrawLine(dashed_pen, pts(i), pts(i + 1));
    }
    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality;
    Pen bez_pen = new Pen(Color.Black, 3);
    e.Graphics.DrawBezier(bez_pen, pts(0), pts(1), pts(2), pts(3))
}

Is there a way, or can someone help me with returning all the points that form the curve? I'd like for each point of a curve calculated from those points to be returned in an array of points, but I'm having no luck figuring it out, and haven't been able to find a similar solution on stackoverflow or google in general.

Thanks.

What you want to do is to convert a Bezier Curve (Cubic from the looks of it) into a Polyline

Use the Equation on this page ...Value of t should be between 0 to 1 ...Calculate all values of Bx(t) and By(t) by using the equation for values of t in increments of "0, 0.01, 0.02....1" (Convert them to integers of course) The smaller your increments, the more accurate your points will be.

Here's a C Sample of the DeCasteljau Algorithm (almost the same procedure, but its a bit optimized i believe) :)

Maxim Shemanarev在“反谷物几何”页面: Bezier曲线的自适应细分中描述了用于创建具有最佳点数的平滑Bezier曲线的完美算法。

It may help if you use a lerp or float t derivatives in-between the draw bezier. I've found it helps with accuracy; considering the number of float calcs .

I know this is an old post, but, having found none of the current answers all that satisfying, hopefully others will get some use out of the following:

using System.Collections.Generic;
using System.Drawing;
    
public List<Point> CubicBezierToPoints(Point P0, Point P1, Point P2, Point P3, double step = 0.01)
{
    var pointList = new List<Point>();
    for (var t = 0.00; t <= 1; t += step)
    {
        var x = Math.Pow(1 - t, 3) * P0.X + 3 * Math.Pow(1 - t, 2) * t * P1.X +
                3 * (1 - t) * Math.Pow(t, 2) * P2.X + Math.Pow(t, 3) * P3.X;
        var y = Math.Pow(1 - t, 3) * P0.Y + 3 * Math.Pow(1 - t, 2) * t * P1.Y +
                3 * (1 - t) * Math.Pow(t, 2) * P2.Y + Math.Pow(t, 3) * P3.Y;
        pointList.Add(new Point((int)x,(int)y));
    }
    return pointList;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM