Skip to content

Instantly share code, notes, and snippets.

@jcblw
Created February 3, 2013 03:12
Show Gist options
  • Save jcblw/4700408 to your computer and use it in GitHub Desktop.
Save jcblw/4700408 to your computer and use it in GitHub Desktop.
Get the area of a polygon
var points = [
[33.950649172363335,-117.40098863840103],
[33.95048007651844,-117.40097254514694],
[33.95035992926655,-117.40098059177399],
[33.950355479365065,-117.4010556936264],
[33.95048675136076,-117.40105032920837],
[33.95066363449239,-117.40104496479034]];
var area = function(points){
var length = points.length;
var arr = [];
for(var i = 0; i < length - 1; i += 1){
var one = points[i][0] * points[i + 1][1];
var two = points[i][1] * points[i + 1][0];
arr.push(one - two);
}
var result = 0;
for(i = 0; i < arr.length; i += 1){
result += arr[i];
}
return (result < 0) ? -1 * result / 2 : result / 2;
}
console.log(area(points));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment