Created
December 25, 2022 08:27
-
-
Save agusmade/a427ffa68c39a6879ee6290e355d9d86 to your computer and use it in GitHub Desktop.
Calculating Polygon Area
This file contains hidden or 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 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