POV-Ray : Newsgroups : povray.advanced-users : Divide by zero Server Time
23 Nov 2024 22:33:52 EST (-0500)
  Divide by zero (Message 1 to 10 of 10)  
From: stbenge
Subject: Divide by zero
Date: 19 Jul 2010 13:44:35
Message: <4c448f03@news.povray.org>
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

From: Le Forgeron
Subject: Re: Divide by zero
Date: 19 Jul 2010 14:04:01
Message: <4c449391$1@news.povray.org>
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

From: stbenge
Subject: Re: Divide by zero
Date: 19 Jul 2010 14:19:13
Message: <4c449721@news.povray.org>
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

From: Darren New
Subject: Re: Divide by zero
Date: 19 Jul 2010 16:51:14
Message: <4c44bac2@news.povray.org>
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

From: Roman Reiner
Subject: Re: Divide by zero
Date: 19 Jul 2010 17:55:01
Message: <web.4c44c8d58188d4686d1d5f70@news.povray.org>
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

From: stbenge
Subject: Re: Divide by zero
Date: 20 Jul 2010 00:24:37
Message: <4c452505@news.povray.org>
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

From: stbenge
Subject: Re: Divide by zero
Date: 20 Jul 2010 00:36:23
Message: <4c4527c7@news.povray.org>
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

From: Roman Reiner
Subject: Re: Divide by zero
Date: 20 Jul 2010 03:30:01
Message: <web.4c4550108188d4686d1d5f70@news.povray.org>
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

From: stbenge
Subject: Re: Divide by zero
Date: 20 Jul 2010 17:22:09
Message: <4c461381@news.povray.org>
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

From: Roman Reiner
Subject: Re: Divide by zero
Date: 21 Jul 2010 02:15:01
Message: <web.4c468fb78188d4681095fea60@news.povray.org>
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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.