POV-Ray : Newsgroups : povray.advanced-users : Distance to line segment? : Re: Distance to line segment? Server Time
29 Jul 2024 20:16:44 EDT (-0400)
  Re: Distance to line segment?  
From: Tor Olav Kristensen
Date: 22 Dec 2000 18:34:06
Message: <3A43E229.47AFFF18@online.no>
Tor Olav Kristensen wrote:
> 
> 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?
> 
> I would do it like this:
> 
> #macro Dist2LnSegm(pP, pA, pB)
> 
>   #local vL = pB - pA;
>   #local ss = vdot(pP - pA, vL)/vdot(vL, vL);
> 
>   vlength(pP - (ss >= 1 ? pB : pA + max(ss, 0)*vL))
> 
> #end // Dist2LnSegm


Changing the last expression in this macro to:

  vlength(pP - pA + max(0, min(ss, 1))*vL)

or

  vlength(pP - (ss >= 1 ? pB : (ss <= 0 ? pA : pA + ss*vL)))

also seems to work.


Tor Olav
--
mailto:tor### [at] hotmailcom
http://www.crosswinds.net/~tok/tokrays.html


Post a reply to this message

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