
Geometry  shortest distance between a point and line 
Mark Oates
Member #1,146
March 2001

I'm having trouble grasping this simple concept for some reason. Basically I have a point (A) and a line (BC), and I want to find the shortest distance between the two. Knowing that the shortest distance will be a line (AX) that has a slope perpendicular to the slope of BC and goes through A. So the intersection of the two lines is X. For some reason I can't seem to get the math to work out right?! Basically I find the slopeintercept form of both lines and then work the two out to find the x and y coordinates of point X, but this code isn't giving me the correct answers. Here's what I have:
What am I doing wrong?  
SiegeLord
Member #7,827
October 2006

Instead of trying to fix your algorithm I shall suggest you a better one. It is faster, and it can handle vertical/horizontal lines(which yours can't as of now). What you do is you find a vector from the first point on the line to the second point. Then you find the absolute value of the cross product between the two vectors(which find the area of the parallelogram formed by the two vectors) and then divide it by the magnitude of the first vector to find the height(which is what you want). Here's the code for that:
"For in much wisdom is much grief: and he that increases knowledge increases sorrow."Ecclesiastes 1:18 
Mark Oates
Member #1,146
March 2001

seems to work, but I'm guessing this assumes the line is of infinate length? How should I accomidate for this?  
Matthew Leverton
Supreme Loser
January 1999

Then it would be the end point. 
Mark Oates
Member #1,146
March 2001

Hmm, I still don't understand. How do I know when it's going to be the end point or a point along the line?  
SiegeLord
Member #7,827
October 2006

(x1,y1) and (x2,y2) are any two(noncoinciding) on the line. There is no order requirement. This method does indeed assume an infinite line. It shall return the distance from the line to the point regardless whether the perpendicular projection of it onto the line(point X in your diagram) is actually between the two points. If you want the distance of a point from a segment(which is what I assume you are referring to) you need a different approach. It is obviously slower. First you get the above vectors the same way. Then you calculate the dot product between them. You divide that by the length of the line segment to get the length of the projection. Then you divide it again by the same value to get the parameter value of that point. If that point parameter is less than 0, then it is behind the start of the segment, if it is more than 1 then it is in front of the end of the segment. If it is between those two then it is on the segment. You use the parameter to calculate the point(or choose one of the endpoints) and then use the distance formula to find the distance from the two points.
"For in much wisdom is much grief: and he that increases knowledge increases sorrow."Ecclesiastes 1:18 
Mark Oates
Member #1,146
March 2001

Thank you SO much! It's working perfectly now.  
Fladimir da Gorf
Member #1,565
October 2001

SiegeLord, could I by any chance add this function to OpenLayer (Line::GetShortestDistanceTo( const Vec2D &point ))? I think it'd be a nice addition OpenLayer has reached a random SVN version number ;)  Online manual  Installation video! MSVC projects now possible with cmake  Now alvailable as a DevC++ Devpack! (Thanks to Kotori) 
SiegeLord
Member #7,827
October 2006

Sure. It should also work for 3D lines/points as well. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."Ecclesiastes 1:18 
Fladimir da Gorf
Member #1,565
October 2001

OK, there it is now, thanks! OpenLayer has reached a random SVN version number ;)  Online manual  Installation video! MSVC projects now possible with cmake  Now alvailable as a DevC++ Devpack! (Thanks to Kotori) 
