On this page
A map app must decide which district a dot falls in; a drawing program must decide which pixels to colour. Both ask: is this point inside this polygon? The classic answer works even on dented shapes.
How does the ray-casting test work?
Draw a ray from the point, say horizontally to the right, to infinity, and count the polygon edges it crosses. Each crossing flips you between inside and outside, and far away you are certainly outside. So an odd count means inside; an even count means outside.
One early published version is "Algorithm 112: Position of point relative to polygon" by M. Shimrat in Communications of the ACM in 1962 (Crossref record). W. Randolph Franklin, whose short C function PNPOLY is the version in the comp.graphics.algorithms FAQ, describes the method plainly: run a horizontal ray out from the point, count the edges it crosses, and "at each crossing, the ray switches between inside and outside" (PNPOLY notes). The guarantee behind it is the Jordan curve theorem, which we cover in Jordan curve theorem: inside, outside and the ray test.
A worked example on a non-convex polygon
Take a U shape with vertices, in order, (0,0), (8,0), (8,6), (5,6), (5,2), (3,2), (3,6), (0,6). It has a notch cut into the top between x = 3 and x = 5, so it is not convex, and the shortcut "inside if it is on the inner side of every edge" fails.
Point A = (4,4) sits in the notch. The ray y = 4 going right meets the vertical edges at x = 5 and x = 8. Two crossings, even, so A is outside.
Point B = (1,3) sits in the left arm. The ray y = 3 going right meets the edges at x = 3, x = 5 and x = 8. Three crossings, odd, so B is inside.
What happens when the ray hits a vertex?
Now try C = (1,2). Its ray runs along the bottom of the notch: it touches the vertex (3,2), slides along the edge to (5,2), then crosses x = 8. Count every edge the ray touches and you get four (x = 3, the flat edge, x = 5, x = 8), which says outside. But C is inside.
The standard fix is a half-open rule: count an edge only if one endpoint is strictly above the ray and the other is not. A vertex lying exactly on the ray is then treated as if it were just below it. Under that rule the flat edge never counts, the edges at x = 3, x = 5 and x = 8 each count once, and C gets three crossings: inside. Franklin's notes say the vertex case is "handled correctly via a careful selection of inequalities" and that the code pretends to shift the ray an infinitesimal amount so it "either clearly intersects, or clearly doesn't touch" (PNPOLY notes).
Map software makes the same choice explicit. In PostGIS, polygons "do not contain lines and points lying fully in their boundary", and a separate function, ST_Covers, gives the more inclusive answer (PostGIS ST_Contains).
What is the winding-number test, and when does it differ?
The winding number counts how many times the boundary wraps around the point. In ray form, the SVG 2 specification says: add one each time a path segment crosses the ray from left to right and subtract one each time it crosses from right to left; if the total is zero, the point is outside (W3C SVG 2, fill-rule). SVG calls this rule nonzero and the parity test evenodd, and nonzero is the default.
For a simple polygon like our U, both tests agree: B has winding number ±1, A has 0. They differ when the boundary crosses itself. In a five-pointed star drawn in one stroke, a ray from the centre crosses two edges, both in the same direction: the parity count is even but the winding number is ±2. So evenodd leaves a hole in the middle while nonzero fills it (Point in polygon, Wikipedia).
If the idea of a curve winding around a point is new to you, Argument principle: how winding numbers count zeros shows the same count doing serious work in complex analysis.
Why does this matter outside a maths class?
Wikipedia lists computer graphics, computer vision, geographic information systems, motion planning and computer-aided design among the fields where this problem comes up (Point in polygon).
A checklist for coding it yourself:
Loop over edges as vertex pairs, including the closing edge.
Count an edge only if exactly one endpoint is strictly above the ray.
Compare your x with the edge's crossing x.
Decide what a boundary point should return, and test it.
Use the winding number if your shapes can cross themselves.
If the Jordan curve theorem or the orientation behind a crossing's sign still feels shaky, the Fermi Concept Library covers every concept in mathematics and computing. To work more cases, the Fermi Problem Library has a step-by-step solver that explains the reasoning.



