Published
Edited
Jul 4, 2022
9 stars
Insert cell
Insert cell
Plot.plot({
style: { overflow: "visible" },
marginTop: 80,
marginRight: 80,
width: width + 100,
height: height + 90,
x: { domain: [0, width] },
y: { domain: [0, height] },
marks: [
Plot.voronoi(pts.slice(0, N)),
Plot.dot(pts, {
fill: (d, i) => (i < N ? "red" : "#ccc"),
r: Math.min(3, Math.sqrt(3000 / N))
})
]
})
Insert cell
pts = points(width, height, N)
Insert cell
N = 700
Insert cell
width = 700
Insert cell
height = 500 // if the aspect ratio width / height is too far from 1, you may have to adapt the algorithm
Insert cell
import { poissonDisc2d } from "@fil/2d-point-distributions"
Insert cell
function points(w, h, N) {
const t = Math.max(w, h);
const P = d3.sort(
poissonDisc2d(w + t / 10, h + t / 10, N * 1.2),
([x, y]) => -Math.min(w - x, h - y)
);
const Q = P.slice(0, N);
const M = d3.min(Q, ([x, y]) => Math.min(w - x, h - y));

// scale to fill
const ratio = t / (t - M);
for (const d of P) {
d[0] *= ratio;
d[1] *= ratio;
}

// translate to center
const x = d3.extent(Q, (d) => d[0]);
const y = d3.extent(Q, (d) => d[1]);
const tx = (w - x[1] - x[0]) / 2;
const ty = (h - y[1] - y[0]) / 2;
P.forEach((d) => {
d[0] += tx;
d[1] += ty;
});

// return Q; // or P for the purposes of the demo
return P;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more