Example 9: Space Filling Phyllotaxy Spiral

refresh(dc, width, height) // Sample code by Jim Bumgardner
{
  dc.clearRect(0,0,width,height);
  
  dc.fillStyle='#000';
  var nbr_circles = 200;
  
  var phi = (Math.sqrt(5)+1)/2 - 1;            // golden ratio
  var golden_angle = phi*2*Math.PI;            // golden angle
  
  var lg_rad = width * .45;
  var lg_area = Math.pow(lg_rad,2)*Math.PI;
  
  var sm_area = lg_area / nbr_circles; // Area of our little circles, if they filled the space entirely
  var sm_rad = Math.sqrt( sm_area / Math.PI ); // This is related to the equation area = pi r squared
  var fudge = .87; // Fudge factor, since our circles don't actually fill up space entirely.
  
  var cx = width/2;
  var cy = height/2;
  
  for (var i = 1; i <= nbr_circles; ++i) {
    dc.beginPath();
    var angle = i*golden_angle;
    var cum_area = i*sm_area;
    var spiral_rad = Math.sqrt( cum_area / Math.PI );
    var x = cx + Math.cos(angle) * spiral_rad;
    var y = cy + Math.sin(angle) * spiral_rad;
    dc.arc(x, y, sm_rad * fudge, 0, 2*Math.PI, false);
    dc.fill();
  }
  
}