Distance between point and line segment
I'm having one of those brain fart days. :( (Lack of sleep on a Friday and all that.)
Could anyone please help by sending in a nice and simple algorithm for determining the shortest distance between a point (px,py) and a line segment(x1,y1)-(x2,y2)? Thanks. :)
Re: Distance between point and line segment
Quote:
Originally Posted by
Anthony Mai
Obviously Philip Nicoletti's code is a bad example, since it used "if" and "else", No boolean logic should be involved in a simple calculation like determing the distance from a point to a line segment.
Suppose you have points A(xa, ya), B(xb, yb) and C(xc,yc). The distance between point C and line segment AB equals the area of parallelgram ABCC' divided by the length of AB.
So it can be written as simple as:
distance = |AB X AC| / sqrt(AB * AB)
Here X mean cross product of vectors, and * mean dot product of vectors. This applied in both 2 dimentional and three dimentioanl space.
In 2-D it becomes:
sqrt(((yb-ya)*(xc-xa)+(xb-xa)*(yc-ya))^2/((xb-xa)^2 + (yb-ya)^2))
I believe your answer is incorrect - the area of the parallelogram is the det of the 2x2 matrix formed by the points: it should therefore be:
Code:
(yb-ya)*(xc-xa)-(xb-xa)*(yc-ya)
Re: Distance between point and line segment
Quote:
Originally Posted by
Anthony Mai
Obviously Philip Nicoletti's code is a bad example, since it used "if" and "else", No boolean logic should be involved in a simple calculation like determing the distance from a point to a line segment.
Finding the shortest distance to a line segment requires you to consider three distances; One to the line and two to the endpoints. To select the shortest of these requires logical statements because selection always does.
Re: Distance between point and line segment
Re: Distance between point and line segment
Re: Distance between point and line segment
Quote:
Originally Posted by
JohnW@Wessex
This may help too.
I hope you know the difference between a line and a line segment?
Re: Distance between point and line segment
can anyone explain why you are digging out a seven year old thread?
Re: Distance between point and line segment
Quote:
Originally Posted by
Richard.J
can anyone explain why you are digging out a seven year old thread?
My bad - I was researching how to find the distance between a line segment and a point. I found this on google, implemented the ingenious suggestion of using areas and discovered it to be flawed so registered to post a correction in the spirit of making the web a better place...
Re: Distance between point and line segment
Quote:
Originally Posted by
arctanb
My bad - I was researching how to find the distance between a line segment and a point. I found this on google, implemented the ingenious suggestion of using areas and discovered it to be flawed so registered to post a correction in the spirit of making the web a better place...
It's a good initiative.
Now if people only new the difference between a line and a line segment. :)
Re: Distance between point and line segment
Quote:
Originally Posted by
nuzzle
Now if people only new the difference between a line and a line segment. :)
... and indeed a ray ;)
OK I think it's time for me to stop unwittingly bumping this topic
Re: Distance between point and line segment
Quote:
Originally Posted by
arctanb
... and indeed a ray ;)
OK I think it's time for me to stop unwittingly bumping this topic
Well, the problem has an optimal algoritmic solution I think (see for example 3D Game Engine Design by Eberly).
But who knows, maybe someone comes up with something better. This happens too often to risk anything so please keep bumping the thread. :)
Re: Distance between point and line segment
This thing shows up on google if you search for this, and I really hate getting 15 search results that give you "Here's an algorithm that's got your solution in it somewhere! If you can find it! Maybe!" The most important problem here is that while this equation here ALMOST works:
http://mathworld.wolfram.com/images/...dEquation8.gif
It actually treats your segment as a line, which is useless. I found the answer buried in some obscure c code that does a variation on this technique: http://local.wasp.uwa.edu.au/~pbourk...tline/source.c - this contains a check that tells us if the point is within the line segment or not. Due to the similarity of the approaches i was able to integrate this into the previous equation for a function that, after almost 6 freaking hours of research, actually does exactly what we want it to do, under all circumstances.
Code:
float DistanceToPointSquared(float X, float Y)
{
float vx = x1-X, vy = y1-Y, ux = x2-x1, uy = y2-y1;
float length = ux*ux+uy*uy;
float det = (-vx*ux)+(-vy*uy); //if this is < 0 or > length then its outside the line segment
if(det<0 || det>length)
{
ux=x2-X;
uy=y2-Y;
return std::min<float>(vx*vx+vy*vy, ux*ux+uy*uy);
}
det = ux*vy-uy*vx;
return (det*det)/length;
}
Re: Distance between point and line segment
keke, Necropost, but.
The code described above fails on the case point: 0,0. Line point0: 1,1, Line point1: 2,2.
The code:
public float DistanceSquared(float X, float Y)
{
float vx = v0.x - X, vy = v0.y - Y, ux = v1.x - v0.x, uy = v1.y - v0.y;
float length = ux * ux + uy * uy;
float det = (-vx * ux) + (-vy * uy); //if this is < 0 or > length then its outside the line segment
if (det < 0)
return (v0.x - X) * (v0.x - X) + (v0.y - Y) * (v0.y - Y);
if (det > length)
return (v1.x - X) * (v1.x - X) + (v1.y - Y) * (v1.y - Y);
det = ux * vy - uy * vx;
return (det * det) / length;
}
takes this into consideration. I think ( run a couple of tests, when point is outside the segment from both ends, above and below).
Also, v0 is first vertex of the segment. v1 is the second one.
Re: Distance between point and line segment
This page mentions some useful info too.
Look for 'Line-Point Distance', second paragraph.
http://community.topcoder.com/tc?mod...s&d2=geometry1
Re: Distance between point and line segment
This thread is over 9 years old.
Re: Distance between point and line segment
Quote:
Originally Posted by
Richard.J
can anyone explain why you are digging out a seven year old thread?
Because the distance between a point and a line segment was only relevant pre-2003, right? ;)
Re: Distance between point and line segment
Quote:
Originally Posted by
JohnW@Wessex
Thank you! Finally! Exactly what I have been looking for for quite a while now.
PS: 2015, and the thread is still relevant ;)
Re: Distance between point and line segment
For calculating the distance between the point and line http://queforum.com/c-cpp-programs/9...nctions-c.html