mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 16:47:55 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,28 +1,29 @@
|
||||
const findConvexHull = async (points) => {
|
||||
const _ = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js');
|
||||
const { sortBy, uniqWith, isEqual } =
|
||||
await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.js');
|
||||
|
||||
const sortedPoints = _.sortBy(_.uniqWith(points, _.isEqual), ['x', 'y']);
|
||||
const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
|
||||
|
||||
if (sortedPoints.length < 3) {
|
||||
return sortedPoints;
|
||||
const pts = sortBy(uniqWith(points, isEqual), ['x', 'y']);
|
||||
|
||||
if (pts.length < 3) return pts;
|
||||
|
||||
const lower = [];
|
||||
for (const p of pts) {
|
||||
while (lower.length >= 2 && cross(lower.at(-2), lower.at(-1), p) <= 0) {
|
||||
lower.pop();
|
||||
}
|
||||
lower.push(p);
|
||||
}
|
||||
|
||||
const crossProduct = (p1, p2, p3) =>
|
||||
(p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
|
||||
|
||||
const buildHalfHull = (pointSet) => {
|
||||
const hull = [];
|
||||
for (const p of pointSet) {
|
||||
while (hull.length >= 2 && crossProduct(hull.at(-2), hull.at(-1), p) <= 0) {
|
||||
hull.pop();
|
||||
}
|
||||
hull.push(p);
|
||||
const upper = [];
|
||||
for (let i = pts.length - 1; i >= 0; i--) {
|
||||
const p = pts[i];
|
||||
while (upper.length >= 2 && cross(upper.at(-2), upper.at(-1), p) <= 0) {
|
||||
upper.pop();
|
||||
}
|
||||
return hull;
|
||||
};
|
||||
|
||||
const lower = buildHalfHull(sortedPoints);
|
||||
const upper = buildHalfHull([...sortedPoints].reverse());
|
||||
upper.push(p);
|
||||
}
|
||||
|
||||
return [...lower.slice(0, -1), ...upper.slice(0, -1)];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user