Is point inside the polygon

The general problem we'd like to solve is, given a point $(x, y)$ and a polygon $P$ (represented by its sequence of vertices), is $(x, y)$ in $P$, on the boundary, or outside?

Fortunately the problem has a simple and elegant answer. Just draw a ray (portion of a line extending infinitely in one direction) down (or in any other direction) from $(x, y)$, count crossings on ray.

Every time the polygon crosses this ray, it separates a part of the ray that's inside the polygon from a part that's outside. For this, all we really need to know is whether there's an even or odd number of them. In even crossings, the point is outside the polygon.


\begin{algorithm}
% latex2html id marker 209
[h]
\caption{: function is-point-in...
...\ELSE
\STATE return (outside);
\ENDIF
\ENDFOR
\end{algorithmic}\end{algorithm}