问题描述
我一直在努力寻找一种可以理解的方式做到这一点。我有四点,一StartPt,端点和交点重present在贝塞尔的波峰和波谷。
I've been struggling looking for an understandable way to do this. I have four points, a StartPt, EndPoint, and Intersection points to represent the peak and valley in the bezier.
该BezierSegment在C#中,需要启动,控制点1,控制点2,终点 - 但是,我没有任何控制点我只有沿贝塞尔曲线(我称他们为上述交点)位于这两个点。 ..我怎么能计算两个控制点?
The BezierSegment in C# requires start, controlPoint 1, controlPoint 2, endpoint - however I don't have any control points I only have these two points that lie along the bezier curves (i'm calling them intersection points above)... how can I calculate the two control points?
由于提前,这已经快把我逼疯了。
Thanks in advance, this has been driving me crazy.
有一些解释在这里: http://www.tinaja.com/glib/nubz4pts1.pdf但它写在后记和语言没有意义,我在所有 - 它在我的头上
There's some kind of explanation here: http://www.tinaja.com/glib/nubz4pts1.pdf but it's written in postscript and that language makes no sense to me at all - it's over my head.
推荐答案
有对通过4个点的曲线解决方案无限多的,但最好简单的解决办法就是尽量使曲线段长度成正比的弦长度。在code您链接到的是一阶近似的运作良好,并为pretty快。
There are an infinite number of solutions to a curve passing through 4 points, but the best simple solution is to try to make the curve segment lengths proportional to the chord lengths. The code you link to is the a first order approximation that works well and is pretty fast.
下面是后记code的C#编译:
Here's the C# translation of the PostScript code:
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;
}
}
我没有测试它,但它编译。只需拨打 DrawingUtility.BezierFromIntersection
与你有4点,它会返回一个的PathFigure
绘制曲线。
I haven't tested it, but it compiles. Just call DrawingUtility.BezierFromIntersection
with the 4 points you have, and it will return a PathFigure
for drawing the curve.
这篇关于如何找到控制点,在C#中给予启动BezierSegment,End和2相交积分 - AKA三次Bezier 4点插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!