xxxxxxxxxx
125
let personas = [];
let poblacion = 200;
let latencia = 100;
let mortalidad = 30;
const WIDTH = 600;
const HEIGHT = 600;
let colores = {
sano: 'lime',
enfermo: 'orange',
recuperado: 'cyan',
muerto: 'black'
};
function setup() {
createCanvas(WIDTH, HEIGHT);
for(let i= 1; i<=poblacion; i++) {
personas.push( new Persona(random(WIDTH) , random(HEIGHT)) );
}
personas[10].estado = 'enfermo';
}
function draw() {
background(220);
personas.forEach(function(p1, i1) {
personas.forEach(function(p2, i2) {
if (i1 != i2) {
p1.colisiona(p2);
}
});
});
let contadores = {
sanos: 0,
enfermos: 0,
recuperados: 0,
muertos: 0
};
personas.forEach(function(p, i) {
p.actualiza();
contadores[ p.estado +'s' ] ++;
});
if (contadores.enfermos == 0) {
noLoop();
console.table(contadores);
}
}
class Persona {
constructor(x,y){
this.pos = createVector(x,y);
this.vel = createVector( random(-3,3), random(-3,3) );
this.radio = 3;
this.estado = 'sano';
this.tiempoenfermo = 0;
}
actualiza(){
if (this.estado != 'muerto') {
this.rebotarenlasparedes();
this.pos.add(this.vel);
}
if (this.estado == 'enfermo') {
this.tiempoenfermo ++;
if (this.tiempoenfermo > latencia){
if ( random(1,100) <= mortalidad) {
this.estado = 'muerto';
} else {
this.estado = 'recuperado';
}
}
}
this.dibuja();
}
rebotarenlasparedes(){
if ((this.pos.x - this.radio < 0) ||
(this.pos.y - this.radio < 0) ||
(this.pos.x + this.radio > width) ||
(this.pos.y + this.radio > height)) {
this.vel.rotate(HALF_PI);
this.pos.x = constrain( this.pos.x, this.radio, width - this.radio );
this.pos.y = constrain( this.pos.y, this.radio, height - this.radio );
}
}
dibuja(){
fill( colores[ this.estado] );
ellipse(this.pos.x, this.pos.y, this.radio * 2, this.radio * 2);
}
colisiona(p) {
if (
dist(this.pos.x, this.pos.y,
p.pos.x, p.pos.y) < this.radio *2) {
this.vel.rotate( random( HALF_PI ) );
this.contagio(p);
}
}
contagio(p) {
if (this.estado == 'enfermo' || p.estado == 'enfermo') {
this.contagiado();
p.contagiado();
}
}
contagiado() {
if (this.estado == 'sano'){
this.estado = 'enfermo';
}
}
}