|
|
|
|
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
"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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
|
|