Skip to content

Instantly share code, notes, and snippets.

@gka
Created October 5, 2018 10:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gka/0aa03da2cc86782d2d29f9df5cd902b3 to your computer and use it in GitHub Desktop.
Returns the area of a simple polygon given in [ [x,y], ...] pairs
function area(vertices) {
var total = 0;
for (var i = 0, l = vertices.length; i < l; i++) {
var addX = vertices[i][0];
var addY = vertices[i == vertices.length - 1 ? 0 : i + 1][1];
var subX = vertices[i == vertices.length - 1 ? 0 : i + 1][0];
var subY = vertices[i][1];
total += (addX * addY * 0.5);
total -= (subX * subY * 0.5);
}
return total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment