mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-03-17 19:21:01 +00:00
26 lines
751 B
JavaScript
26 lines
751 B
JavaScript
async function findConvexHull(points) {
|
|
const { default: _ } = await import("https://cdn.skypack.dev/lodash");
|
|
|
|
const unique = _.uniqWith(points, _.isEqual);
|
|
const sorted = _.sortBy(unique, ["x", "y"]);
|
|
|
|
if (sorted.length < 3) return sorted;
|
|
|
|
const cross = (o, a, b) =>
|
|
(a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
|
|
|
|
const buildHalf = (pts) =>
|
|
pts.reduce((hull, p) => {
|
|
while (hull.length >= 2 && cross(hull.at(-2), hull.at(-1), p) <= 0)
|
|
hull.pop();
|
|
return [...hull, p];
|
|
}, []);
|
|
|
|
const lower = buildHalf(sorted);
|
|
const upper = buildHalf([...sorted].reverse());
|
|
|
|
return [...lower.slice(0, -1), ...upper.slice(0, -1)];
|
|
}
|
|
export default findConvexHull;
|
|
// Generation time: 3.848s
|
|
// Result: PASS
|