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
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
tthat minimizes the above quantity, you take itsfirst derivative relative to
tand 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 ofttogether (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 = 0andt = 1and 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=0andt=1and pick the actual minimum of all those values.