nick34992
4/20/2016 - 3:23 AM

Ellipse No Overlap. Using p5.js library.

Ellipse No Overlap. Using p5.js library.

// Based on code featured in Shiffman's video tutorial. https://youtu.be/XATr_jdh-44

var circles = [];

function setup() {
  createCanvas(896, 504);
  var protection = 0;
  while (circles.length < 2000) {
    var circle = {
      x: random(width),
      y: random(height),
      r: random(6, 36)
    }
    var overlapping = false;
    for (var j = 0; j < circles.length; j++) {
      var other = circles[j];
      var d = dist(circle.x, circle.y, other.x, other.y);
      if (d < circle.r + other.r) {
        overlapping = true;
      }
    }
    if (!overlapping) {
      circles.push(circle);
    }
    protection++;
    if (protection > 10000) {
      break;
    }
  }
  for (var i = 0; i < circles.length; i++) {
    fill(random(128, 255), random(128, 255), random(128, 255));
    noStroke();
    ellipse(circles[i].x, circles[i].y, circles[i].r * 2, circles[i].r * 2);
  }
}