Nearest point on a quadratic bezier curve

一个人想着一个人 提交于 2019-12-01 09:29:56

I can get you started on the math. I'm not sure how quadratic Bezier is defined, but it must be equivalent to:

(x(t), y(t)) = (a_x + b_x t + c_x t^2, a_y + b_y t + c_y t^2),

where 0 < t < 1. The a, b, c's are the 6 constants that define the curve.

You want the distance to (X, Y):

sqrt( (X - x(t))^2 + (Y - y(t))^2  )

Since you want to find t that minimizes the above quantity, you take its first derivative relative to t and set that equal to 0. This gives you (dropping the sqrt and a factor of 2):

0 = (a_x - X + b_x t + c_x t^2) (b_x + 2 c-x t) + (a_y - Y + b_y t + c_y t^2) ( b_y + 2 c_y t) 

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 of t 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) at t = 0 and t = 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 at t=0 and t=1 and pick the actual minimum of all those values.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!