Skip to content

Instantly share code, notes, and snippets.

@uinz
Last active April 1, 2019 09:45
Show Gist options
  • Save uinz/9d466d464881fba88128164dd0a85b0c to your computer and use it in GitHub Desktop.
Save uinz/9d466d464881fba88128164dd0a85b0c to your computer and use it in GitHub Desktop.
calculate polygon area
function polygonArea(points: { x: number; y: number }[]) {
let i = 0;
let s = 0;
while (points[i + 1]) {
const p1 = points[i];
const p2 = points[i + 1];
s += p1.x * p2.y - p2.x * p1.y;
i += 1;
}
return Math.abs(s / 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment