Last active
July 12, 2021 15:08
-
-
Save tnraro/f6c0bf3daa711721d3ce0dea1c37124a 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