Published
Edited
Jan 19, 2020
2 forks
11 stars
Also listed in…
Graphs
Insert cell
Insert cell
function true_neighbors(voronoi, i) {
const n = [];
const ai = new Set((voronoi.cellPolygon(i) || []).map(String));
for (const j of voronoi.delaunay.neighbors(i))
for (const c of voronoi.cellPolygon(j) || [])
if (ai.has(String(c))) n.push(j);
return n;
}
Insert cell
{
const context = DOM.context2d(width, height),
path = d3
.geoPath()
.context(context)
.pointRadius(3);

function render(i) {
context.clearRect(0, 0, width, height);

if (i === +i) {
context.fillStyle = "lime";
context.beginPath();
voronoi.renderCell(i, context);
context.fill();

const Y = true_neighbors(voronoi, i),
U = [...delaunay.neighbors(i)].filter(j => !Y.includes(j));

context.fillStyle = "yellow";
context.beginPath();
for (const j of U) voronoi.renderCell(j, context);
context.fill();

context.fillStyle = "#ccffcc";
context.beginPath();
for (const j of Y) voronoi.renderCell(j, context);
context.fill();

context.beginPath();
for (const j of delaunay.neighbors(i)) {
context.moveTo(...points[i]);
context.lineTo(...points[j]);
}
context.strokeStyle = "green";
context.stroke();
}

context.strokeStyle = "#000";

context.beginPath();
voronoi.render(context);
voronoi.renderBounds(context);
context.stroke();

context.fillStyle = "black";
context.beginPath();
path({ type: "MultiPoint", coordinates: points });
context.fill();
}

render(4);

return d3
.select(context.canvas)
.on("mousemove", function() {
const m = d3.mouse(this),
i = delaunay.find(...m);
render(i);
})
.node();
}
Insert cell
m = 1
Insert cell
voronoi.cellPolygon(0)
Insert cell
delaunay = d3.Delaunay.from(points)
Insert cell
voronoi = delaunay.voronoi([m, m, width - m, height - m])
Insert cell
width = 500
Insert cell
height = width
Insert cell
d3 = require("d3@5", "d3-delaunay@5")
Insert cell
rng = {
const frac = x => x - Math.floor(x);
let i = 0.1,
a = 0;
return () => (a = frac((1 + Math.sin(++i) + a) * 100000));
}
Insert cell
points = Array.from({ length: 200 }, () => [rng() * width, rng() * height])
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