RAY CASTING shoot a ray, count the crossings — odd means inside
Is a point inside a polygon? Shoot a ray from it in any direction and count how many edges it crosses: odd means inside, even means outside. The intuition is a wall — each crossing flips you between inside and out, so an odd count leaves you within. It handles any simple polygon, convex or not, with a handful of comparisons per edge, and the only care is the tie-handling at vertices (a half-open crossing rule). For a unit square, (2,2) crosses one edge (odd → inside) and (6,2) crosses none (even → outside).
THE TECHNIQUE count edge crossings of a ray — odd = inside, even = outside
The demo casts a rightward ray from two points against a square — (2,2) crosses once (inside), (6,2) crosses zero (outside): live demo
HISTORY & CREDIT point-in-polygon · Shimrat 1962
“Inside-or-out needs the whole shape at once.” — one ray and a crossing count decides it, edge by edge. cited
the ray · a half-line from the point; count edges it crosses. the parity · odd → inside, even → outside — each crossing flips in/out. 1962 · Shimrat — the point-in-polygon test; the even-odd rule of every fill routine.
A ray fired through the boundary, crossings tallied — parity decides inside or out. recognizer
RECOMMEND FOR I-13 the crossing parity, on the compiler
On the canonical compiler, from (2,2) the ray crosses the square once (odd, inside); from (6,2), zero times (even, outside):
$ i13 run cg_raycasting.i13 # edge-crossing parity
RUN OK · 672 step(s) · peak stack 16 · call depth 5
inside = 1 -- (2,2): odd crossings
outside = 0 -- (6,2): even crossings
test = 1
Recommend as a NULL — a recognizer. Ray casting tests membership (inside/outside) — a validity predicate (B41), the exact gate that a keeper must GENERATE past rather than CHECK. Its answer is forall-pinned by the polygon (B39). NULL — the even-odd rule under every polygon fill.