var fc = require('fc');
var btnode = require('./binarytreenode');
var nick = require('./clip').nick;

var cleanPSLG = require('clean-pslg');
var poly2pslg = require('poly-to-pslg');
var pslg2poly = require('pslg-to-poly');
var arc = require('subdivide-arc');

var root = new btnode({
  plane: [],
  geometry: [[-100, 200], [-100, -100], [200, -100], [200, 200]]
});

root
  .cut([0,0, 0,100], 'L')
  .cut([0,100, 100,100], 'L')
  .cut([100,100, 100,0], 'L')
  .cut([100,0, 0,0], 'L')
  .cut([50,75, 25,50], 'R')
  .cut([25,50, 50,25], 'R')
  .cut([50,25, 75,50], 'R')
  .cut([75,50, 50,75], 'R');

var current = root;
var count = 5;
var arcpoints = arc(0, 0, 10, 0, Math.PI*2, count)
var currentPoint = arcpoints[0]
for (var i=1; i<count; i++) {
  var next = arcpoints[i];
  current = current.cut([
    currentPoint[0],
    currentPoint[1],
    next[0],
    next[1]
  ], '-')
  currentPoint = next;
}

function drawPoly (ctx, polygon) {
  ctx.moveTo(polygon[0][0], polygon[0][1]);
  for (var i = 0; i < polygon.length; i++) {
    ctx.beginPath();
    ctx.arc(polygon[i][0], polygon[i][1], 4, 0, 2 * Math.PI, false);
    ctx.fillStyle = 'rgba(' + 1 + ', 0, 0, ' + (i+1) *.15 + ')';
    ctx.closePath()
    ctx.fill();
  }

  ctx.beginPath();
  ctx.moveTo(polygon[0][0], polygon[0][1]);

  for (var i = 0; i < polygon.length; i++) {
    if (i < polygon.length - 1) {
      ctx.lineTo(polygon[i + 1][0], polygon[i + 1][1]);
    }
  }
  ctx.closePath();

  ctx.fillStyle = 'rgba(0, 0, 0, .1)';
  ctx.strokeStyle = 'rgba(0, 0, 0, .1)'
  ctx.stroke();
  ctx.fill();
}

function render (side) {
  var polygons = [];

  function renderIterator (context) {
    if (context && context.isLeaf() && (context.side === side) || (side === '-')) {
console.log(context.data.plane);
      polygons.push(context.data.geometry);
    }
  }

  function renderCompleted () {
    var ctx = fc(function () {
      ctx.translate(200, 200);
      for (var j = 0; j < polygons.length; j++) {
        drawPoly(ctx, polygons[j]);
      }
    });

    ctx.dirty();
  }

  root.traverse(renderIterator, 0, '-', renderCompleted);
}

@@ -891,24 +891,38 @@
  = next;%0A%7D%0A%0A
+render('R');%0A%0A
 function dra

add back missing call to kick off rendering