Skip to main content
GameDev.net gamedev.net
🔒 Locked

Distance from point to line segment

Started by Andos Dec 26, 2003 at 3:38 PM 9 replies 31.5k views
Original Post
Andos
Andos
I have read this article about distances from point to line and line segments http://geometryalgorithms.com/Archive/algorithm_0102/algorithm_0102.htm#Distance%20to%20Ray%20or%20Segment but i don''t understand the last part about the line segments. I currently have the distance from the point to the infinite line but i just don''t understand what this article says about making it work for a line segment. Are there an easier way? In other words: If i have a line Xa,Ya,Yb,Yb: |----------| How can i tell if the point is not ''over'' the line so i can use the normal distance formulae to the point. And how to tell what point to find the distance to if the point is not ''over'' the line? I hope you understand
idinkin
idinkin
How do you define distance of a point from line segment?



It does not matter what you will reach in your life, but the way you will reach it
Sometimes movement is a result of a kick in the ass!
Andos
Andos
Well I can now find the smallest distance from a point and an infinite line. That infinite line is given by two x,y cordinates Xa,Ya and Xb,Yb

now i want the line not to be infinite and only have the length that is between the two cordinates. So how can i now get the smallest distance to the line from any point?
Promit
Promit
If you draw a segment from the point to the closest point on the line, that segment will always be perpendicular to your line. So if you have a line:
y = ax + b
and a point
(c, d)

The segment connecting that point and the closest point on the line will have a slope of -1/a and you''ll have to substitute in order to find the intercept. Once you know the connecting segment, you can find where it intersects the original line and calculate a distance.

Probably not the fastest way, but clear and simple.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Ximmer
Ximmer
this is some code I use for finding the closest point on a line segment


// a is the first point on the line segment
// b is the second point on the line segment
// Point is the point your trying to find
Vector ClosestPointOnLine(const Vector & a, const Vector & b, const Vector & Point)
{
Vector c = Point - a; // Vector from a to Point
Vector v = (b - a).Normalize(); // Unit Vector from a to b
float d = (b - a).Length(); // Length of the line segment
float t = v.DotProduct(c); // Intersection point Distance from a

// Check to see if the point is on the line
// if not then return the endpoint
if(t < 0) return a;
if(t > d) return b;

// get the distance to move from point a
v *= t;

// move from point a to the nearest point on the segment
return a + v;
}
Andos
Andos
"you''ll have to substitute in order to find the intercept. Once you know the connecting segment, you can find where it intersects the original line and calculate a distance."

I didn''t understand that part. I don''t have the slope (a) of my line but of course i could get it but maybe there was other methods.




And i probalbly should have written; i don''t use a programming language to create my game so i can''t use that code because i don''t understand it. I''m using a completly other language/script sort of thing to make my game. I''m not english either so I also have some difficulties understanding some of the complex english maths expressions. I was hoping to get it explained with more simple english words so i could better understand it

Anyway here is a picture that maybe could help you understand what i want:
http://www.andosmedia.com/other/line.gif
How can i tell if my point X,Y is in the green area or in the red area by math?
billybob
billybob
here is how I do it (this is the body of a small function)

if(T)
*T = -(Origin - Vector).Dot(Dir) / Dir.LengthSq();
return Dir.Cross(Origin - Vector).Length() / Dir.Length();

Origin is point A on the line, Dir is the direction, so 'Dir = point B - point A'. Vector is the point you are finding the distance to. T is a pointer to a float, it represents the position on the line. 0.0 is point A, 1.0 is point B, so if T is in the range [0, 1] then the intersection is on the line segment, and if its outside that range then its in the red or green area in your picture. this is based on the derivation from that mathworld wolfram site, if you google for 'line point distance wolfram', i would imagine it comes up.

edit: i just noticed you are using 2D. if you are lazy just put 0 for z in all the vectors, that should work. the wolfram site probably has a 2D version

http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html

it has a link to the 2D version at the bottom, but i didn't see a method of checking whether it was on the segment or not for it. thats why i gave the 3D version link.

[edited by - billybob on December 27, 2003 6:24:53 PM]
Andos
Andos
Thanks alot. Sorry I could not explain what i wanted clearly.
Thanks again.
Andos
Andos
Altlast i managed to put together some psuedo code for the distance from point to line:


// Psuedocode for returning the absolute distance to a line segment from a point.
//Xa,Ya is point 1 on the line segment.
//Xb,Yb is point 2 on the line segment.
//Xp,Yp is the point.

xu = xp - xa;
yu = yp - ya;
xv = xb - xa;
yv = yb - ya;
if (xu * xv + yu * yv < 0)
then return sqrt( (Xp-Xa)^2 + (Yp-Ya)^2);

xu = xp - xb;
yu = yp - yb;
xv = -xv;
yv = -yv;
if (xu * xv + yu * yv < 0)
then return sqrt( (Xp-Xb)^2 + (Yp-Yb)^2 );

return
Abs( ( Xp * ( Ya - Yb ) + Yp * ( Xb - Xa ) + ( Xa * Yb - Xb * Ya ) ) / Sqrt( ( Xb - Xa )^2 + ( Yb - Ya )^2 ) );




Thanks all.
YoshiN
YoshiN
quote:
Original post by Promit
If you draw a segment from the point to the closest point on the line, that segment will always be perpendicular to your line. So if you have a line:
y = ax + b
and a point
(c, d)

The segment connecting that point and the closest point on the line will have a slope of -1/a and you''ll have to substitute in order to find the intercept. Once you know the connecting segment, you can find where it intersects the original line and calculate a distance.

Probably not the fastest way, but clear and simple.


Nope. That''s true for a line of infinite length, but not necessarily true for a line segment. For a line segment it will either be perpendicular or it will pass through one of the segment''s endpoints.
-YoshiXGXCX ''99

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.