Created
October 5, 2018 10:38
Star
You must be signed in to star a gist
Returns the area of a simple polygon given in [ [x,y], ...] pairs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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