Skip to content

Instantly share code, notes, and snippets.

@AntiHero
Last active March 10, 2022 12:44
Show Gist options
  • Save AntiHero/2ba0e13e73b96f6ea24bd457892ad40c to your computer and use it in GitHub Desktop.
Save AntiHero/2ba0e13e73b96f6ea24bd457892ad40c to your computer and use it in GitHub Desktop.
const polygonArea = (ps) => {
const v = ps.reduce(
(acc, p, i) => (acc += p.x * ps.at(i - 1).y - ps.at(i - 1).x * p.y),
0
);
return Math.abs(v) / 2;
};
// or
const polygonArea = (ps) =>
ps.reduce(
(acc, p, i) => (acc += Math.abs(p.x * ps.at(i - 1).y - ps.at(i - 1).x * p.y)),
0
) / 2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment