POV-Ray : Newsgroups : povray.advanced-users : Distance to line segment? : Re: Distance to line segment? Server Time
29 Jul 2024 20:25:11 EDT (-0400)
  Re: Distance to line segment?  
From: Ron Parker
Date: 21 Dec 2000 16:38:22
Message: <slrn944u2g.3r9.ron.parker@fwi.com>
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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.