Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!hplabs!hpfcso!hpfelg!koren
From: koren@hpfelg.HP.COM (Steve Koren)
Newsgroups: comp.graphics
Subject: Re: Re: Intersection between a line and a polygon (UNDECIDABLE??)
Message-ID: <1140024@hpfelg.HP.COM>
Date: 29 Sep 89 18:56:32 GMT
References: <32610@cornell.UUCP>
Organization: HP Elec. Design Div. -FtCollins
Lines: 40

> >
> >  I need to find a formula/algorithm to determine if a line intersects
> >  a polygon.  I would perfer a method that would do this in as litte
> >  time as possible.  I need this for use in a forward raytracing
> >  program.

> I think that this is a very difficult problem. To start with,
> lines and polygons are semi-algebraic sets which both contain

If you are writing this for a ray tracer, I assume you really mean "line"
or "half-line" and not "line-segment".  In that case, can't you just check
the intersection of the line and each of the edges of the polygon?  If it
hits any edge, it passes through the polygon.  This is sort of similar to
an algorithm that determines if a point is inside a polygon.  You draw a 
line from the point to infinity, and count the number of edges of the polygon
the line crosses.  If it crosses an even number, you're outside.  If it
crosses an odd number, you're inside.  This even takes care of polygons
with "holes" in them.

There's some code that does basically this in QRT, you can pull it out
and use it if you wish.  Its in a file called "pattern.c", if you have
the QRT source.  Its a little hacked out, but its only a few lines.

Hope this helps.

         - steve (koren@hpfela.HP.COM)

PS - oh by the way, there's a fairly simply extention to this if you
want to use line-segments instead of lines or half-lines.  Proceed as above:
if you hit a polygon-side you hit the polygon.  If you dont hit a side,
see if both points are inside or outside the polygon (easy to do; see the
code I pointed out above).  That will tell you if the line is competely
inside or outside the polygon.  If one point is inside and one is outside
but you didn't find an interection with the polygon side, somethings
messed up in the code.

PPS - if the line doesn't lie in the same plane as the polygon, the problem
is even simpler.  Just find the intersection point of the line and the
plain of the polygon.  If this point is inside the polygon (see the test
above), your line hit the polygon somewhere.