Last active
April 1, 2019 09:45
-
-
Save uinz/9d466d464881fba88128164dd0a85b0c to your computer and use it in GitHub Desktop.
calculate polygon area
This file contains hidden or 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
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