A simple model where one agent is contagious and passes color along to the others until it is no longer contagious. The circle represents the zone of infection which decreases over time. Link to source files.
let numpts = 200;
let points = [];
function setup() {
createCanvas(800, 800);
for(let i = 0; i < numpts; i++){
append(points, new Pt());
}
points[10].state = 1;
}
function draw() {
background(0);
for(let i = 0; i < points.length; i++){
let p = points[i];
p.display();
p.move();
if(p.state > 0){
p.update();
}
}
}
class Pt{
constructor( xpos,ypos,size,ct,c ) {
this.xs = random(-1,1);
this.ys = random(-1,1);
this.x = random(width);
this.y = random(height);
this.cont = 0;
this.state = 0;
this.c = color(random(255),random(255),random(255));
}
move(){
if(this.x + this.xs < 0 || this.x + this.xs > width){
this.xs = -this.xs;
}
if(this.y + this.ys < 0 || this.y + this.ys > height){
this.ys = -this.ys;
}
this.x = this.x + this.xs;
this.y = this.y + this.ys;
}
update(){
if(this.state == 1){
this.cont += 3;
if(this.cont > 100){
this.cont = 100;
this.state = 2;
}
}
if(this.state == 2){
this.cont -= 1;
if(this.cont < 0){
this.cont = 0;
this.state = 0;
}
}
if(this.cont > 0){
for(let i = 0; i < points.length; i++){
let p1 = points[i];
let dd = dist(this.x,this.y,p1.x,p1.y);
if(dd < this.cont/2 && dd != 0 && p1.state == 0){
p1.state = 1;
let R = random(-20,20);
let G = random(-20,20);
let B = random(-20,20);
let newc = color(red(this.c)+R,green(this.c)+G,blue(this.c)+B);
p1.c = newc;
}
}
}
}
display(){
stroke(255);
noFill();
if(this.cont > 0){
stroke(this.c);
}
strokeWeight(6);
point(this.x,this.y);
if(this.cont > 0){
strokeWeight(2);
ellipse(this.x,this.y,this.cont);
}
}
}