|
|
|
|
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Hello,
In one of my macros, I'm finding the slope of a 2D line, and sometimes I
get that warning about dividing by zero. Since it seems that diving any
number by zero results in an infinite number, would something like this
be a good fix?
#if(A.x-B.x != 0)
#local m1 = (A.y-B.y)/(A.x-B.x);
#else
#local m1 = 1e10;
#end
It seems to get rid of the warnings, but I wonder if there's a better way.
This poor hack was (understandably) giving me artifacts:
(A.x-B.x+0.000001)
I guess the other option is to jitter my points slightly, but that would
introduce problems into the whole setup, and total infallibility would
still not be ensured.
Sam
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Le 19/07/2010 19:44, stbenge nous fit lire :
> Hello,
>
> In one of my macros, I'm finding the slope of a 2D line, and sometimes I
> get that warning about dividing by zero. Since it seems that diving any
> number by zero results in an infinite number, would something like this
> be a good fix?
>
> #if(A.x-B.x != 0)
> #local m1 = (A.y-B.y)/(A.x-B.x);
> #else
> #local m1 = 1e10;
> #end
>
> It seems to get rid of the warnings, but I wonder if there's a better way.
>
> This poor hack was (understandably) giving me artifacts:
>
> (A.x-B.x+0.000001)
>
> I guess the other option is to jitter my points slightly, but that would
> introduce problems into the whole setup, and total infallibility would
> still not be ensured.
Why do you need the sloperate of your 2D line ?
why not computing the angle of the slope instead, using atan2 ?
With the angle instead of the slope, no more issue with vertical line.
(atan2() result would be in radian, just in case you would be wondering)
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Le_Forgeron wrote:
> Why do you need the sloperate of your 2D line ?
> why not computing the angle of the slope instead, using atan2 ?
> With the angle instead of the slope, no more issue with vertical line.
>
> (atan2() result would be in radian, just in case you would be wondering)
It's for a 2D line intersection macro, and the way I learned how to
perform that makes use of classic Coordinate Geometry.
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
stbenge wrote:
> (A.x-B.x+0.000001)
That fails when A.x is a tiny bit larger than B.x :-)
--
Darren New, San Diego CA, USA (PST)
C# - a language whose greatest drawback
is that its best implementation comes
from a company that doesn't hate Microsoft.
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
You should use 2D vectors rather than slopes. That way "infinite slope" is just
the vector <0,1> which can be used in calculations without problems.
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Darren New wrote:
> stbenge wrote:
>> (A.x-B.x+0.000001)
>
> That fails when A.x is a tiny bit larger than B.x :-)
Yeah, it was a horrible fix. I thought I might run into problems because
of it. Setting the slope to 1e10 seems to be working just fine, so
unless I discover more inconsistencies, I'll keep it that way for now.
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Roman Reiner wrote:
> You should use 2D vectors rather than slopes. That way "infinite slope" is just
> the vector <0,1> which can be used in calculations without problems.
It's probably easier than I think, but I was rushing to get to the good
part and just plugged in a formula:
#macro LineIntersect2D(A,B,C,D)
#local m1 = (A.y-B.y)/(A.x-B.x);
#local m2 = (C.y-D.y)/(C.x-D.x);
#local b1 = A.y - m1*A.x;
#local b2 = C.y - m2*C.x;
#local slope_intercept_x = (b2-b1)/(m1-m2);
#local slope_intercept_y = m1*slope_intercept_x + b1;
<slope_intercept_x, slope_intercept_y>
#end
That's minus the #if statements I put in there as a patch. I'm sure
there's a much simpler solution, but my momentum has been sending me
toward farther goals, so I haven't worked it out yet.
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
There you go. Not really simpler but it wont give you divide by zero errors.
#macro LineIntersect2D(A,B,C,D)
#local det = (D.y-C.y)*(B.x-A.x)-(D.x-C.x)*(B.y-A.y);
#local c = (D.x-C.x)*(A.y-C.y)-(D.y-C.y)*(A.x-C.x);
#if(det != 0)
#local slope_intercept_x = A.x+c/det*(B.x-A.x);
#local slope_intercept_y = A.y+c/det*(B.y-A.y);
#else
#if(c != 0)
//handle parallel case
#else
//handle coincident case
#end
#end
<slope_intercept_x, slope_intercept_y>
#end
stbenge <myu### [at] hotmailcom> wrote:
> It's probably easier than I think, but I was rushing to get to the good
> part and just plugged in a formula:
>
> #macro LineIntersect2D(A,B,C,D)
> #local m1 = (A.y-B.y)/(A.x-B.x);
> #local m2 = (C.y-D.y)/(C.x-D.x);
> #local b1 = A.y - m1*A.x;
> #local b2 = C.y - m2*C.x;
> #local slope_intercept_x = (b2-b1)/(m1-m2);
> #local slope_intercept_y = m1*slope_intercept_x + b1;
> <slope_intercept_x, slope_intercept_y>
> #end
>
> That's minus the #if statements I put in there as a patch. I'm sure
> there's a much simpler solution, but my momentum has been sending me
> toward farther goals, so I haven't worked it out yet.
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
Roman Reiner wrote:
> There you go. Not really simpler but it wont give you divide by zero errors.
>
> #macro LineIntersect2D(A,B,C,D)
> #local det = (D.y-C.y)*(B.x-A.x)-(D.x-C.x)*(B.y-A.y);
> #local c = (D.x-C.x)*(A.y-C.y)-(D.y-C.y)*(A.x-C.x);
> #if(det != 0)
> #local slope_intercept_x = A.x+c/det*(B.x-A.x);
> #local slope_intercept_y = A.y+c/det*(B.y-A.y);
> #else
> #if(c != 0)
> //handle parallel case
> #else
> //handle coincident case
> #end
> #end
> <slope_intercept_x, slope_intercept_y>
> #end
Thanks! I'll test it out. For the parallel and coincident cases, I
suppose just returning <0,0> would suffice?
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |
| |
|
|
stbenge <myu### [at] hotmailcom> wrote:
> Roman Reiner wrote:
> > There you go. Not really simpler but it wont give you divide by zero errors.
> >
> > #macro LineIntersect2D(A,B,C,D)
> > #local det = (D.y-C.y)*(B.x-A.x)-(D.x-C.x)*(B.y-A.y);
> > #local c = (D.x-C.x)*(A.y-C.y)-(D.y-C.y)*(A.x-C.x);
> > #if(det != 0)
> > #local slope_intercept_x = A.x+c/det*(B.x-A.x);
> > #local slope_intercept_y = A.y+c/det*(B.y-A.y);
> > #else
> > #if(c != 0)
> > //handle parallel case
> > #else
> > //handle coincident case
> > #end
> > #end
> > <slope_intercept_x, slope_intercept_y>
> > #end
>
> Thanks! I'll test it out. For the parallel and coincident cases, I
> suppose just returning <0,0> would suffice?
Depends on what you want to use the macro for. In the coincident case every
point is a intersection point so you could for example return <A.x,A.y>. In the
parallel case there is no intersection point at all. Returning <0,0> in that
case isn't a very good idea though, as an intersection could actually happen at
<0,0>.
You could return <slope_intercept_x, slope_intercept_y,0> in case of an
intersection and <0,0,1> in the parallel case and then check against the third
return value.
Post a reply to this message
|
|
| |
| |
|
|
|
|
| |