Mathwords logoMathwords

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 PP with vertices (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n) and a query point q=(x0,y0)q = (x_0, y_0) not on the boundary, the crossing-number (ray casting) test counts the edges of PP that intersect the ray {(x,y0):x>x0}\{(x, y_0) : x > x_0\}. By the Jordan curve theorem, qq is in the interior of PP 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

(yi>y0)≠(yj>y0)andx0<xi+(y0−yi)(xj−xi)yj−yi\big(y_i > y_0\big) \neq \big(y_j > y_0\big) \quad\text{and}\quad x_0 < x_i + \frac{(y_0 - y_i)(x_j - x_i)}{y_j - y_i}
Where:
  • (x0,y0)(x_0, y_0) = The query point being tested
  • (xi,yi),(xj,yj)(x_i, y_i), (x_j, y_j) = The two endpoints of one polygon edge
  • xi+(y0−yi)(xj−xi)yj−yix_i + \frac{(y_0 - y_i)(x_j - x_i)}{y_j - y_i} = The x-coordinate where the edge meets the horizontal line y=y0y = y_0

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.
x=8+(4−6)(4−8)2−6=6,x=4+(4−2)(0−4)6−2=2x = 8 + \frac{(4-6)(4-8)}{2-6} = 6, \qquad x = 4 + \frac{(4-2)(0-4)}{6-2} = 2
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.
#{8,6}=2    (even)  ⟹  outside\#\{8, 6\} = 2 \;\; (\text{even}) \implies \text{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 y>y0y > y_0. 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.

Related Terms

  • Polygon — The closed shape whose interior this test detects
  • Concave Polygon — Case where simple side checks fail but ray casting works
  • Convex Polygon — Allows a faster same-side test for every edge
  • Analytic Geometry — Supplies the coordinate line equations behind crossing points
  • Euclidean Geometry — Plane setting where the Jordan curve argument applies
  • Jordan Curve — The theorem that guarantees a simple polygon has one inside and one outside