Distance from curve

Started by
2 comments, last by Alrecenk 16 years, 11 months ago
I have a curve defined by y = abs(x)^3, and a point. How do I find the exact distance from the point to the nearest point on the curve? Thanks in advance.
Advertisement
The curve is parameterized as (x(t),y(t)) = (t,t^3). The point of interest is (x0,y0). You need a point (x(t),y(t)) so that the vector (x(t)-x0,y(t)-y0) is perpendicular to the curve at (x(t),y(t)). The tangent to the curve is (x'(t),y'(t)) = (1,3*t^2), so you need 0 = Dot((x'(t),y'(t)),(x(t)-x0,y(t)-y0)). This condition reduces to 0 = (t-x0) + 3*t^2*(t^3-y0), which is a degree 5 polynomial in t. Numerically solve the polynomial equation for t.
I was basically going to say what Dave said before he ninja'd me to it, but you also need to be careful regarding that absolute value sign. What I would do is solve the polynomial for both x3 and -x3, and sort the solutions (by distance of course). Then return the first "good" solution, i.e. you have a solution where x is positive but it came from solving -x3 (which gives you a negative value), it doesn't satisfy |x|3 and you skip it.
In regards to the absolute value, you can move it from the function to the point you're checking since you know the closest point on the polynomial will have the same x sign as the point you're comparing it to. So take the absolute value of X at the start and ignore it in the function and it should work out right.

This topic is closed to new replies.

Advertisement