xxxxxxxxxx
56
let bubbles = [];
function setup() {
createCanvas(600, 400);
for (let i = 0; i < 4; i++) {
bubbles.push(new Bubble(random(width), random(height)));
}
}
function draw() {
background(0);
for (let bubble of bubbles) {
for (let b of bubbles) {
bubble.otherBub(b);
}
bubble.show();
bubble.grow();
}
}
class Bubble {
constructor(x, y) {
this.x = x;
this.y = y;
this.d = 1;
this.growing = true;
}
show() {
noFill();
stroke(255);
circle(this.x, this.y, this.d);
}
grow() {
if (this.edges()) {
if (this.growing) {
this.d += 1;
}
}
}
edges() {
return (
this.x + this.d / 2 < width &&
this.x - this.d / 2 > 0 &&
this.y + this.d / 2 < height &&
this.y - this.d / 2 > 0
);
}
otherBub(other) {
let d = dist(this.x, this.y, other.x, other.y);
if (d < this.d/2+other.d/2) {
this.growing = false;
} else {
this.growing = true;
}
}
}