MapMath

CHAPTER 11

Polygons — area, centroid, and containment

Ray-casting point-in-polygon, planar and spherical polygon area, centroid computation, and the edge cases every implementation misses.

3 min read

Points and lines cover most location queries, but the moment you need to answer "is this delivery address inside our service zone?" or "what's the area of this selected region?" you need polygon math. This chapter covers three fundamental operations: containment testing, area calculation, and centroid finding.

Point-in-polygon (ray casting)

Cast a horizontal ray from the test point and count how many times it crosses polygon edges. Odd = inside, even = outside. Click anywhere in the diagram to test a point.

Ray casting — click to test a point

✓ INSIDE
1 crossinginside

Ray goes right. Odd crossings = inside. Even = outside.

The algorithm works for any simple (non-self-intersecting) polygon, including concave shapes. It can fail for points exactly on an edge — if that's a concern, add a small epsilon check.

function pointInPolygon(point, polygon) {
  const [px, py] = point;
  let inside = false;
  for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
    const [xi, yi] = polygon[i];
    const [xj, yj] = polygon[j];
    const intersect = ((yi > py) !== (yj > py)) &&
                      (px < (xj - xi) * (py - yi) / (yj - yi) + xi);
    if (intersect) inside = !inside;
  }
  return inside;
}

For small polygons, treating lon/lat as planar (x, y) is fine. For polygons spanning continents, project first.

Polygon area on a plane (Shoelace)

For projected coordinates:

A=12i(xiyi+1xi+1yi)A = \tfrac{1}{2} \left| \sum_{i} (x_i \cdot y_{i+1} - x_{i+1} \cdot y_i) \right|

(indices wrap around)

optional — skip if familiarrefresher

The Shoelace formula (also called the surveyor's formula) is a standard algorithm many developers encounter in computational geometry. It computes the signed area of a polygon from its vertex coordinates by summing cross products of adjacent vertex pairs. The absolute value gives the unsigned area. It runs in O(n) and is exact for planar coordinates.

Spherical polygon area

For lat/lon polygons, Shoelace is wrong. The Shoelace formula assumes a flat plane with equal-area spacing — but a 1° × 1° box at the equator is ~12,300 km², while the same angular box at 80° N is only ~210 km². Applying Shoelace to raw lat/lon coordinates produces wildly incorrect areas for any polygon that spans more than a few degrees.

Use the spherical excess formula instead:

A=R22i(λi+1λi)(2+sinφi+sinφi+1)A = \frac{R^2}{2} \left| \sum_{i} (\lambda_{i+1} - \lambda_i) \cdot (2 + \sin\varphi_i + \sin\varphi_{i+1}) \right|

This gives area in m² when R is in meters. Vertices must be in radians and ordered consistently (CCW for "outside").

Chapter 11 · Paid content

Continue reading "Polygons — area, centroid, and containment"

You've reached the end of the free preview. Unlock all 22 paid chapters, including distance math, bearings, polygons, spatial indexing, and 3D map rendering — plus a downloadable PDF and the companion code repo.

  • All 22 paid chapters with worked examples
  • Downloadable PDF for offline reading
  • Companion GitHub repo (JavaScript + Python)
  • Free updates for life

Multiple payment options including Wise, PayPal, and bank transfer.