mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 08:37:56 +00:00
26 lines
773 B
JavaScript
26 lines
773 B
JavaScript
async function findConvexHull(points) {
|
|
const _ = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js').then(m => m.default);
|
|
|
|
if (points.length < 3) return points;
|
|
|
|
const sorted = _.sortBy(_.uniqWith(points, _.isEqual), ['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 buildHull = pts => {
|
|
const hull = [];
|
|
for (const p of pts) {
|
|
while (hull.length >= 2 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) {
|
|
hull.pop();
|
|
}
|
|
hull.push(p);
|
|
}
|
|
hull.pop();
|
|
return hull;
|
|
};
|
|
|
|
return [...buildHull(sorted), ...buildHull(sorted.slice().reverse())];
|
|
}
|
|
export default findConvexHull; |