Jump to content
  • Advertisement
Sign in to follow this  
Kest

Distance from infinite line to point

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

image hosted by ImageVenue.com I have: length (distance), dir (normalized), PA (pos), PB (pos), X (pos, grey "+" on the image) I want: DR (distance) What would be the most efficient way to obtain the DR distance? I've been calculating it like this..
float DL = Dot( dir, X - PA );
Vector IntersectPoint = PA + dir*DL;
float DR = (IntersectPoint - X).Length();
But I'm hoping there's a simpler and more efficient approach to this? Help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Your method is not too bad. An alternative is to use the Pythagorean theorem this way:

float distance(Point &X, Line &L){
Vector AX = X - L.A;
float DL = Dot(L.dir,AX);
return sqrt(Dot(AX,AX) - DL*DL);
}




The performance killer in either case is the square root, just as with computing the distance between two points. In many cases you can get away with just computing the square of the distance.

Share this post


Link to post
Share on other sites
Optimizing this is fairly pointless, to get any actual performance increase you need far more context then this, preferable the surrounding loop that it can be inlined into for better instruction scheduling, etc...

However, the easiest to understand / most logical way IMO is to project x into the line then get the component perpendicular to the line by subtracting this from x and finally take the length of the result, assuming d = dir is unit length then
Proj_d(x) = <x, d> * d
Ortho_d(x) = x - Proj_d(x) = x - <x, d> * d
dist = |x - <x, d> * d|

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!