mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-04-27 19:52:14 +00:00
24 lines
760 B
JavaScript
24 lines
760 B
JavaScript
const findConvexHull = async (pts) => {
|
|
const { sortBy, uniqBy } = await import("https://esm.sh/lodash-es@4.17.21"),
|
|
u = uniqBy(pts, p => `${p.x},${p.y}`),
|
|
s = sortBy(u, ["x", "y"]),
|
|
n = s.length,
|
|
c = (a, b, d) => (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x),
|
|
l = [],
|
|
h = [];
|
|
if (n < 3) return s;
|
|
for (let i = 0; i < n; i++) {
|
|
const p = s[i];
|
|
while (l.length > 1 && c(l[l.length - 2], l[l.length - 1], p) <= 0) l.pop();
|
|
l.push(p);
|
|
}
|
|
for (let i = n; i--; ) {
|
|
const p = s[i];
|
|
while (h.length > 1 && c(h[h.length - 2], h[h.length - 1], p) <= 0) h.pop();
|
|
h.push(p);
|
|
}
|
|
return [...l.slice(0, -1), ...h.slice(0, -1)];
|
|
};
|
|
export default findConvexHull;
|
|
// Generation time: 116.732s
|
|
// Result: PASS
|