# 2d, Point on line closested to point

This topic is 4889 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
Thanks a ton! works great =D

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634060
• Total Posts
3015300
×