2d, Point on line closested to point

Started by
4 comments, last by timw 18 years, 7 months ago
ive searched, but had no luck, I need it in Vector form( P + uV ) need, Formula to find Closest point on 2d line, to a 2d point. thanks in advance! =D
"I seek knowledge and to help those who also seek it"
Advertisement
Let the line be O+tD. The point is P. The squared distance from P to a point at t is:

f(t) = (d-tD)2

Where d = P-O. Expand:

f(t) = (D.D)t2-2(D.d)t+(d.d)

This is a quadratic function, always concave up and non-degenerate for valid input. We wish to find the value of t corresponding to the minimum value of f(t). Take the first derivative:

f'(t) = 2(D.D)t-2(D.d)

And solve for 0:

2(D.D)t-2(D.d) = 0
2(D.D)t = 2(D.d)
t = (2(D.d))/(2(D.D))
t = (D.d)/(D.D)

For a segment or ray, clamp t as appropriate. The closest point to P is then O+tD.
Thanks a ton! works great =D
"I seek knowledge and to help those who also seek it"
hmm one possible more efficent method would be to take take the point and subtract off the origin

ray = P + tV
vector = POINT - P;

this is the vector from the origin of your ray to the point, now simply take the scaler projection of vector onto V

distance = vector DOT V

now assuming V is normalized this is the signed distance from P to the closest point on the line.

so

ClosestPoint = P + (distance)*V;

seems like this would be a more efficent approach then the differentail minimization.

Tim
Quote:seems like this would be a more efficent approach then the differentail minimization.
It's exactly the same thing. Take the minimization solution, add the constraint that D is unit-length, and you get:

closest = O+dot(P-O,D)*D

The difference between the two methods is purely conceptual.
oh, I was looking at all those square terms and stuff and didn't realize you didn't need to calcuate them lol. just not reading enough sorry.

This topic is closed to new replies.

Advertisement