xxxxxxxxxx
83
let bobs = [];
function setup() {
createCanvas(800, 400);
rectMode(CENTER);
}
function draw() {
background(0,5);
let bobby = addBob();
if (bobby !== null) {
bobs.push(bobby);
}
for (let bob of bobs) {
for (let b of bobs) {
if (bob !== b) bob.grow(b);
}
bob.show();
}
}
function addBob() {
let x = random(width);
let y = random(height);
let validPosition = true;
for (let i = 0; i < bobs.length; i++) {
let d = dist(x, y, bobs[i].x, bobs[i].y);
if (d < bobs[i].r + bobs[i].strokeWidth) {
validPosition = false;
break;
}
}
if (validPosition) {
return new Bob(x, y);
} else {
return null;
}
}
class Bob {
constructor(x, y) {
this.x = x;
this.y = y;
this.r = 1;
this.growing = true;
this.strokeWidth = 1;
this.shade = color(0, 0, 0);
}
show() {
// fill(this.shade);
noFill();
strokeWeight(this.strokeWidth);
stroke(this.shade);
rect(this.x, this.y, this.r * 2);
}
touch(other) {
let d = dist(this.x, this.y, other.x, other.y);
if (d < this.r + other.r) {
this.growing = false;
}
}
grow(other) {
if (this.growing && !this.outCanvas() && !this.touch(other)) {
this.r += 0.001;
let randy = color(0,255, random(255),50)
this.shade = randy;
}
}
outCanvas() {
return (
this.x + this.r > width ||
this.y + this.r > height ||
this.x - this.r < 0 ||
this.y - this.r < 0
);
}
}