Point in Polygon — Definition, Formula & Example
Point in polygon is the problem of deciding whether a given point lies inside, outside, or on the boundary of a polygon. The most common method draws a ray from the point and counts how many times it crosses the polygon's edges.
Given a simple polygon with vertices and a query point not on the boundary, the crossing-number (ray casting) test counts the edges of that intersect the ray . By the Jordan curve theorem, is in the interior of if and only if this count is odd. A half-open rule for edge endpoints makes the count well defined when the ray passes through a vertex.
Key Formula
Where:
- = The query point being tested
- = The two endpoints of one polygon edge
- = The x-coordinate where the edge meets the horizontal line
Worked Example
Problem: A fenced yard has corners at (0, 0), (8, 0), (8, 6), (4, 2), and (0, 6), measured in feet. The corner at (4, 2) dents inward, so the yard is concave. Is a sprinkler at (4, 4) inside the yard? Use a ray pointing right from the sprinkler.
Step 1: Find the edges that straddle the line y = 4, meaning one endpoint is above it and one is not. The bottom edge from (0, 0) to (8, 0) does not straddle it. The other four edges do: (8, 0)–(8, 6), (8, 6)–(4, 2), (4, 2)–(0, 6), and (0, 6)–(0, 0).
Step 2: Find where each straddling edge meets y = 4. The two vertical edges meet it at x = 8 and x = 0. Use the crossing formula for the two slanted edges.
Step 3: Count the crossings to the right of the sprinkler, where x > 4. Only x = 8 and x = 6 qualify, so the ray crosses the boundary 2 times. An even count means the point is outside.
Answer: The ray crosses the boundary 2 times, which is even, so the sprinkler at (4, 4) is outside the yard. It sits in the notch formed by the inward corner.
Why It Matters
Mapping software runs this test to decide which county, ZIP code, or delivery zone a GPS coordinate falls in. Graphics programs use it to tell whether a mouse click lands on a shape. It is also one of the first algorithms taught in a computational geometry course.
Common Mistakes
Mistake: Counting a crossing twice when the ray passes exactly through a vertex. Both edges that meet at that vertex get counted.
Correction: Treat each edge as half-open. Count an edge only when exactly one endpoint satisfies . Then a vertex where the boundary passes through the ray adds 1. A vertex where the boundary only touches the ray adds 0 or 2, so the parity stays correct.
