The Steps of the Algorithm. To ease the presenta-
tion of the steps of the algorithm, it defines its own co-
ordinate system with (0|0) = Q by moving the poly-
gon. This translation is only for illustration purposes.
The algorithm then uses the positive x-axis as ray to
calculate intersections with the edges of P. So the
start vertex of the ray is (0|0) and the end vertex is
(x
max
|0) where x
max
is an x value greater than that of
any of the vertices of P.
1. First it is determined if Q is equal to any of the
vertices of P or lies on any of the edges connecting
the vertices. If so the result is inside.
2. A vertex P
s
that does not lie on the x-axis is
searched in the set of vertices of P. If no such
vertex can be found the result is outside.
3. Set i to 1. Beginning from that vertex P
s
the fol-
lowing steps are repeated until all vertices of P
have been visited:
(a) The index s is increased to s + i until the next
vertex P
s+i
not lying on the x-axis is found. If
the index s + i > n then i is set to −s and the
search is continued.
(b) Depending on the course of step (a) one of the
following steps is taken:
i. No vertex has been skipped: the line segment
from P
s
to P
s+i
is intersected with the positive
x-axis.
ii. At least one vertex with a positive x-value has
been skipped: the line segment from P
s
to P
s+i
is intersected with the complete x-axis.
iii. At least one vertex with a negative x-value has
been skipped: nothing is done.
(c) P
s+i
is the starting vertex for the next iteration.
4. If the count of intersections with the x-axis is even
then the result is outside, if it is odd the result is
inside.
Proof of Correctness. To prove that the oven-odd
algorithm in general is correct one can generalize it
to the Jordan Curve Theorem and prove its correct-
ness (Tverberg, 1980). What has to be proven is that
the given algorithm is in fact a correct even-odd al-
gorithm, meaning that the count of edges is correct
under all circumstances.
A lot of challenges emerge when trying to inter-
sect two line segments and one of the segments has
one or two of its vertices lying on the other segment.
If the count of intersections with the x-axis are to be
counted correctly, then each edge of P must either
clearly intersect the x-axis or not intersect it at all.
There must be no case where the starting or end ver-
tex of a line segment lies on the line segment it should
be intersected with.
The x-axis is the first line segment to look at, since
it is part of all the intersections. The start vertex of
the x-axis, namely Q, is guaranteed not to lie on any
edge or to be equal to any vertex of P. If this was the
case, then the first step of the algorithm would have
already returned the correct result. The end vertex is
guaranteed to have an x value greater than any of the
vertices of P, so no vertex of P can be equal to it and
no edge of P can contain it.
Of course there still exists the challenge that one
or more vertices of P lie on the x-axis as seen in Fig-
ure 2.
Figure 2: One of the edges of P lies on the x-axis.
The algorithm deals with this kind of challenge by
ignoring the vertices lying on the x-axis and stepping
over them when trying to find an edge to intersect. It
then creates a new auxiliary edge that it can intersect
safely. So starting for example at vertex P
1
it would
ignore P
2
and P
3
and then create a new edge between
P
1
and P
4
as shown in Figure 3.
Figure 3: A new auxiliary edge has been created.
At this point it is clear that none of the edges used
to calculate the count of intersections with the x-axis
has a vertex on the x-axis and is either clearly inter-
secting it or not. What has to be shown is that all
created auxiliary edges are correct substitutes to cal-
culate the intersections with the x-axis.
Indeed they are not a correct substitute to intersect
the positive x-axis, as can be seen in Figure 4. Here
GRAPP 2017 - International Conference on Computer Graphics Theory and Applications
176