Published
Edited
Aug 23, 2019
Importers
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function convexHull2D(p) {
const p_ = p.slice();
const sorted = p_.sort((a, b) => sortByX(a, b));
const upper = upperHull(sorted);
const lower = lowerHull(sorted);
return [...upper, ...lower];
}
Insert cell
function upperHull(p) {
const n = p.length;
const u = [];
for (let i = 0; i < n; ++i) {
while (u.length >= 2 && det3(u[u.length - 2], u[u.length - 1], p[i]) <= 0) {
u.pop();
}
u.push(p[i]);
}
return u;
}
Insert cell
function lowerHull(p) {
const n = p.length;
const b = p.slice().reverse();
const l = [];
for (let i = 0; i < b.length; ++i) {
while (l.length >= 2 && det3(l[l.length - 2], l[l.length - 1], b[i]) <= 0) {
l.pop();
}
l.push(b[i]);
}
l.shift();
return l;
}
Insert cell
function drawHull(hull, context) {
const hl = hull.length;
hull.forEach((p, i) => line(p, hull[(i + 1) % hl], context));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more