View Full Version : Distance Point To Line

Andrew Jameson

04-20-2003, 03:54 AM

Hi,

I'm surprised that I can't find this via Google but someone must know this piece of 3D maths.

Ok ... I've got a line LS and LE where LS and LE are vectors and a point P. I need to know the minimum distance from the Point to the line ... it's obviously the perpendicular intersection of the line but I can only figure it out in 2D !

Also, as the line's not infinite then it should return the actual nearest distance.

Thanks

Andrew

Does the short answer at http://www.exsanity.freeola.com/nearestpoint.html help any?

Andrew Jameson

04-20-2003, 01:12 PM

Thanks for your response ... oddly enough this was the only example that I came across but I couldn't understand the simplified maths ...

Direction = EndLine - StartLine

Direction is obviously a vector but it's this next one that's got me ...

Projection = ( Point - StartOfLine).Direction

-----------------------------------

Direction^2

Could someone please expand this a little ... it looks as though Projection is a GLfloat and the rest ... well ... it looks like a VectorDotProduct of Point-StartOfLine and Direction all divided by the square of Direction ... which ends up as a vector and not a GLfloat.

Thanks

Andrew

hkyProgrammer88

04-20-2003, 02:11 PM

How can I display pictures onto this forum? It would be alot easier to answer with a picture.

I too am confused by the mathmatical explanation however, with a picture it is very easy to understand. I will post here again in a few minutes when I figure out how (to post pics).

Andrew Jameson

04-20-2003, 02:38 PM

Although this thread will be of interest to others ... dare I suggest that you e-mail the info direct to me ?

Andrew

Old GLman

04-20-2003, 02:58 PM

You have the two endpoints of the line segment and an arbitrary point. You wish to find the shortest distance between the point and the line.

Let p0 be the point.

Let p1 and p2 be opposite ends of the line segment.

then to find the distance:

distance = |(p2-p1) x (p1-p0)|

-------------------

|(p2-p1)|

where:

x is the cross product

- is vector substraction

| is vector magnitude

Old GLman

Andrew Jameson

04-21-2003, 02:17 AM

Thanks for everyone's help ... what I came up with in the end is ...

function PointToLineDistance(vPoint, vStart, vEnd : TVector) : GLfloat;

var

Direction : TVector;

Projection : GLfloat;

NearPoint : TVector;

begin

Direction := VectorSubtract(vEnd, vStart);

Projection := VectorDotProduct(VectorSubtract(vPoint, vStart), Direction) / VectorLength(VectorSquared(Direction));

if (Projection < 0) then

NearPoint := vStart

else if (Projection > 1) then

NearPoint := vEnd

else

NearPoint := VectorAdd(VectorMultiply(vEnd, Projection), VectorMultiply(vStart, 1-Projection));

Result := VectorLength(VectorSubtract(NearPoint, vPoint));

end; {PointToLineDistance}

Again thanks,

Andrew

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.