Skip to content

Instantly share code, notes, and snippets.

@agusmade
Created December 25, 2022 08:27
Show Gist options
  • Select an option

  • Save agusmade/a427ffa68c39a6879ee6290e355d9d86 to your computer and use it in GitHub Desktop.

Select an option

Save agusmade/a427ffa68c39a6879ee6290e355d9d86 to your computer and use it in GitHub Desktop.
Calculating Polygon Area
function polygonArea(X, Y) {
var area = 0; // Accumulates area in the loop
var numPoints = X.length;
var j = numPoints-1; // The last vertex is the 'previous' one to the first
for (var i=0; i<numPoints; i++) {
area = area + (X[j]+X[i]) * (Y[j]-Y[i]);
j = i; //j is previous vertex to i
}
return area/2;
}
var xPts = [3, 3, 2, 2];
var yPts = [2, 4, 4, 5];
var a = polygonArea(xPts, yPts);
console.log("Area = " + a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment