See Issue #11 : .triangles() crashing when there is an extent().
Forked from mbostock's block: Delaunay Triangulation
xxxxxxxxxx
<meta charset="utf-8">
<style>
.polygon {
fill: none;
stroke: #ecc;
}
.links {
stroke: #413bb3;
stroke-width: 1.5;
}
.blinks {
stroke: #c53634;
stroke-width: 1.5;
}
.triangles {
fill: #ccf;
}
.btriangles {
fill: #feeb56;
}
.sites {
fill: #000;
stroke: #fff;
}
</style>
<svg width="960" height="400"></svg>
<p>see <a href="https://github.com/d3/d3-voronoi/issues/11">Issue #11</a>.</p>
<p>In blue: without .extent()
<br>In yellow/red: with .extent()</p>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="d3-voronoi.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var sites = d3.range(100)
.map(function(d) { return [ Math.random() * width, Math.random() * height]; });
var voronoi = d3.voronoi();
var bvoronoi = d3.voronoi()
.extent([[1, 1],[width-1, height-1]]);
svg.append("g")
.attr("class", "polygon")
.selectAll("path")
.data(bvoronoi.polygons(sites))
.enter().append("path")
.attr("d", function(d) { return "M" + d.join("L") + "Z"; });
svg.append("g")
.attr("class", "triangles")
.selectAll("path")
.data(voronoi.triangles(sites))
.enter().append("path")
.attr("d", function(d) { return "M" + d.join("L") + "Z"; });
svg.append("g")
.attr("class", "btriangles")
.selectAll("path")
.data(bvoronoi.triangles(sites))
.enter().append("path")
.attr("d", function(d) { return "M" + d.join("L") + "Z"; });
svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(voronoi.links(sites))
.enter().append("line")
.call(redrawLink);
svg.append("g")
.attr("class", "blinks")
.selectAll("line")
.data(bvoronoi.links(sites))
.enter().append("line")
.call(redrawLink);
var site = svg.append("g")
.attr("class", "sites")
.selectAll("circle")
.data(sites)
.enter().append("circle")
.attr("r", 2.5)
.call(redrawSite);
function redrawLink(link) {
link
.classed("primary", function(d) { return d.source === sites[0] || d.target === sites[0]; })
.attr("x1", function(d) { return d.source[0]; })
.attr("y1", function(d) { return d.source[1]; })
.attr("x2", function(d) { return d.target[0]; })
.attr("y2", function(d) { return d.target[1]; });
}
function redrawSite(site) {
site
.attr("cx", function(d) { return d[0]; })
.attr("cy", function(d) { return d[1]; });
}
</script>
https://d3js.org/d3.v4.min.js