Skip to content

Instantly share code, notes, and snippets.

@cyberkm
Created December 15, 2018 09:14
Show Gist options
  • Save cyberkm/a7f881b126dc852c41ce0ff4b96149bb to your computer and use it in GitHub Desktop.
Save cyberkm/a7f881b126dc852c41ce0ff4b96149bb to your computer and use it in GitHub Desktop.
Compute area of polygon
Source: https://www.johndcook.com/blog/2018/09/26/polygon-area/
Shoelace formula:
x1 y2 + x2 y3 + … xn y1 – y1 x2 – y2 x3 – … – yn x1
Python implementation
def area(x, y):
n = len(x)
s = 0.0
for i in range(-1, n-1):
s += x[i]*y[i+1] - y[i]*x[i+1]
return 0.5*s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment