Again, label the polygon
with
, and consider a point
.
Method #1: Choose a point that you know is
outside the polygon. Find how many times the line segment
crosses the edges of the polygon. If
this count is odd, then
is inside the polygon; if it
is even, then
is outside.
Details: For the coordinate of
, you
could, say, take the max of the
coordinates of the
points
and add 1. Then
is definitely
outside. For the
coordinate of
, use a
random number between 0 and 1; that way, there is
essentially no possibility that
will go
through one of the
, an undesirable case. If
is on the boundary, you'll discover that fact as part
of looking for the crossings.
Method #2 (the winding number method): Add up the
angles subtended at by the edges of the polygon,
with appropriate signs, and divide the total by
.
The result is called the winding number (the number of
times that the polygon winds around
). If the
winding number is 1 or -1,
is inside; if it is
,
is outside.
In Method #2, for each you will need to
find the signed angle between the vector
and the vector
, using the method of Handout
C, §5, (vi). It is a good idea to check separately
that
is not equal to any of the points
,
so that the difference vectors are both nonzero. You can
check that
is not on the boundary when you are
finding the angles.