-
-
Save innomatrix/f46ebe8f698291f1d1a2edc73b539376 to your computer and use it in GitHub Desktop.
determine the area of a polygon. using shoelace formula algorithm.
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
Math.area = Math.area || function(polygon){ | |
const length = polygon.length; | |
let sum = 0; | |
for(let i = 0; i < length; i += 2){ | |
sum += polygon[i ] * polygon[(i + 3) % length] | |
- polygon[i + 1] * polygon[(i + 2) % length]; | |
} | |
return Math.abs(sum) * 0.5; | |
} |
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
Math.area([ | |
0, 0, | |
3, 0, | |
0, 4 | |
]); // 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment