|
|
On Thu, 21 Dec 2000 22:23:23 +0100, Rune wrote:
>How do I find the distance from the point P to the nearest point in the line
>segment defined by point A and point B?
The distance to the line defined by A and B is given by
P-A-vdot(P-A,B-A)/vlength(B-A)
However, that ignores the "line segment" part of your question. If
vdot(P-A,B-A) is less than zero, you should instead use vlength(P-A).
If vdot(P-A,B-A)/vlength(B-A) is greater than vlength(B-A), you should
use vlength(P-B). So, in POV code:
#macro DistToLS(P,A,B)
#local SegLen = vlength(B-A);
#local ParComp = vdot(P-A,B-A)/SegLen;
#if ( ParComp < 0 )
vlength(P-A)
#else
#if (ParComp > SegLen)
vlength(P-B)
#else
(P-A-ParComp)
#end
#end
#end
--
Ron Parker http://www2.fwi.com/~parkerr/traces.html
My opinions. Mine. Not anyone else's.
Post a reply to this message
|
|