Skip to content

Blog Post

Area Calculation of a Simple Polygon using Shoelace Formula

A convenient method to calculate the area of a simple polygon (one that does not intersect itself).

Algorithms

The Shoelace formula (also called Gauss's area formula or the surveyor's formula) is a convenient method to calculate the area of a simple polygon (one that does not intersect itself) when you know the coordinates of its vertices. It needs nothing more than multiplications and additions, works for any number of vertices, convex or not, and is exact for the given coordinates.

The formula

If a polygon has $n$ vertices $(x_1, y_1), (x_2, y_2), \dots, (x_n, y_n)$, listed in order (clockwise or counterclockwise), then the area $A$ is:

$$ A = \frac{1}{2} \left| \sum_{i=1}^{n} (x_i y_{i+1} - x_{i+1} y_i) \right| $$

where $(x_{n+1}, y_{n+1}) = (x_1, y_1)$ - that is, the first vertex is repeated at the end to "close the loop".

The key idea. Each term $\tfrac12(x_i y_{i+1} - x_{i+1} y_i)$ is the signed area of the triangle formed by the origin and one edge of the polygon. Walking once around the boundary adds these triangles up; the parts that fall outside the polygon cancel, and what remains is exactly the polygon's area.

Why it is called "shoelace"

Write the vertices in a column, repeat the first one at the bottom, and multiply diagonally. The crossing diagonals look like a laced shoe:

$$ A = \frac{1}{2} \left| \underbrace{(x_1 y_2 + x_2 y_3 + \dots + x_n y_1)}_{\text{down-right diagonals}} - \underbrace{(y_1 x_2 + y_2 x_3 + \dots + y_n x_1)}_{\text{down-left diagonals}} \right| $$

Where it comes from

For two consecutive vertices $P_i$ and $P_{i+1}$, the determinant

$$ \frac{1}{2}\begin{vmatrix} x_i & x_{i+1} \\ y_i & y_{i+1} \end{vmatrix} = \frac{1}{2}(x_i y_{i+1} - x_{i+1} y_i) $$

is the signed area of the triangle $O\,P_i\,P_{i+1}$: positive when $P_{i+1}$ lies counterclockwise from $P_i$ as seen from the origin, negative otherwise. Summing around the whole boundary, triangles are counted positively where the edge moves counterclockwise around the origin and negatively where it moves back, so everything outside the polygon is added and removed again. The same result follows from Green's theorem, $A = \tfrac12\oint (x\,dy - y\,dx)$, applied to a polygonal boundary.

The sum is cyclic, so it does not matter which vertex you start from; and moving the origin changes the individual triangles but not their total.

A worked example

Take the five vertices used in the script below, $P_1(1,6)$, $P_2(3,1)$, $P_3(7,2)$, $P_4(2,4)$, $P_5(8,5)$:

$i$$P_i$$P_{i+1}$$x_i y_{i+1} - x_{i+1} y_i$Value
1$(1, 6)$$(3, 1)$$1\cdot1 - 3\cdot6$$-17$
2$(3, 1)$$(7, 2)$$3\cdot2 - 7\cdot1$$-1$
3$(7, 2)$$(2, 4)$$7\cdot4 - 2\cdot2$$24$
4$(2, 4)$$(8, 5)$$2\cdot5 - 8\cdot4$$-22$
5$(8, 5)$$(1, 6)$$8\cdot6 - 1\cdot5$$43$
Sum$27$
$$A = \frac{1}{2}\,|27| = 13.5$$
x y 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 P1 (1,6) P2 (3,1) P3 (7,2) P4 (2,4) P5 (8,5)
Polygon 1: area $13.5$. The vertex $P_4$ points inward, so the polygon is not convex.
x y 0 2 4 6 8 10 12 1 3 5 7 9 11 P1 (4,10) P2 (9,7) P3 (11,2) P4 (2,2)
Polygon 2: area $45.5$. All turns go the same way, so it is convex.

Orientation and the sign of the sum

The absolute value in the formula makes the result independent of the direction in which you list the vertices. Without it, the sum is the signed area:

  • positive when the vertices run counterclockwise (Polygon 1: $+13.5$),
  • negative when they run clockwise (Polygon 2: $-45.5$).

This is a handy cheap test for the orientation of a ring, for example before passing a boundary to a library that expects a certain winding order.

Implementation in Python

The following Python script defines a class that computes the area of a polygon using the Shoelace formula and checks whether the polygon is convex.

class Polygon:
    def __init__(self, vertices):
        self.vertices = vertices

    def get_area(self):
        # Initializations
        area = 0.0
        n = len(self.vertices)
        j = n - 1

        for i in range(0, n):
            area += (self.vertices[j][0] + self.vertices[i][0]) * \
                    (self.vertices[j][1] - self.vertices[i][1])
            j = i   # set j to the previous vertex

        return abs(0.5 * area)

    def is_convex(self):
        # Initializations
        N = len(self.vertices)  # Stores count of edges in polygon
        prev = 0                # Stores direction of cross product of previous traversed edges
        cur = 0                 # Stores direction of cross product of current traversed edges
     
        for i in range(N):
            # Stores three adjacent edges of the polygon
            temp = [self.vertices[i], self.vertices[(i + 1) % N], self.vertices[(i + 2) % N]]
            cur = self.cross_product(temp)
     
            if (cur != 0):
                # If direction of cross product of all adjacent edges are not same
                if (cur * prev < 0):
                    return False
                else:
                    prev = cur
     
        return True

    def cross_product(self, a):
         
        x1 = (a[1][0] - a[0][0])    # Stores abscissas of vector a[1] ---> a[0]
        y1 = (a[1][1] - a[0][1])    # Stores ordinates of vector a[1] ---> a[0]
        x2 = (a[2][0] - a[0][0])    # Stores abscissas of vector a[2] ---> a[0]
        y2 = (a[2][1] - a[0][1])    # Stores ordinates of vector a[2] ---> a[0]
     
        # Return cross product
        return (x1 * y2 - y1 * x2)


if __name__ == "__main__":

    vertices1 = [[1, 6], [3, 1], [7, 2], [2, 4], [8, 5]]
    polygon1 = Polygon(vertices1)
    print(polygon1.get_area())
    print(polygon1.is_convex())

    vertices2 = [[4, 10], [9, 7], [11, 2], [2, 2]]
    polygon2 = Polygon(vertices2)
    print(polygon2.get_area())
    print(polygon2.is_convex())

Running it prints:

13.5
False
45.5
True

How the code works

  • get_area uses the equivalent trapezoid form of the formula, $\tfrac12\sum (x_j + x_i)(y_j - y_i)$ with $j$ the previous vertex (starting with $j = n-1$, which closes the loop without repeating the first vertex). Expanding the products shows that the extra $x_j y_j$ and $x_i y_i$ terms cancel around the cycle, leaving exactly $\sum (x_i y_j - x_j y_i)$, the same sum as above with the opposite sign.
  • is_convex takes every three consecutive vertices and computes the cross product of the two edges. A polygon is convex when none of these cross products changes sign; collinear triples (cross product $0$) are ignored.
  • cross_product returns $x_1 y_2 - y_1 x_2$ for the vectors from the first point to the other two.

Both methods visit each vertex once, so the time complexity is $O(n)$ and the extra memory is $O(1)$.

Practical notes

Large coordinates. With projected map coordinates (for example easting and northing in the millions of metres) the products $x_i y_{i+1}$ become huge and nearly equal, so floating-point rounding can eat many digits of the result. Subtract the first vertex (or the centre of the bounding box) from every point before summing: the area is unchanged and the numbers stay small.
  • Simple polygons only. If edges cross each other (a bow-tie), the formula returns a net signed area in which the lobes partly cancel, not the sum of the areas of the lobes.
  • Order matters. The vertices must follow the boundary. A shuffled list describes a different (usually self-intersecting) polygon.
  • Polygons with holes. Compute the outer ring and each hole separately and subtract the hole areas, or list the holes with the opposite orientation and add the signed areas.
  • Planar coordinates. The formula assumes a flat plane. For latitude and longitude over large regions, project the points first (or use a geodesic area method).
  • Closing vertex. Do not list the first vertex twice; the formula already wraps around. A duplicated last point only adds a zero term, so it is harmless but wasted work.

Bonus: the centroid

The same cross terms give the centroid $(C_x, C_y)$ of the polygon. With the signed area $A = \tfrac12\sum(x_i y_{i+1} - x_{i+1} y_i)$:

$$ C_x = \frac{1}{6A} \sum_{i=1}^{n} (x_i + x_{i+1})(x_i y_{i+1} - x_{i+1} y_i), \qquad C_y = \frac{1}{6A} \sum_{i=1}^{n} (y_i + y_{i+1})(x_i y_{i+1} - x_{i+1} y_i) $$

For Polygon 1 this gives $C_x = 305/81 \approx 3.77$ and $C_y = 297/81 \approx 3.67$. Use the signed area here (no absolute value) so that the formula works for either orientation.

Summary

  • List the vertices in boundary order, close the loop, and sum $x_i y_{i+1} - x_{i+1} y_i$.
  • The area is half the absolute value of that sum; its sign tells you the orientation.
  • It runs in $O(n)$, works for concave polygons, and extends to centroids and polygons with holes.
  • Shift the coordinates to a local origin when working with large projected values.
Share LinkedIn

Keep reading

Related articles

Algorithms

Point-in-Polygon: Ray Casting vs Winding Number

How do you decide whether a point lies inside a polygon? Two classic algorithms, ray casting (even–odd) and the winding number, compared: how they work, where they differ, and a short Python implementation.

Read article

Searching...