block by nitaku 7302132

Delaunay

Full Screen

Same as this example, but using d3.js Delaunay triangulation.

index.js

(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);

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Delaunay</title>
        <link type="text/css" href="index.css" rel="stylesheet"/>
        <script src="//d3js.org/d3.v3.min.js"></script>
        <script src="index.js"></script>
    </head>
    <body onload="main()"></body>
</html>

index.coffee

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")
        

index.css

.delaunay {
  opacity: 0.4;
}

.polygon {
  stroke: black;
  stroke-width: 2px;
  fill: none;
}

index.sass

.delaunay
    opacity: 0.4
    
.polygon
    stroke: black
    stroke-width: 2px
    fill: none