POV-Ray : Newsgroups : povray.advanced-users : Divide by zero : Re: Divide by zero Server Time
28 Sep 2024 18:56:23 EDT (-0400)
  Re: Divide by zero  
From: Roman Reiner
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.