xxxxxxxxxx
// Virus Spread Simulator
// Part of East Hampton High School's Coding Java (Processing) course.
// Spring 2020 - Started March 3rd
// Urban Reininger aka UrbanAtWork
Host mary; // Named after Typhoid Mary but really Patient Zero here
int numOfPopulation = 200;
int hostSize = 12;
Host[] population = new Host[numOfPopulation];
boolean maryT = true;
int touchedCounter = 0;
int immuneCounter = 0;
int untouchedCounter =0;
int whenMaryImmune = 1000; // *out of order - Number of frames it takes before Mary stops spreading virus (she keeps spreading)
Barrier [] walls = new Barrier[2]; // all the wall barriers.
Graphit graph = new Graphit(frameCount, touchedCounter);
Graphit graph2 = new Graphit(frameCount, immuneCounter);
Graphit graph3 = new Graphit(frameCount, untouchedCounter);
////////////////////////////////////////////
void setup(){
//createCanvas(windowWidth, windowHeight); // doesn't work in non p5.js version
size(380,700);
//frameRate(15);
mary = new Host(true);
mary.immune = false;
for(int i=0; i<numOfPopulation; i++){
population[i] = new Host(hostSize);
}
walls[0] = new Barrier(250,0,8,height/2-30);
walls[1] = new Barrier(250,height/2+30,8,height/2);
mary.infect();
} // end of setup ====================================
void draw(){
background(64);
mary.update();
mary.show();
for(int i=0; i<numOfPopulation; i++){
float distToMary = dist(mary.position.x,mary.position.y,population[i].position.x,population[i].position.y);
//text(distToMary, population[i].position.x,population[i].position.y);
if(distToMary < (mary.howBig/2 + population[i].howBig/2)){
if(!mary.immune){
population[i].infect();
}
} // end if
for(int m = 0; m < numOfPopulation; m++){
if(m!=i){
float distToOther = dist(population[i].position.x, population[i].position.y, population[m].position.x, population[m].position.y);
if(distToOther < (population[i].howBig/2) + (population[m].howBig/2)){
if((population[i].touched && !population[i].immune) || (population[m].touched && !population[m].immune)){
population[i].infect();
population[m].infect();
}// end if!
} // end if
} // end if
} // end of for m
population[i].show();
population[i].update();
for(int w=0; w < walls.length; w++){
int rebound = block(walls[w].x, walls[w].y, walls[w].w, walls[w].h, population[i].position);
population[i].velocity.x *= rebound;
} // end of w loop
} // end for i loop
//////// COUNTERS ==========================================
touchedCounter = 0;
for(int s=0; s<numOfPopulation; s++){
if(population[s].touched){ // check to see how many are "touched"
touchedCounter++;
}// end if
} // end for
immuneCounter = 0;
for(int imm = 0; imm < numOfPopulation; imm++){
if(population[imm].immune){
immuneCounter++;
}
}
//////////////// Display in left corner /////////////////
translate(2,60);
fill(64,200);
rect(0,0,160,90);
fill(255);
float perc = float(touchedCounter)/numOfPopulation*100;
text("Infected: " + touchedCounter + " of " + numOfPopulation, 15,20);
text("Percent Infected: " + round(perc), 15,40);
text("Recovered / Immune: " + immuneCounter, 15,60);
text("Timer: " + frameCount,15,80);
noStroke();
fill(255,0,0);
rect(4,12,8,8);
fill(0,255,0,100);
rect(4,51,8,8);
translate(-2,-60);
fill(255);
line(mary.position.x-4,mary.position.y-4, mary.position.x+4, mary.position.y+4);
if(frameCount > whenMaryImmune){ // HOW LONG IT TAKES BEFORE MARY IS NOT SPREADING VIRUS
mary.immune = true;
}
/////////////////////
for(int w2 = 0; w2 < walls.length; w2++){
walls[w2].display();
} // end display walls loop
if(frameCount % 10 ==0){
int graphCounter = int(map(touchedCounter, 0, numOfPopulation, 0, 140));
graph.addDataPoint(frameCount/10, -graphCounter);
}
if(frameCount % 10 ==0){
int graphCounter2 = int(map(immuneCounter, 0, numOfPopulation, 0, 140));
graph2.addDataPoint(frameCount/10, -graphCounter2);
}
graph.display(200,0,0);
graph2.display(0,200,0);
} // end of draw
int block(float boxX, float boxY, float boxw, float boxh, PVector hostLocation){
if((hostLocation.x >= boxX && hostLocation.x <= boxX + boxw) && (hostLocation.y >= boxY && hostLocation.y <= boxY+ boxh)){
//background(0,100,255);
return (-1);
} else {
return (1);
}
} // end of block