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