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. :)