二次贝塞尔曲线上的最近点
我在计算二次曲线上距鼠标位置最近的点时遇到一些问题。我尝试了一些 API,但没有找到有效的函数。我找到了适用于五次三次贝塞尔曲线的实现,但我没有数学技能将其转换为二次曲线。我找到了一些方法,如果我有价值,可以帮助我解决问题,但我不知道如何开始找到t。如果有人可以向我指出一种查找 t 的算法,或者一些用于查找二次曲线上到任意点最近的点的示例代码,我将非常感激。
谢谢
I am having some issues calculating the nearest point on a quadratic curve to the mouse position. I have tried a handful of APIs, but have not had any luck finding a function for this that works. I have found an implementation that works for 5th degree cubic bezier curves, but I do not have the math skills to convert it to a quadratic curve. I have found some methods that will help me solve the problem if I have a t value, but I have no idea how to begin finding t. If someone could point me to an algorithm for finding t, or some example code for finding the nearest point on a quadratic curve to an arbitrary point, I'd be very grateful.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以让你开始数学。
我不确定二次贝塞尔曲线是如何定义的,但它必须是等效的
到:
其中
0 < t < 1.
. a、b、c 是定义曲线的 6 个常数。您想要到 (X, Y) 的距离:
由于您想找到使上述数量最小化的
t
,因此您可以取其相对于
t
的一阶导数并将其设置为 0。这给出了(去掉 sqrt 和因子 2):
这是
t
中的三次方程。解析解是已知的,您可以在网上找到它;您可能需要做一些代数运算才能获得t
的幂系数(即 0 = a + bt + ct^2 + dt^3)。您也可以使用牛顿-拉夫森等方法以数值方式求解该方程。但请注意,如果 3 个解决方案都不在您的范围内
0
0
t < 1.
.在这种情况下,只需计算t = 0
和t = 1
处到 (X, Y)(第一个方程)的距离值,并取最小距离二。编辑:
实际上,当你求解一阶导数 = 0 时,你得到的解可以是最大距离,也可以是最小距离。因此,您应该计算得到的解的距离(第一个方程)(最多 3 个
t
值),以及t=0
和处的距离>t=1
并选择所有这些值中的实际最小值。I can get you started on the math.
I'm not sure how quadratic Bezier is defined, but it must be equivalent
to:
where
0 < t < 1
. The a, b, c's are the 6 constants that define the curve.You want the distance to (X, Y):
Since you want to find
t
that minimizes the above quantity, you take itsfirst derivative relative to
t
and set that equal to 0.This gives you (dropping the sqrt and a factor of 2):
which is a cubic equation in
t
. The analytical solution is known and you can find it on the web; you will probably need to do a bit of algebra to get the coefficients of the powers oft
together (i.e. 0 = a + b t + c t^2 + d t^3). You could also solve this equation numerically instead, using for example Newton-Raphson.Note however that if none of the 3 solutions might be in your range
0 < t < 1
. In that case just compute the values of the distance to (X, Y) (the first equation) att = 0
andt = 1
and take the smallest distance of the two.EDIT:
actually, when you solve the first derivative = 0, the solutions you get can be maximum distances as well as minimum. So you should compute the distance (first equation) for the solutions you get (at most 3 values of
t
), as well as the distances att=0
andt=1
and pick the actual minimum of all those values.在线提供了一个可以轻松适应 Java 的 ActionScript 实现 此处。
它源自“Graphics Gems”一书中的算法,您可以在 Google 图书上仔细阅读该算法 此处。
There's an ActionScript implementation that could easily be adapted to Java available online here.
It's derived from an algorithm in the book "Graphics Gems" which you can peruse on Google Books here.
愚蠢、幼稚的方法是迭代曲线上的每个点并计算该点与鼠标位置之间的距离,最小的就是获胜者。不过,根据您的应用程序,您可能必须选择比这更好的东西。
The dumb, naive way would be to iterate through every point on the curve and calculate the distance between that point and the mouse position, with the smallest one being the winner. You might have to go with something better than that though depending on your application.