简体   繁体   English

如何找到贝塞尔曲线的控制点?

[英]How do I find the control points for a Bezier curve?

I need to implement connections in the form of curved lines in C# (Unity).我需要在 C# (Unity) 中以曲线的形式实现连接。 I would like to get the result as similar as possible to the implementation in Miro.com (see screenshot).我希望得到与 Miro.com 中的实现尽可能相似的结果(见截图)。

After attaching the curve, I calculate the path of the cubic Bezier curve.附加曲线后,我计算三次贝塞尔曲线的路径。 For this first segment, the anchor points and offsets from the objects it connects are used.对于第一段,使用锚点和与其连接的对象的偏移量。 There are no problems at this stage.在这个阶段没有任何问题。

Problem : When dividing the curve into segments by clicking and dragging one of the blue points of the segment (see screenshot), it is split in two in the middle.问题:当通过单击并拖动线段的一个蓝点(见截图)将曲线分割成线段时,它在中间被一分为二。 At the junction of two new curves, a new interactive (movable) point is formed for which the tangent and coordinates of the control points are unknown.在两条新曲线的交界处,形成了一个新的交互(可移动)点,其控制点的切线和坐标是未知的。 I need to find the position of these control points every time the position of the interactive points changes (white points in the picture below).每次交互点的position发生变化(下图中的白点)时,我都需要找到这些控制点的position。 Moreover, the curve should not drastically change its position when dividing, not form loops, have different lengths of control point vectors (I'm not sure here) and behave as adequately as possible (like on the board in Miro).此外,曲线在划分时不应显着改变其 position,而不是形成循环,具有不同长度的控制点向量(我在这里不确定)并尽可能充分地表现(就像在 Miro 的板上)。

By control points I mean 2 invisible guide points for the Bezier segment.控制点是指 Bezier 段的 2 个不可见的引导点。

In black I painted the known control points, and in red those that I need to find.我用黑色绘制了已知的控制点,用红色绘制了我需要找到的那些。 (Pn - interactive points, Cn - control points) (Pn - 交互点,Cn - 控制点) Miro.com 中的弯曲连接

The algorithms I have tried to find them give incorrect distances and directions of control points.我试图找到它们的算法给出了不正确的控制点距离和方向。

The following algorithms were tested:测试了以下算法:

  1. Interpolation from Tacent - jumps of the curve when separating, inappropriate direction and amount of indentation of control points; Interpolation from Tacent - 分离时曲线的跳跃,控制点的方向和缩进量不合适;
  2. Chaikin's algorithm - curve jumps during separation, creates loops; Chaikin 算法 - 分离过程中的曲线跳跃,创建循环;
  3. "Custom" interpolation based on guesses (takes into account the distance to the center of the segment between the start and end points of the segment, as well as the direction between the start and end points) - has all the same problems, but looks slightly better than those above.基于猜测的“自定义”插值(考虑到线段起点和终点之间到线段中心的距离,以及起点和终点之间的方向) - 有所有相同的问题,但看起来比上面的稍微好一点。

I suspect the solution is to chordally interpolate the points using a Catmull-Rom spline and translate the result to points for a Bezier curve.我怀疑解决方案是使用 Catmull-Rom 样条对点进行弦内插,并将结果转换为贝塞尔曲线的点。 However, there are still problems with implementation.但是,实施上仍然存在问题。

The curves from 3DMax also look very similar.来自 3DMax 的曲线看起来也非常相似。 In their documentation, I found only a mention of the parametric curve.在他们的文档中,我发现只提到了参数曲线。

3DMax中的曲线米罗的相同曲线

Methods that I did not use (or did not work):我没有使用(或不起作用)的方法:

  1. Catmull-Rom interpolation; Catmull-Rom 插值;
  2. B-spline interpolation; B样条插值;
  3. Hermitian interpolation;厄米插值;
  4. De Casteljau's algorithm (although it seems not for this) De Casteljau 的算法(虽然它似乎不适合这个)

I would be immensely grateful for any help, but I ask for as much detail as possible.我将非常感谢任何帮助,但我要求尽可能详细。

Find helpful sources to understand bezier curves here and here .在此处此处查找有用的资源以了解贝塞尔曲线。

To do what you want, I would give a try to the Catmull-Rom approach which I believe is much more simple than Bezier's, which is the one used in theitween asset, that is free, and you got plenty of funtionality implemented.为了做你想做的事,我会尝试 Catmull-Rom 方法,我认为它比 Bezier 方法简单得多,它是在itween资产中使用的一种,它是免费的,并且你实现了很多功能。

If you want to stick to the bezier curves and finding the control points, I will tell you what I would do to find them.如果你想坚持贝塞尔曲线并找到控制点,我会告诉你我会怎么做才能找到它们。

For the case of 2 control point bezier curve:对于 2 个控制点贝塞尔曲线的情况:

P = (1-t)P1 + tP2 P = (1-t)P1 + tP2

To get to know the control points P1(x1,y1) and P2(x2,y2), you need to apply the equation in a known point of your curve.要了解控制点 P1(x1,y1) 和 P2(x2,y2),您需要将方程应用于曲线的已知点。 Take into account that the 2D equation is vectorial, so each points provides 2 equations one for x and one for y, and you got 4 unknows, x and y for each point.考虑到二维方程是矢量的,所以每个点提供 2 个方程,一个用于 x,一个用于 y,每个点都有 4 个未知数,x 和 y。

So for the first node of the curve (t=0), you would have:因此,对于曲线的第一个节点 (t=0),您将拥有:

Px = (1-0)P1x + 0*P2x Px = (1-0)P1x + 0*P2x

Py = (1-0)P1y + 0*P2y Py = (1-0)P1y + 0*P2y

For the last point (t=1)对于最后一点 (t=1)

Px = (1-1)P1x + 1*P2x Px = (1-1)P1x + 1*P2x

Py = (1-1)P1y + 1*P2y Py = (1-1)P1y + 1*P2y

With these 4 equations I would try to achieve the control points P1 and P2.通过这 4 个方程,我将尝试实现控制点 P1 和 P2。 You can do it with t=0 and t=1 which are the supposed points you know of your curve and the ones that simplify the math due to the t values, but you should be able to use any as long as you know the points coords in the curve for determined t.您可以使用 t=0 和 t=1 来执行此操作,它们是您知道曲线的假定点,以及由于 t 值而简化数学的点,但只要您知道这些点,您就应该能够使用任何点确定的 t 曲线中的坐标。

If the curve is a 3 control point bezier, you would need 6 equations for the 3 control points and so on.如果曲线是 3 个控制点贝塞尔曲线,则 3 个控制点需要 6 个方程,依此类推。

I think that the best approach is to compound the curve of cuadratic curves composition, and calculate the control points for each chunk, but I am not sure about this.我认为最好的方法是复合三次曲线合成曲线,并计算每个块的控制点,但我不确定这一点。

Once maths are understood and control points achieved, In case that was successful I would try to implement that in the code.一旦理解了数学并实现了控制点,如果成功,我将尝试在代码中实现它。

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

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