Skip to content

Instantly share code, notes, and snippets.

@wiederkehr
Created September 16, 2011 09:35
Show Gist options
  • Save wiederkehr/1221675 to your computer and use it in GitHub Desktop.
Save wiederkehr/1221675 to your computer and use it in GitHub Desktop.
Voronoi Tesselation
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Voronoi Tesselation</title>
<link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="chart"></div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://raw.github.com/mbostock/d3/master/lib/colorbrewer/colorbrewer.js"></script>
<script src="script.js"></script>
</body>
</html>
(function() {
var width = 960,
height = 500;
var vertices = d3.range(100).map(function(d) {
return [Math.random() * width, Math.random() * height];
});
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "RdYlBu")
.on("mousemove", function() { vertices[0] = d3.mouse(this); update(); });
var path = svg.append("g").selectAll("path");
svg.selectAll("circle")
.data(vertices.slice(1))
.enter().append("circle")
.attr("transform", function(d) { return "translate(" + d + ")"; })
.attr("r", 2);
update()
function update() {
path = path.data(d3.geom.voronoi(vertices).map(function(d) { return "M" + d.join("L") + "Z"; }), String);
path.exit().remove();
path.enter().append("path").attr("class", function(d, i) { return "q" + (i % 9) + "-9"; }).attr("d", String);
path.order();
}
})()
path {
stroke: #000;
stroke-width: .5px;
}
path:first-child {
fill: #000 !important;
}
circle {
fill: #fff;
stroke: #000;
stroke-width: .5px;
pointer-events: none;
}
.RdYlBu .q0-9{fill:rgb(215,48,39)}
.RdYlBu .q1-9{fill:rgb(244,109,67)}
.RdYlBu .q2-9{fill:rgb(253,174,97)}
.RdYlBu .q3-9{fill:rgb(254,224,144)}
.RdYlBu .q4-9{fill:rgb(255,255,191)}
.RdYlBu .q5-9{fill:rgb(224,243,248)}
.RdYlBu .q6-9{fill:rgb(171,217,233)}
.RdYlBu .q7-9{fill:rgb(116,173,209)}
.RdYlBu .q8-9{fill:rgb(69,117,180)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment