POV-Ray : Newsgroups : povray.advanced-users : Distance to line segment? : Re: Distance to line segment? Server Time
29 Jul 2024 20:22:13 EDT (-0400)
  Re: Distance to line segment?  
From: Tor Olav Kristensen
Date: 22 Dec 2000 18:05:43
Message: <3A43DB80.979B89C3@online.no>
Ron Parker wrote:
> 
> 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)

It seems like you are subtracting 
a scalar from a vector expression.

Are you that this is the right thing 
to do here ?


> 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

For me it looks like there are some
problems with the above macro.

When I change it to this:

#macro DistToLS(P, A, B)

  #local SegLen = vlength(B - A);
  #local ParComp = vdot(P - A, B - A)/SegLen;

  #if (ParComp <= 0) 
    #local dd = vlength(P - A);
  #else 
    #if (ParComp >= SegLen)
      #local dd = vlength(P - B);
    #else
      #local dd = vlength(P - A - ParComp*vnormalize(B - A));
    #end // if
  #end // if

  dd
  
#end // DistToLS 

then it behaves like I would suspect
it too do (in POV v3.1)


Regards,

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.