Published unlisted
Edited
Sep 3, 2019
Insert cell
Insert cell
Insert cell
function edges(poly) {
if (!poly || poly.length < 3) return [];
let a = poly[0],
b = poly[1];
const edges = [];
for (let i = 2; i < poly.length + 2; i++) {
const c = poly[i % poly.length];
if (
Math.abs((a[0] - b[0]) * (a[1] - c[1]) - (a[0] - c[0]) * (a[1] - b[1])) >
1e-18
) {
edges.push(b);
a = b;
}
b = c;
}
return edges.concat([edges[0]]);
}
Insert cell
distribution = function() {
return Array.from({ length: 200 }, () => [
Math.random() * 300,
Math.random() * 200
]);
}
Insert cell
{
const height = 5060;
const svg = d3.create("svg").attr("viewBox", [-20, -20, width, height]);

for (let y = 10; y < height; y += 210) {
for (let x = 10; x < width - 150; x += 305) {
svg
.append("g")
.attr("transform", `translate(${x},${y})`)
.call(example);
yield svg.node();
}
}
}
Insert cell
function example(svg) {
const points = distribution();
const voronoi = d3.Delaunay.from(points).voronoi([0, 0, 290, 190]);

const spurious = new Set(
[...voronoi.cellPolygons()].flat().map(d => d.join(","))
);

for (let i = 0; i < points.length; i++) {
for (const d of edges(voronoi.cellPolygon(i))) {
spurious.delete(d.join(","));
}
}

svg
.append("path")
.attr("d", voronoi.renderBounds())
.attr("fill", "none")
.attr("stroke", "black");

svg
.append("path")
.attr("d", voronoi.render())
.attr("fill", "none")
.attr("stroke", "#999");

svg
.selectAll("circle")
.data(points)
.join("circle")
.attr("r", 1)
.attr("transform", d => `translate(${d})`);

svg
.append("g")
.selectAll("circle")
.data([...spurious])
.join("circle")
.attr("r", 4)
.style("fill", "red")
.attr("transform", d => `translate(${d})`);
}
Insert cell
d3 = require("d3@5", "d3-delaunay@5.1.4")
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