xxxxxxxxxx
89
var circles = [];
var maxDiameter = 100;
function setup() {
createCanvas(600, 400);
circles.push(new Circ(random(width), random(height)));
}
function draw() {
background(0);
noFill();
stroke(255);
var foundSpot = false;
while (foundSpot == false){
var x = random(width);
var y = random(height);
var contained = false;
for (var i=0; i < circles.length; i++) {
if (circles[i].contains(x,y)){
contained = true;
}
}
if (contained == false){
foundSpot = true;
circles.push(new Circ(x,y));
}
}
for (let i = 0; i < circles.length; i++) {
for (let j = 0; j<i; j++){
if (circles[i].intersects(circles[j])){
circles[i].stopped = true;
circles[j].stopped = true;
}
}
}
for (let i = 0; i < circles.length; i++) {
circles[i].update();
circles[i].display();
}
}
class Circ {
constructor(x, y) {
this.x = x; //random(width);
this.y = y; //random(height);
this.diameter = 1.0;
this.stopped = false;
}
update() {
if (!this.stopped){
this.diameter = min(this.diameter + 0.5, maxDiameter);
}
}
intersects(that){
var out = false;
var dx = this.x - that.x;
var dy = this.y - that.y;
var dh = sqrt(dx*dx + dy*dy);
var rR = this.diameter/2 + that.diameter/2;
if (dh <= rR){
out = true;
}
return out;
}
contains (x, y){
var out = false;
var dx = this.x - x;
var dy = this.y - y;
var dh = sqrt(dx*dx + dy*dy);
if (dh < this.diameter/2){
out = true;
}
return out;
}
display() {
ellipse(this.x, this.y, this.diameter, this.diameter);
}
}