Jump to content
  • Advertisement
Sign in to follow this  
sjf

Distance betwwen line and point? 0

This topic is 4891 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

Does anyone have a simple, quick and clean algorithm to find the shortest distance between a line (denoted by x1, z1, x2, z2) and a point (x0, z0)? Something to just cut ‘n’ paste?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Does anyone have a simple, quick and clean algorithm to find the shortest distance between a line (denoted by x1, z1, x2, z2) and a point (x0, z0)?
Yes:

Vector2 O(x1, z1);
Vector2 D(x2-x1, z2-z1);
Vector2 diff(x0-x1, z0-z1);
float t = diff.Dot(D) / D.Dot(D);
if (t >= 1.0f) diff -= D;
else if (t >= 0.0f) diff -= t*D;
return diff.Dot(diff); // Squared length

Share this post


Link to post
Share on other sites
Distance between a line and a point is defined as:

d = (| b x (p-a) |) / |b|

Where p is a vector containing the point, a is the origin of the line, and b is the vector of the line. Ill let you put that into code...

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!