计算切线垂直于锚线的三次贝塞尔曲线 T 值

发布于 2024-08-29 16:01:10 字数 650 浏览 3 评论 0原文

将三次贝塞尔曲线 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2024-09-05 16:01:10

假设您有一条 P0 = 0P3 = 1 的一维三次贝塞尔曲线,则曲线为:

P(t) = b0,3(t)*0 + b1,3(t)*P1 + b2,3(t)*P2 + b3,3(t)*1

其中 bi,3(t)是 3 次的 Bernstein 多项式。然后我们寻找 的值t,其中 P(t) 是最小和最大,因此我们得出:

P'(t) = b1,3'(t)*P1 + b2,3'(t)*P2 + b3,3'(t)
      = (3 - 12t + 9t^2)*P1 + (6t - 9t^2)*P2 + 3t^2
      = 0

这有一个封闭形式但不平凡的解决方案。根据 WolframAlpha,当 3P1 - 3P2 +1 != 0 时,它是:

t = [2*P1 - P2 +/- sqrt(P1^2-P1*P2-P1+P2^2)] / (3*P1 - 3*P2 + 1)

否则它是:

t = 3P1 / (6P1 - 2)

对于一般的 n 维三次贝塞尔曲线 P0*, P1*, P2*, P3* 计算:

P1 = proj(P1*, P03*) / |P3* - P0*|
P2 = proj(P2*, P03*) / |P3* - P0*|

其中 proj(P, P03*) 是从 P0* 到投影在经过 P0* 的直线上的点 P 的有符号距离> 和 P3*

(我没有检查过这一点,所以请确认我的推理没有任何错误。)

Let's assume you got a 1D cubic Bézier curve with P0 = 0 and P3 = 1 then the curve is:

P(t) = b0,3(t)*0 + b1,3(t)*P1 + b2,3(t)*P2 + b3,3(t)*1

Where bi,3(t) are the Bernstein polynomials of degree 3. Then we're looking for the value of t where this P(t) is minimal and maximal, so we derive:

P'(t) = b1,3'(t)*P1 + b2,3'(t)*P2 + b3,3'(t)
      = (3 - 12t + 9t^2)*P1 + (6t - 9t^2)*P2 + 3t^2
      = 0

This has a closed-form but nontrivial solution. According to WolframAlpha, when 3P1 - 3P2 +1 != 0 it's:

t = [2*P1 - P2 +/- sqrt(P1^2-P1*P2-P1+P2^2)] / (3*P1 - 3*P2 + 1)

Otherwise it's:

t = 3P1 / (6P1 - 2)

For a general n-dimensional cubic Bézier P0*, P1*, P2*, P3* compute:

P1 = proj(P1*, P03*) / |P3* - P0*|
P2 = proj(P2*, P03*) / |P3* - P0*|

Where proj(P, P03*) is the signed distance from P0* to the point P projected on the line passing through P0* and P3*.

(I haven't checked this, so please confirm there is nothing wrong in my reasoning.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文