For this project, I decided to go in a direction of analyzing covid spread through showing how if you infect one person, how the others will get infected through probability and chance. I will use an array of people and if one or more is clicked, a droplet of covid is “given” to the person to “infect” them. Over time, the nearby people will heal and recover or pass away. If all people are infected, the population will never recover.
// 15-104
// SEAN CHEN
// FINAL PROJECT
var people = [];
var wNum = 14;
var hNum = 12;
var chance = .1; // chance of transmission
var speed = 20; // speed of transmission
var deathRate = 0.02; // death rate
var body;
// image for the body
function preload() {
body = loadImage('https://i.imgur.com/BBvwTQi.png');
}
// creating the heads array
function setup() {
createCanvas(600, 400);
for (var i = 0; i < wNum; i++) {
var temp = []
for (var j = 0; j < hNum; j++) {
temp.push(createPerson(i, j));
}
people.push(temp);
}
frameRate(speed);
}
// drawing people + infection spreading
function draw() {
background(235);
updatePerson();
infectPerson();
}
// step and drawing of people
function updatePerson() {
for (var i = 0; i < wNum; i++) {
for (var j = 0; j < hNum; j++) {
people[i][j].draw();
people[i][j].step();
}
}
}
// creating person object
function createPerson(x, y) {
var p = {
px: x*(width/(wNum-1)),
py: y*(height/(hNum-1)),
infected: 0,
age: 0,
col: 0,
draw: thePerson,
step: stepPerson,
}
return p;
}
// individual people
function thePerson() {
stroke(0);
push();
translate(-20, 5);
image(body, this.px, this.py, 40, 80);
pop();
push();
fill(colPerson(this.col));
circle(this.px, this.py, 30);
fill(255);
ellipse(this.px-8, this.py-2, 7, 10);
ellipse(this.px+8, this.py-2, 7, 10);
pop();
push();
noStroke();
fill(0);
circle(this.px-8, this.py-2, 4, 4);
circle(this.px+8, this.py-2, 4, 4);
pop();
}
// red for most infected
// green for temp immunity
// black for death
function colPerson(colNum) {
if (colNum == 0) {
return color(255);
} else if (colNum == 1) {
return color(255, 0, 0);
} else if (colNum == 2) {
return color(255, 65, 65);
} else if (colNum == 3) {
return color(255, 130, 130);
} else if (colNum == 4) {
return color(255, 195, 195);
} else if (colNum == 5) {
return color(0, 255, 0);
} else if (colNum == 6) {
return color(0);
}
}
// if infected, what stage infection
function stepPerson() {
if (this.infected == 2) {
this.col = 6;
this.age = 0;
}
if (this.infected == 1) {
this.age++;
if (this.age <= 10) {
this.col = 1;
} else if (this.age > 10 & this.age <= 20) {
this.col = 2;
} else if (this.age > 20 & this.age <= 30) {
this.col = 3;
} else if (this.age > 30 & this.age < 40) {
this.col = 4;
} else if (this.age < 60) {
if (random(0, 10) < deathRate) {
this.infected = 2;
} else {
this.col = 5;
}
} else if (this.age == 60) {
this.col = 0;
this.age = 0;
this.infected = 0;
}
}
}
function mouseClicked() {
clickPerson();
}
// clicking persons to give covid
function clickPerson() {
for (var i = 0; i < wNum; i++) {
for (var j = 0; j < hNum; j++) {
if (dist(mouseX, mouseY, people[i][j].px, people[i][j].py) < 20) {
people[i][j].infected = 1;
people[i][j].col = 1;
people[i][j].age = 0;
}
}
}
}
// infecting right
function r(i, j) {
if (random(0, 10) < chance & people[i+1][j].infected != 2) {
people[i+1][j].infected = 1;
}
}
// infecting left
function l(i, j) {
if (random(0, 10) < chance & people[i-1][j].infected != 2) {
people[i-1][j].infected = 1;
}
}
// infecting up
function u(i, j) {
if (random(0, 10) < chance & people[i][j-1].infected != 2) {
people[i][j-1].infected = 1;
}
}
// infecting down
function d(i, j) {
if (random(0, 10) < chance & people[i][j+1].infected != 2) {
people[i][j+1].infected = 1;
}
}
// probability and chance of spread
function infectPerson() {
for (var i = 0; i < wNum; i++) {
for (var j = 0; j < hNum; j++) {
if (people[i][j].age >= 20) {
if (i > 0 & j > 0 && i < wNum-1 && j < hNum-1) {
r(i, j);
l(i, j);
u(i, j);
d(i, j);
} else if (i == 0 & j == hNum-1) {
r(i, j);
u(i, j);
} else if (i == wNum-1 & j == 0) {
l(i, j);
d(i, j);
} else if (i == 0 & j == 0) {
r(i, j);
d(i, j);
} else if (i == 0) {
r(i, j);
u(i, j);
d(i, j);
} else if (j == 0) {
r(i, j);
l(i, j);
d(i, j);
} else if (i == wNum-1 & j == hNum-1) {
l(i, j);
u(i, j);
} else if (i == wNum-1) {
l(i, j);
u(i, j);
d(i, j);
} else if (j == hNum-1) {
r(i, j);
l(i, j);
u(i, j);
}
}
}
}
}