Skip to content

Instantly share code, notes, and snippets.

@nitaku
Last active December 27, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitaku/7302132 to your computer and use it in GitHub Desktop.
Save nitaku/7302132 to your computer and use it in GitHub Desktop.
Delaunay
window.main = () ->
width = 960
height = 500
svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height)
polygon = [[300, 200], [300, 300], [370, 310], [400, 300], [360, 260], [400, 180]]
colorize = d3.scale.category10()
svg.selectAll('.delaunay')
.data(d3.geom.delaunay(polygon))
.enter().append('path')
.attr('class', 'delaunay')
.style('fill', (d, i) -> colorize(i))
.attr('d', (d) -> "M#{d.join('L')}Z")
svg.selectAll('.polygon')
.data([polygon])
.enter().append('path')
.attr('class', 'polygon')
.attr('d', (d) -> "M#{d.join('L')}Z")
.delaunay {
opacity: 0.4;
}
.polygon {
stroke: black;
stroke-width: 2px;
fill: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Delaunay</title>
<link type="text/css" href="index.css" rel="stylesheet"/>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="index.js"></script>
</head>
<body onload="main()"></body>
</html>
(function() {
window.main = function() {
var colorize, height, polygon, svg, width;
width = 960;
height = 500;
svg = d3.select('body').append('svg').attr('width', width).attr('height', height);
polygon = [[300, 200], [300, 300], [370, 310], [400, 300], [360, 260], [400, 180]];
colorize = d3.scale.category10();
svg.selectAll('.delaunay').data(d3.geom.delaunay(polygon)).enter().append('path').attr('class', 'delaunay').style('fill', function(d, i) {
return colorize(i);
}).attr('d', function(d) {
return "M" + (d.join('L')) + "Z";
});
return svg.selectAll('.polygon').data([polygon]).enter().append('path').attr('class', 'polygon').attr('d', function(d) {
return "M" + (d.join('L')) + "Z";
});
};
}).call(this);
.delaunay
opacity: 0.4
.polygon
stroke: black
stroke-width: 2px
fill: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment