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

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

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

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

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

通过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

这是两个很好的例子:

http://www.codeproject.com/KB/graphics/ClosedBezierSpline.aspx http://www.codeproject.com/KB/graphics/BezierSpline.aspx

另请参阅此动画以更好地了解BezierSplines如何工作http://en.wikipedia.org/wiki/B%C3%A9zier_curve

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

在Windows窗体中,可以使用DrawCurveDrawClosedCurve方法。 在WPF中没有直接的等价物。 这篇文章描述了使用C#在WPF中使用基数样条线。

Floris – 用于WPF Cardinal Spline的AddCurve

Petzold – WPF和Silverlight中的规范样条

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. { var returnVal:Vector. = new Vector.(); 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. = 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. = 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. { var returnVec:Vector. = new Vector.(); 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; } } }