Skip to content

Instantly share code, notes, and snippets.

@gka gka/polygonArea.js
Created Oct 5, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.