如何在C#中找到BezierSegment的控制点,给定起点、终点和2个交点 - 也称为三次Bezier 4点插值。

17

我一直在寻找一种易于理解的方法来做这件事。 我有四个点,一个起始点,一个终点和两个交点来表示贝塞尔曲线中的峰值和谷底。

C#中的BezierSegment需要起点、控制点1、控制点2和终点,然而我没有任何控制点,我只有沿着贝塞尔曲线的这两个点(上面称为交点)......如何计算这两个控制点?

提前感谢,这让我疯狂了。

这里有一些解释:http://www.tinaja.com/glib/nubz4pts1.pdf但它是用Postscript编写的,那种语言对我来说毫无意义-超出我的能力范围。


你能解释一下“交点”是什么意思吗?贝塞尔曲线通常不会与任何东西相交。 - John Feminella
1
这些点位于Bezier曲线上,一个点可能是从起始点到终点的1/3处, 另一个点可能是从终点到起始点的1/3处… 这样说清楚了吗? - softwarequestioneer
我相信你所说的交点是控制点。你只需要沿着曲线选取4个点来定义贝塞尔曲线。只要这4个点都在曲线上,无论它们在曲线上的位置如何,你都应该没问题。 - Pace
2
这些交点不是控制点,如果我把它们作为控制点包括进去,贝塞尔曲线就不能经过这些点。控制点位于贝塞尔曲线之外。为了更好地解释这个问题,请在脑海中绘制一条曲线,注意起点和终点,并从每个端点的三分之一处选择两个点。现在你有4个点 - 根据这些坐标,如果你想近似一个贝塞尔片段,你需要计算出两个位于曲线之外的控制点... - softwarequestioneer
更多解释请看这张图片:http://www.freeimagehosting.net/uploads/293dee372e.png我已经有了起点、终点、交点1和交点2的值,有没有办法确定控制点1和控制点2 - 因为这是BezierSegment所需要的... - softwarequestioneer
这是我的意思,立方贝塞尔4点插值...他们在这个链接中讨论如何在Flash中实现:http://algorithmist.wordpress.com/2008/09/08/cubic-bezier-4-point-interpolation-part-ii/“回顾一下,问题如下。给定四个向量V0、V1、V2和V3,找到三次贝塞尔曲线B(t)的控制点P0、P1、P2和P3,使得曲线通过V0、V1、V2和V3。”你们有没有想过如何用C#来实现这个? - softwarequestioneer
4个回答

18
通过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;
    }
}

我没有测试过它,但它可以编译。只需使用你拥有的4个点调用DrawingUtility.BezierFromIntersection,它将返回一个PathFigure以绘制曲线。


哇!Gabe,非常感谢你。我不知道你是如何阅读那个PostScript代码(看起来像汇编语言MIPS),但这太完美了。我刚试了一下,效果很好。嗯...现在我得解决同样的问题,但是要找到一个二次贝塞尔曲线中的单个控制点,给定起点、中点和终点。你不会偶然知道如何插值二次贝塞尔曲线吧? - softwarequestioneer
如果你想知道如何做二次方程,可以另开一篇帖子并在这里链接。 - Gabe
谢谢Gabe!这是我关于二次曲线的问题的链接:https://dev59.com/cUzSa4cB1Zd3GeqPkCpe - softwarequestioneer
+1 运作得非常好。我正在寻找一个干净的代码,用于计算Bezier曲线的两个控制点。将其翻译为Qt相当容易。 - gibertoni

1

1

您应该考虑使用基数样条(Canonical Splines),它使用一组存在于路径上的点,加上一个“张力”参数来控制角落平滑到角落切线的程度。

在Windows Forms中,可以使用DrawCurveDrawClosedCurve方法。在WPF中没有直接的等价物。以下是两篇文章,介绍了如何在C#中使用基数样条在WPF中实现。

Floris - AddCurve For WPF Cardinal Spline

Petzold - Canonical Splines In WPF And Silverlight


0

AS3 版本:

package 
{
    import flash.geom.Vector3D;

    public class DrawingUtility 
    {
        private var x1:Number; 
        private var y1:Number; 
        private var x2:Number;
        private var y2:Number;

        // linear equation solver utility for ai + bj = c and di + ej = f
        private function solvexy(a:Number, b:Number, c:Number, d:Number, e:Number, f:Number):Vector.<Number>
        {
            var returnVal:Vector.<Number> = new Vector.<Number>();
            var j:Number = (c - a / d * f) / (b - a * e / d);
            var i:Number = (c - (b * j)) / a;
            returnVal[0] = i;
            returnVal[1] = j;
            return returnVal;
        }

        // basis functions
        private function b0(t:Number):Number { 
            return Math.pow(1 - t, 3);
        }
        private function b1(t:Number):Number {
            return t * (1 - t) * (1 - t) * 3;
        }
        private function b2(t:Number):Number {
            return (1 - t) * t * t * 3;
        }
        private function b3(t:Number):Number {
            return Math.pow(t, 3);
        }

        private function bez4pts1(x0:Number, y0:Number, x4:Number, y4:Number, x5:Number, y5:Number, x3:Number, y3:Number):void
        {
            // find chord lengths
            var c1:Number = Math.sqrt((x4 - x0) * (x4 - x0) + (y4 - y0) * (y4 - y0));
            var c2:Number = Math.sqrt((x5 - x4) * (x5 - x4) + (y5 - y4) * (y5 - y4));
            var c3:Number = Math.sqrt((x3 - x5) * (x3 - x5) + (y3 - y5) * (y3 - y5));
            // guess "best" t
            var t1:Number = c1 / (c1 + c2 + c3);
            var t2:Number = (c1 + c2) / (c1 + c2 + c3);
            // transform x1 and x2
            var x1x2:Vector.<Number> = solvexy(b1(t1), b2(t1), x4 - (x0 * b0(t1)) - (x3 * b3(t1)), b1(t2), b2(t2), x5 - (x0 * b0(t2)) - (x3 * b3(t2)));
            x1 = x1x2[0];
            x2 = x1x2[1];
            // transform y1 and y2
            var y1y2:Vector.<Number> = solvexy(b1(t1), b2(t1), y4 - (y0 * b0(t1)) - (y3 * b3(t1)), b1(t2), b2(t2), y5 - (y0 * b0(t2)) - (y3 * b3(t2)));
            y1 = y1y2[0];
            y2 = y1y2[1];
        }

        public function BezierFromIntersection(startPt:Vector3D, int1:Vector3D, int2:Vector3D, endPt:Vector3D):Vector.<Vector3D>
        {
            var returnVec:Vector.<Vector3D> = new Vector.<Vector3D>();
            bez4pts1(startPt.x, startPt.y, int1.x, int1.y, int2.x, int2.y, endPt.x, endPt.y);

            returnVec.push(startPt);
            returnVec.push(new Vector3D(x1, y1));
            returnVec.push(new Vector3D(x2, y2));
            returnVec.push(endPt);
            return returnVec;
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接