xxxxxxxxxx
75
let circles = [];
function setup() {
createCanvas(500, 500);
}
function draw() {
background(64);
for (let c of circles) {
c.draw();
}
addCircles(1);
stopExistingCircles();
}
function addCircles(amount) {
for (let i = 0; i < amount; i++) {
let newCircle = new Circle(random(width), random(height));
if (!newCircleOverlaps(newCircle)) {
circles.push(newCircle);
}
}
}
function newCircleOverlaps(newCircle) {
for (let otherCircle of circles) {
if (newCircle.overlaps(otherCircle)) {
return true;
}
}
return false;
}
function stopExistingCircles(){
for (let i = 0; i < circles.length - 1; i++) {
let circleOne = circles[i];
for (let j = i + 1; j < circles.length; j++) {
let circleTwo = circles[j];
if(circleOne.overlaps(circleTwo)){
circleOne.isGrowing = false;
circleTwo.isGrowing = false;
}
}
}
}
class Circle {
constructor(x, y) {
this.x = x;
this.y = y;
this.r = 5;
this.isGrowing = true;
this.color = color(random(255), random(255), random(255));
}
draw() {
fill(this.color);
circle(this.x, this.y, this.r * 2);
if(this.isGrowing){
this.r += .1;
}
}
overlaps(otherCircle){
return dist(this.x, this.y, otherCircle.x, otherCircle.y)
< this.r + otherCircle.r + 1;
}
}