POV-Ray : Newsgroups : povray.advanced-users : Distance to line segment? : Re: Distance to line segment? Server Time
29 Jul 2024 20:12:47 EDT (-0400)
  Re: Distance to line segment?  
From: Tor Olav Kristensen
Date: 22 Dec 2000 17:58:25
Message: <3A43D9BC.1EBB5DB4@online.no>
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


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.