mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-06-07 20:12:13 +00:00
27 lines
798 B
JavaScript
27 lines
798 B
JavaScript
export async function findConvexHull(points) {
|
|
const { sortBy, uniqBy } = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/+esm');
|
|
const pts = sortBy(uniqBy(points, ({ x, y }) => `${x},${y}`), ['x', 'y']);
|
|
|
|
if (pts.length < 3) return pts;
|
|
|
|
const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
|
|
|
|
const build = (arr) => {
|
|
const hull = [];
|
|
for (const p of arr) {
|
|
while (hull.length >= 2 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) {
|
|
hull.pop();
|
|
}
|
|
hull.push(p);
|
|
}
|
|
return hull;
|
|
};
|
|
|
|
const lower = build(pts);
|
|
const upper = build([...pts].reverse());
|
|
|
|
return [...lower.slice(0, -1), ...upper.slice(0, -1)];
|
|
}
|
|
export default findConvexHull;
|
|
// Generation time: 14.861s
|
|
// Result: PASS
|