Welcome, guest | Sign In | My Account | Store | Cart

Function for finding the area of a polygon in a 2D co-ordinate system.

Python, 8 lines
1
2
3
4
5
6
7
8
def poly_area2D(poly):
    total = 0.0
    N = len(poly)
    for i in range(N):
        v1 = poly[i]
        v2 = poly[(i+1) % N]
        total += v1[0]*v2[1] - v1[1]*v2[0]
    return abs(total/2)

This function implements Green's theorem, also known as the shoelace theorem or the surveyors' theorem, which is the 2D case of the more general Stokes' theorem.

Wikipedia link

A useful explanation of how the formula works

My Excel VBA implementation of this theorem

Created by Jamie Bull on Mon, 1 Oct 2012 (MIT)
Python recipes (4591)
Jamie Bull's recipes (5)

Required Modules

  • (none specified)

Other Information and Tasks