POV-Ray : Newsgroups : povray.advanced-users : Distance to line segment? Server Time
26 Nov 2024 06:35:05 EST (-0500)
  Distance to line segment? (Message 1 to 10 of 10)  
From: Rune
Subject: Distance to line segment?
Date: 21 Dec 2000 16:25:15
Message: <3a42753b@news.povray.org>
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?

Thanks in advance!

Rune
--
\ Include files, tutorials, 3D images, raytracing jokes,
/ The POV Desktop Theme, and The POV-Ray Logo Contest can
\ all be found at http://rsj.mobilixnet.dk (updated December 17)
/ Also visit http://www.povrayusers.org


Post a reply to this message

From: Ron Parker
Subject: Re: Distance to line segment?
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

From: Rune
Subject: Re: Distance to line segment?
Date: 22 Dec 2000 17:55:46
Message: <3a43dbf2@news.povray.org>
"Ron Parker" wrote:
> #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

Thanks you!

Rune
--
\ Include files, tutorials, 3D images, raytracing jokes,
/ The POV Desktop Theme, and The POV-Ray Logo Contest can
\ all be found at http://rsj.mobilixnet.dk (updated December 17)
/ Also visit http://www.povrayusers.org


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Distance to line segment?
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

From: Tor Olav Kristensen
Subject: Re: Distance to line segment?
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

From: Tor Olav Kristensen
Subject: Re: Distance to line segment?
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

From: Rune
Subject: Re: Distance to line segment?
Date: 23 Dec 2000 10:31:16
Message: <3a44c544@news.povray.org>
Thank you! The macros work fine, except the one with the line

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

...but I'll just use one of the others.

Rune
--
\ Include files, tutorials, 3D images, raytracing jokes,
/ The POV Desktop Theme, and The POV-Ray Logo Contest can
\ all be found at http://rsj.mobilixnet.dk (updated December 17)
/ Also visit http://www.povrayusers.org


Post a reply to this message

From: Ron Parker
Subject: Re: Distance to line segment?
Date: 23 Dec 2000 23:33:14
Message: <slrn94av4e.53d.ron.parker@fwi.com>
On Fri, 22 Dec 2000 23:53:52 +0100, Tor Olav Kristensen wrote:
>
>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 ?

Nope.  That's what I get for posting without testing.  The scalar should be
multiplied by the unit vector in the direction of B-A, or by (B-A)/vlength(B-A).


-- 
Ron Parker   http://www2.fwi.com/~parkerr/traces.html
My opinions.  Mine.  Not anyone else's.


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Distance to line segment?
Date: 24 Dec 2000 09:41:09
Message: <3A460805.A5B814EE@online.no>
Rune wrote:
> 
> Thank you! The macros work fine, except the one with the line
> 
> vlength(pP - pA + max(0, min(ss, 1))*vL)

That + sign should have been a - sign.

Sorry.

I guess I didn't test it thoroughly enough.


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


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Distance to line segment?
Date: 24 Dec 2000 09:43:34
Message: <3A460897.582507F8@online.no>
Ron Parker wrote:
> 
> On Fri, 22 Dec 2000 23:53:52 +0100, Tor Olav Kristensen wrote:
> >
> >
> >Are you that this is the right thing
> >to do here ?
> 
> Nope.  That's what I get for posting without testing.  

:|


> The scalar should be multiplied by the unit vector in 
> the direction of B-A, or by (B-A)/vlength(B-A).

Yes, that's what I found too.


Best 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.