计算切线垂直于锚线的三次贝塞尔曲线 T 值
将三次贝塞尔曲线 p1,p2,p3,p4 投影到直线 p1,p4 上。当p2或p3没有投影到p1和p4之间的线段上时,曲线将从锚点处凸出。有没有办法计算曲线切线垂直于锚线的T值?
这也可以表示为找到投影曲线距离线段 p1,p4 的中心最远的 T 值。当p2和p3投影到线段上时,解分别为0和1。是否有一个方程可以解决更有趣的情况?
T 值似乎仅取决于映射控制点与锚线段的距离。
我可以通过完善猜测来确定该值,但我希望有更好的方法。
编辑:
从 2d 中的 p1,..,p4 开始,值为 x1,y1, ..., x4,y4 我根据 Philippe 的答案使用以下代码:
dx = x4 - x1;
dy = y4 - y1;
d2 = dx*dx + dy*dy;
p1 = ( (x2-x1)*dx + (y2-y1)*dy ) / d2;
p2 = ( (x3-x1)*dx + (y3-y1)*dy ) / d2;
tr = sqrt( p1*p1 - p1*p2 - p1 + p2*p2 );
t1 = ( 2*p1 - p2 - tr ) / ( 3*p1 - 3*p2 + 1 );
t2 = ( 2*p1 - p2 + tr ) / ( 3*p1 - 3*p2 + 1 );
在我查看的示例中,必须从1.0之前是正确的。
Project a cubic bezier p1,p2,p3,p4 onto the line p1,p4. When p2 or p3 does not project onto the line segment between p1 and p4, the curve will bulge out from the anchor points. Is there a way to calculate the T value where the tangent of the curve is perpendicular to the anchor line?
This could also be stated as finding the T values where the projected curve is farthest from the center of the line segment p1,p4. When p2 and p3 project onto the line segment, then the solutions are 0 and 1 respectively. Is there an equation for solving the more interesting case?
The T value seems to depend only on the distance of the mapped control points from the anchor line segment.
I can determine the value by refining guesses, but I am hoping there is a better way.
Edit:
Starting with p1,..,p4 in 2d with values x1,y1, ..., x4,y4 I use the following code based on the answer from Philippe:
dx = x4 - x1;
dy = y4 - y1;
d2 = dx*dx + dy*dy;
p1 = ( (x2-x1)*dx + (y2-y1)*dy ) / d2;
p2 = ( (x3-x1)*dx + (y3-y1)*dy ) / d2;
tr = sqrt( p1*p1 - p1*p2 - p1 + p2*p2 );
t1 = ( 2*p1 - p2 - tr ) / ( 3*p1 - 3*p2 + 1 );
t2 = ( 2*p1 - p2 + tr ) / ( 3*p1 - 3*p2 + 1 );
In the sample I looked at, t2 had to be subtracted from 1.0 before it was correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您有一条
P0 = 0
和P3 = 1
的一维三次贝塞尔曲线,则曲线为:其中
bi,3(t)
是 3 次的 Bernstein 多项式。然后我们寻找的值t
,其中P(t)
是最小和最大,因此我们得出:这有一个封闭形式但不平凡的解决方案。根据 WolframAlpha,当
3P1 - 3P2 +1 != 0
时,它是:否则它是:
对于一般的 n 维三次贝塞尔曲线 P0*, P1*, P2*, P3* 计算:
其中
proj(P, P03*)
是从P0*
到投影在经过P0*
的直线上的点P
的有符号距离> 和P3*
。(我没有检查过这一点,所以请确认我的推理没有任何错误。)
Let's assume you got a 1D cubic Bézier curve with
P0 = 0
andP3 = 1
then the curve is:Where
bi,3(t)
are the Bernstein polynomials of degree 3. Then we're looking for the value oft
where thisP(t)
is minimal and maximal, so we derive:This has a closed-form but nontrivial solution. According to WolframAlpha, when
3P1 - 3P2 +1 != 0
it's:Otherwise it's:
For a general n-dimensional cubic Bézier P0*, P1*, P2*, P3* compute:
Where
proj(P, P03*)
is the signed distance fromP0*
to the pointP
projected on the line passing throughP0*
andP3*
.(I haven't checked this, so please confirm there is nothing wrong in my reasoning.)