xxxxxxxxxx
let n = 15;
let radius = 12;
let diameter;
let space;
let agents = [];
let numAgents = n*n;
function setup() {
createCanvas(600, 600);
colorMode(RGB, 1.0, 1.0, 1.0);
background(1.0, 1.0, 1.0);
diameter = 2 * radius;
space = (width - n * diameter) / (n - 1);
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
let c = color(random(1.0), random(1.0), random(1.0));
agents[i * n + j] = new Agent(i * space + (2 * i + 1) * radius, j * space + (2 * j + 1) * radius, radius, c);
}
}
}
function draw() {
background(1.0, 1.0, 1.0);
for(let i = 0; i < numAgents; i++) {
agents[i].display();
agents[i].evolve(0.05);
}
}
class Agent{
constructor(x, y, radius, c) {
this.x = x;
this.y = y;
this.radius = radius;
this.r0 = radius;
this.r = 1.0;
this.c = c;
this.r = red(c);
this.g = green(c);
this.b = blue(c);
}
evolve(dt) {
let temp = this.r + random(-dt, dt);
if(temp <= 1.0 && temp >= 0) {
this.r = temp;
}
temp = this.g + random(-dt, dt);
if(temp <= 1.0 && temp >= 0) {
this.g = temp;
}
temp = this.b + random(-dt, dt);
if(temp <= 1.0 && temp >= 0) {
this.b = temp;
}
this.c = color(this.r, this.g, this.b);
}
display() {
noStroke();
fill(this.c);
circle(this.x, this.y, 2 * this.radius);
}
}