Homeβ€Ί DSAβ€Ί Polygon Area β€” Shoelace Formula and Applications

Polygon Area β€” Shoelace Formula and Applications

Where developers are forged. Β· Structured learning Β· Free forever.
πŸ“ Part of: Geometry β†’ Topic 2 of 4
Learn the Shoelace formula for polygon area.
βš™οΈ Intermediate β€” basic DSA knowledge assumed
In this tutorial, you'll learn:
  • Shoelace: area = |Ξ£(xα΅’yα΅’β‚Šβ‚ - xα΅’β‚Šβ‚yα΅’)| / 2. O(n) time, O(1) space.
  • Signed area: positive = CCW, negative = CW. Essential for polygon orientation detection.
  • Works for any simple polygon (non-self-intersecting), including non-convex.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
⚑ Quick Answer
The Shoelace formula computes the area of any polygon from its vertex coordinates in O(n) β€” no integration, no triangulation. Cross-multiply adjacent vertices, sum with alternating signs, divide by 2. The signed result also reveals the winding order (clockwise or counterclockwise), which is critical for many geometry algorithms.

The Shoelace formula (also Gauss's area formula, 1795) computes polygon area in O(n) from vertex coordinates. It is named for the pattern of cross-multiplication that resembles lacing a shoe. Beyond area, the formula's signed result reveals polygon orientation β€” fundamental for polygon clipping, triangulation, and ray casting. Every GIS system, every game physics engine, and every computational geometry library implements it.

Shoelace Formula

For a polygon with vertices (x1,y1), (x2,y2), ..., (xn,yn): Area = |Ξ£α΅’ (xα΅’Γ—yα΅’β‚Šβ‚ - xα΅’β‚Šβ‚Γ—yα΅’)| / 2

The signed version (without absolute value) is positive for CCW polygons, negative for CW.

shoelace.py Β· PYTHON
123456789101112131415161718192021222324
def polygon_area(vertices: list[tuple]) -> float:
    """Signed area: positive = CCW, negative = CW."""
    n = len(vertices)
    area = 0
    for i in range(n):
        j = (i + 1) % n
        area += vertices[i][0] * vertices[j][1]
        area -= vertices[j][0] * vertices[i][1]
    return area / 2

def polygon_area_abs(vertices):
    return abs(polygon_area(vertices))

def is_counterclockwise(vertices):
    return polygon_area(vertices) > 0

# Unit square
square = [(0,0),(1,0),(1,1),(0,1)]
print(f'Area: {polygon_area_abs(square)}')  # 1.0
print(f'CCW: {is_counterclockwise(square)}') # True

# Triangle
triangle = [(0,0),(4,0),(2,3)]
print(f'Triangle area: {polygon_area_abs(triangle)}')  # 6.0
β–Ά Output
Area: 1.0
CCW: True
Triangle area: 6.0

Centroid and Applications

centroid.py Β· PYTHON
1234567891011121314
def polygon_centroid(vertices):
    """Centroid (geometric centre) of a polygon."""
    n = len(vertices)
    cx = cy = 0
    area = polygon_area(vertices)  # signed
    for i in range(n):
        j = (i + 1) % n
        cross = vertices[i][0]*vertices[j][1] - vertices[j][0]*vertices[i][1]
        cx += (vertices[i][0] + vertices[j][0]) * cross
        cy += (vertices[i][1] + vertices[j][1]) * cross
    factor = 1 / (6 * area)
    return (cx * factor, cy * factor)

print(polygon_centroid([(0,0),(4,0),(4,4),(0,4)]))  # (2.0, 2.0)
β–Ά Output
(2.0, 2.0)

🎯 Key Takeaways

  • Shoelace: area = |Ξ£(xα΅’yα΅’β‚Šβ‚ - xα΅’β‚Šβ‚yα΅’)| / 2. O(n) time, O(1) space.
  • Signed area: positive = CCW, negative = CW. Essential for polygon orientation detection.
  • Works for any simple polygon (non-self-intersecting), including non-convex.
  • Exact for integer coordinates β€” no floating point error.

Interview Questions on This Topic

  • QDerive the Shoelace formula from the trapezoidal decomposition.
  • QHow does the signed area indicate polygon winding order?
  • QWhat is the area of a polygon with vertices (0,0),(3,0),(3,4)?

Frequently Asked Questions

Does the Shoelace formula work for self-intersecting polygons?

It computes the algebraic area β€” regions traced clockwise cancel regions traced counterclockwise. For a figure-8, the two loops cancel to give area 0. This is correct for some applications (winding number calculation) but not for physical area.

πŸ”₯
Naren Founder & Author

Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.

← PreviousLine Intersection and OrientationNext β†’Point in Polygon β€” Ray Casting Algorithm
Forged with πŸ”₯ at TheCodeForge.io β€” Where Developers Are Forged