mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-15 09:07:55 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,29 +1,23 @@
|
||||
async function findConvexHull(points) {
|
||||
if (!Array.isArray(points) || points.length < 3) return points || [];
|
||||
if (!Array.isArray(points) || points.length < 1) return [];
|
||||
const { default: _ } = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.default.min.js');
|
||||
const ps = _.uniqWith(points, (a, b) => a.x === b.x && a.y === b.y);
|
||||
if (ps.length < 3) return ps;
|
||||
const pts = _.sortBy(ps, ['x', 'y']);
|
||||
const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
|
||||
const lower = [];
|
||||
const s = _.uniqBy(points, p => `${p.x},${p.y}`);
|
||||
if (s.length < 2) return s.slice();
|
||||
const pts = _.sortBy(s, ['x', 'y']);
|
||||
const c = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
|
||||
const l = [];
|
||||
for (let p of pts) {
|
||||
for (; lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0;) lower.pop();
|
||||
lower.push(p);
|
||||
for (; l.length > 1 && c(l[l.length - 2], l[l.length - 1], p) <= 0;) l.pop();
|
||||
l.push(p);
|
||||
}
|
||||
const upper = [];
|
||||
const u = [];
|
||||
for (let i = pts.length - 1; i >= 0; i--) {
|
||||
const p = pts[i];
|
||||
for (; upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0;) upper.pop();
|
||||
upper.push(p);
|
||||
for (; u.length > 1 && c(u[u.length - 2], u[u.length - 1], p) <= 0;) u.pop();
|
||||
u.push(p);
|
||||
}
|
||||
const hull = lower.slice(0, -1).concat(upper.slice(0, -1));
|
||||
if (hull.length <= 2) return hull;
|
||||
let area = 0;
|
||||
for (let i = 0; i < hull.length; i++) {
|
||||
const j = (i + 1) % hull.length;
|
||||
area += hull[i].x * hull[j].y - hull[j].x * hull[i].y;
|
||||
}
|
||||
if (area > 0) hull.reverse();
|
||||
return hull;
|
||||
l.pop();
|
||||
u.pop();
|
||||
return l.concat(u);
|
||||
}
|
||||
export default findConvexHull;
|
||||
Reference in New Issue
Block a user