通过三个给定点绘制二次 Bézier 曲线
我在 2D 中有三个点,我想绘制一条穿过它们的二次贝塞尔曲线。如何计算中间控制点(如quadTo 中的x1
和y1
)?我在大学时了解线性代数,但需要一些简单的帮助。
如何计算中间控制点以使曲线也穿过它?
I have three points in 2D and I want to draw a quadratic Bézier curve passing through them. How do I calculate the middle control point (x1
and y1
as in quadTo)? I know linear algebra from college but need some simple help on this.
How can I calculate the middle control point so that the curve passes through it as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
令 P0、P1、P2 为控制点,Pc 为您希望曲线经过的固定点。
那么贝塞尔曲线的定义是
...其中 t 从 0 到 1。
您的问题有无限多个答案,因为它可能会通过您的点以获取任何 t 值...所以只需选择一个,例如t=0.5,并求解 P1:
“P”值是 (x,y) 对,因此只需对 x 应用一次方程,对 y 应用一次方程:
...其中 (xc,yc) 是您想要的点它要经过, (x0,y0) 为起点,(x2,y2) 为终点。这将为您提供在 t=0.5 处通过 (xc,yc) 的贝塞尔曲线。
Let P0, P1, P2 be the control points, and Pc be your fixed point you want the curve to pass through.
Then the Bezier curve is defined by
...where t goes from zero to 1.
There are an infinite number of answers to your question, since it might pass through your point for any value of t... So just pick one, like t=0.5, and solve for P1:
There the "P" values are (x,y) pairs, so just apply the equation once for x and once for y:
...where (xc,yc) is the point you want it to pass through, (x0,y0) is the start point, and (x2,y2) is the end point. This will give you a Bezier that passes through (xc,yc) at t=0.5.
令我们想要的四边形贝塞尔曲线为 P(t) = P1t^2 + PC2t(1-t) + P2*(1-t)^ 2
且四边形贝塞尔曲线经过投掷 P1,Pt,P2
通过三个点 P1,Pt,P3 的最佳四边形贝塞尔曲线具有控制点 PC,其张力指向曲线切线的垂直方向。该点也是该贝塞尔曲线的平分线。任何从 P1 开始并以 PC 结束的贝塞尔曲线位于通过抛出 PC 和 Pt 的直线上,以相同的参数 t 值切割四边形贝塞尔曲线。
PC 点不是通过贝塞尔曲线的参数位置 t=.5 实现。一般来说,对于任何 P1、Pt、P2,我们都会得到下一个公式中所述的 Pc。
生成的 PC 也是该贝塞尔曲线到 Pt 的近点,并且穿过 Pt 和 Pc 的直线是三角形 P1、Pt、Pc 的平分线。
这是描述定理和公式的论文 - 那是在我的网站 https://microbians.com/math/Gabriel_Suchowolski_Quadratic_bezier_through_third_points_and_the_-equivalent_quadratic_bezier_(theorem)-.pdf
这里还有代码
享受
Let the quad bezier we want to take as P(t) = P1t^2 + PC2t(1-t) + P2*(1-t)^2
and that quad bezier passing throw P1,Pt,P2
The best quad bezier that pass through the three points P1,Pt,P3 have the control point PC with tension directed in the perpendicular of the tangent of the curve. That point is also bisector of that bezier. Any bezier starting P1 and ending P3 with PC on the rect line that pass throw PC and Pt cuts the quad beziers at the same parametric t value.
The PC point is not achieved through the parametric position t=.5 of the bezier. In general for any P1,Pt,P2 the we got a Pc as described on the next formula.
The resulting PC is also the near point of that bezier to Pt and the rect line that pass through Pt and Pc is the bisector of the triangle P1, Pt, Pc.
Here is the paper where the theorem and formula is described -That is at my website https://microbians.com/math/Gabriel_Suchowolski_Quadratic_bezier_through_three_points_and_the_-equivalent_quadratic_bezier_(theorem)-.pdf
And also here is code
Enjoy
我在我的 JavaFX 应用程序中使用了 Nemos 答案,但我的目标是绘制曲线,以便曲线的视觉转折点始终与所选的固定转折点 (CP) 相符。
CP = 控制点
SP = 起点
EP = 端点
BP(t) = 贝塞尔曲线上的变量点,其中 t 介于 0 和 1 之间
为了实现此目的,我创建了一个变量 t(不固定为 0.5)。如果选择的 CP 点不再位于 SP 和 EP 之间,则必须稍微向上或向下改变 t。
第一步,您需要知道 CP 是否更接近 SP 还是 EP:
令距离SP为CP和SP之间的距离
distanceEP 是 CP 和 EP 之间的距离
然后我将比率定义为:
现在我们将使用它来上下改变 t:
注意:这仍然是一个近似值,1/3 是通过尝试和错误选择的。
这是我的 Java 函数:
(Point2D是JavaFX的一类)
我希望这会有所帮助。
I have used Nemos answer in my JavaFX apllication, but my goal was to draw the curve, so that the visual turning point of the curve always fits with the choosen fixed one (CP).
CP = ControlPoint
SP = StartPoint
EP = EndPoint
BP(t) = variable Point on BeziérCurve where t is between 0 and 1
To achieve this i made a variable t (not fix 0.5). If the choosen Point CP is no longer in the middle between SP and EP, you have to vary t up or down a little bit.
As a first step you need to know if CP is closer to SP or EP:
Let distanceSP be the distance between CP and SP
and distanceEP the distance between CP and EP
then i define ratio as:
Now we are going to use this to vary t up and down:
note: This is still an approximation and 1/3 is choosen by try and error.
Here is my Java-Function:
(Point2D is a class of JavaFX)
I hope this helps.
如果您不需要精确的中点,而是想要 t 的任何值(0 到 1),则方程为:
当然,这也适用于中点,只需将 t 设置为 0.5。简单的! :-)
If you don't want the exact middle point, rather you want any value for t (0 to 1), the equation is:
Of course, this will also work for the mid point, just set t to be 0.5. Simple! :-)