Created
December 15, 2016 11:30
-
-
Save ningunaparte/08499682680b49d20745ca5970e91d8e to your computer and use it in GitHub Desktop.
Devuelve el área de un polígono en javascript
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
// DEVUELVE EL AREA DE TERMINADO POLIGONO | |
// http://www.mathopenref.com/coordpolygonarea2.html | |
function polygonArea(X, Y, numPoints) { | |
area = 0; // Accumulates area in the loop | |
j = numPoints - 1; // The last vertex is the 'previous' one to the first | |
for (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 = [4, 4, 8, 8, -4, -4]; | |
var yPts = [6, -4, -4, -8, -8, 6]; | |
var a = polygonArea(xPts, yPts, 6); | |
console.log("Area = " + a); | |
// http://math.stackexchange.com/questions/99565/simplest-way-to-calculate-the-intersect-area-of-two-rectangles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment