简体   繁体   English

如何沿贝塞尔曲线返回所有点?

[英]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.我希望从这些点计算出的曲线的每个点都以点数组的形式返回,但我没有运气弄清楚,并且无法在 stackoverflow 或谷歌中找到类似的解决方案一般的。

Thanks.谢谢。

What you want to do is to convert a Bezier Curve (Cubic from the looks of it) into a Polyline 您要做的是将贝塞尔曲线(从其外观看,立方)转换为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. 使用这个公式在网页 ...价值t应介于0 to 1 ...计算的所有值Bx(t)By(t)通过使用对于t值的公式中的增量"0, 0.01, 0.02....1" (当然,将它们转换为integers )增量越小,点将越精确。

Here's a C Sample of the DeCasteljau Algorithm (almost the same procedure, but its a bit optimized i believe) :) 这是DeCasteljau算法的C示例(几乎相同的过程,但是我认为它有些优化):)

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

It may help if you use a lerp or float t derivatives in-between the draw bezier. 如果在绘制贝塞尔曲线之间使用lerp或float t导数可能会有所帮助。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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