// Draw the polyhedron.
// Copyright 2007 Bill Gibbons
// License: Creative Commons Attribution 3.0 License
// See http://creativecommons.org/licenses/by/3.0/

function draw_polyhedron(ctx, polyhedron) {
  polyhedron.sort(
    function(x,y) {
      var sumx = 0;
      for (var i = 1; i < x.length; i++)
        sumx += x[i][2];
      var sumy = 0;
      for (var i = 1; i < y.length; i++)
        sumy += y[i][2];
      return (sumx - sumy);
    }
  );

  for (var i = 0; i < polyhedron.length; i++)
    draw_polygon(ctx, polyhedron[i]);
}


function draw_polygon(ctx, polygon) {
  ctx.beginPath();
  ctx.fillStyle = polygon.color;
  ctx.lineWidth = 0.5;
  ctx.lineJoin = "round";

  ctx.moveTo(polygon[0][0], polygon[0][1]);
  for (var j = 1; j < polygon.length; j++)
    ctx.lineTo(polygon[j][0], polygon[j][1]);
  ctx.lineTo(polygon[0][0], polygon[0][1]);

  ctx.fill();
  ctx.stroke();
}
