Jump to content
  • Advertisement
Sign in to follow this  
Xero-X2

2d, Point on line closested to point

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!