Skip to content

Instantly share code, notes, and snippets.

@innomatrix
Forked from tnraro/area-polyfill.js
Created December 6, 2020 22:09
Show Gist options
  • Save innomatrix/f46ebe8f698291f1d1a2edc73b539376 to your computer and use it in GitHub Desktop.
Save innomatrix/f46ebe8f698291f1d1a2edc73b539376 to your computer and use it in GitHub Desktop.
determine the area of a polygon. using shoelace formula algorithm.
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;
}
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