如何在C#中找到开始,结束和2个交叉点的BezierSegment的控制点 - AKA Cubic Bezier 4点插值

sof*_*eer 16 c# wpf bezier

我一直在努力寻找一种可以理解的方法来做到这一点.我有四个点,一个StartPt,EndPoint和Intersection点来表示贝塞尔曲线中的峰值和谷值.

C#中的BezierSegment需要start,controlPoint 1,controlPoint 2,endpoint - 但是我没有任何控制点我只有这两个点位于贝塞尔曲线上(我称之为上面的交点)...我可以计算出两个控制点吗?

在此先感谢,这一直让我发疯.

这里有一些解释:http://www.tinaja.com/glib/nubz4pts1.pdf,但它是用postscript写的,而且这种语言对我来说毫无意义 - 这是我的头脑.

Gab*_*abe 16

通过4个点的曲线有无数个解,但最好的简单解决方案是尝试使曲线段长度与弦长成比例.您链接的代码是一阶近似,效果很好并且非常快.

这是PostScript代码的C#转换:

static class DrawingUtility
{
    // linear equation solver utility for ai + bj = c and di + ej = f
    static void solvexy(double a, double b, double c, double d, double e, double f, out double i, out double j)
    {
        j = (c - a / d * f) / (b - a * e / d);
        i = (c - (b * j)) / a;
    }

    // basis functions
    static double b0(double t) { return Math.Pow(1 - t, 3); }
    static double b1(double t) { return t * (1 - t) * (1 - t) * 3; }
    static double b2(double t) { return (1 - t) * t * t * 3; }
    static double b3(double t) { return Math.Pow(t, 3); }

    static void bez4pts1(double x0, double y0, double x4, double y4, double x5, double y5, double x3, double y3, out double x1, out double y1, out double x2, out double y2)
    {
        // find chord lengths
        double c1 = Math.Sqrt((x4 - x0) * (x4 - x0) + (y4 - y0) * (y4 - y0));
        double c2 = Math.Sqrt((x5 - x4) * (x5 - x4) + (y5 - y4) * (y5 - y4));
        double c3 = Math.Sqrt((x3 - x5) * (x3 - x5) + (y3 - y5) * (y3 - y5));
        // guess "best" t
        double t1 = c1 / (c1 + c2 + c3);
        double t2 = (c1 + c2) / (c1 + c2 + c3);
        // transform x1 and x2
        solvexy(b1(t1), b2(t1), x4 - (x0 * b0(t1)) - (x3 * b3(t1)), b1(t2), b2(t2), x5 - (x0 * b0(t2)) - (x3 * b3(t2)), out x1, out x2);
        // transform y1 and y2
        solvexy(b1(t1), b2(t1), y4 - (y0 * b0(t1)) - (y3 * b3(t1)), b1(t2), b2(t2), y5 - (y0 * b0(t2)) - (y3 * b3(t2)), out y1, out y2);
    }

    static public PathFigure BezierFromIntersection(Point startPt, Point int1, Point int2, Point endPt)
    {
        double x1, y1, x2, y2;
        bez4pts1(startPt.X, startPt.Y, int1.X, int1.Y, int2.X, int2.Y, endPt.X, endPt.Y, out x1, out y1, out x2, out y2);
        PathFigure p = new PathFigure { StartPoint = startPt };
        p.Segments.Add(new BezierSegment { Point1 = new Point(x1, y1), Point2 = new Point(x2, y2), Point3 = endPt } );
        return p;
    }
}
Run Code Online (Sandbox Code Playgroud)

我没有测试它,但它编译.只需DrawingUtility.BezierFromIntersection使用您拥有的4个点进行调用,它将返回PathFigure绘制曲线的点.