-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
executable file
·194 lines (156 loc) · 5.71 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var People = [];
var days = [];
var numInfections = [];
var numRemoved = [];
var numPeople = 1000;
var size = 3.0;
var maxAccel = 3.0;
var infectionRate = 0.3;
var infectionRadius = 3 * size;
var recoveryMin = 1 * 60;
var recoveryMax = 2 * 60;
var simX = 30;
var simY = 30;
var simWidth = 600;
var simHeight = 500;
var running = false;
function myFunction() {
running = false;
}
function setup() {
frameRate(240);
createCanvas(windowWidth, windowHeight);
background(51);
}
function draw() {
if (!running) {
infectionRate = document.getElementById("infectionSlider").value / 100;
simWidth = windowWidth * 5/12;
People = [];
for (var i = 0; i < numPeople; i++) {
append(People, new Person());
}
People[0].isInfected = 1;
People[0].dayInfected = frameCount;
days = [];
numInfections = [];
numRemoved = [];
currentNumInfections = 1;
currentNumRemoved = 0;
running = true;
}
background(30);
fill(0);
stroke(200);
strokeWeight(10);
fill(255);
rect(simX - size - 5, simY - size - 5, simWidth + 2 * size + 10, simHeight + 2 * size + 10);
for (var i = 0; i < numPeople; i++) {
People[i].render();
People[i].move();
if (People[i].catchInfection(People, numPeople, frameCount) == 1) {
currentNumInfections += 1;
}
if (People[i].checkRemoved(frameCount)) {
currentNumRemoved += 1;
currentNumInfections -= 1;
}
}
append(days, frameCount);
append(numInfections, currentNumInfections);
append(numRemoved, currentNumRemoved);
var newInfections = numInfections[frameCount - 1] - numInfections[frameCount - 61];
var newRemoved = numRemoved[frameCount - 1] - numRemoved[frameCount - 61];
graph(days, numInfections, numRemoved, numPeople, frameCount, simX + simWidth + 50, displayWidth - 50, simY + simHeight, simY);
fill(255);
textSize(25);
text("New Daily Cases: " + (newInfections + newRemoved), simX + simWidth + 75, simY + 50);
text("New Removed Cases: " + newRemoved, simX + simWidth + 75, simY + 85);
text("Susceptible: " + (numPeople - currentNumInfections - currentNumRemoved), simX + simWidth + 500, simY + 50);
text("Infected: " + currentNumInfections, simX + simWidth + 500, simY + 85);
text("Removed: " + currentNumRemoved, simX + simWidth + 500, simY + 120);
}
function Person() {
this.size = size;
this.position = createVector(random(simX, simX + simWidth), random(simY, simY + simHeight), 0);
this.velocity = createVector(0, 0, 0);
this.maxAccel = maxAccel;
this.isInfected = 0;
this.dayInfected = -1;
this.move = function () {
this.position = p5.Vector.add(this.position, this.velocity);
this.position.x = constrain(this.position.x, simX, simX + simWidth);
this.position.y = constrain(this.position.y, simY, simY + simHeight);
if (this.position.x == simX || this.position.x == simX + simWidth) {
this.velocity.x = -this.velocity.x;
}
if (this.position.y == simY || this.position.y == simY + simHeight) {
this.velocity.y = -this.velocity.y;
}
this.velocity = this.velocity.add(random(-1, 1), random(-1, 1), 0).normalize().mult(random(0, this.maxAccel));
};
this.render = function () {
if (this.isInfected == 1) {
fill(255, 0, 0);
} else if (this.isInfected == 0) {
fill(0, 255, 0);
} else {
fill(0, 0, 255);
}
strokeWeight(1);
stroke(10);
ellipse(this.position.x, this.position.y, this.size * 2, this.size * 2);
};
this.catchInfection = function (peopleList, lenPeopleList, day) {
if (this.isInfected == 0) {
for (var i = 0; i < lenPeopleList; i++) {
if (peopleList[i].isInfected == 1 && infectionRadius > (p5.Vector.sub(this.position, peopleList[i].position)).mag()) {
if (random(0, 1) < infectionRate) {
this.isInfected = 1;
this.dayInfected = day;
return 1;
}
}
}
}
return 0;
}
this.checkRemoved = function (day) {
if (this.isInfected == 1 && day - this.dayInfected > random(recoveryMin, recoveryMax)) {
console.log(day);
this.isInfected = 2;
return 1;
} else {
return 0;
}
}
}
function graph(xList, yList1, yList2, maxYVal, dataLength, xLeft, xRight, yBottom, yTop) {
fill(0);
rect(xLeft, yTop, (xRight - xLeft), (yBottom - yTop));
strokeWeight(2);
line(xLeft, yBottom, xLeft, yTop);
line(xLeft, yBottom, xRight, yBottom);
var xScale = max(xList) / (xRight - xLeft);
var yScale = 1.5 * maxYVal / (yTop - yBottom);
fill(0, 255, 0);
rect(xLeft, yTop + (yBottom - yTop) * (1 - 1 / 1.5), xRight - xLeft, (yBottom - yTop) / 1.5);
fill(0, 0, 255);
beginShape();
vertex(xLeft, yBottom);
for (var i = 0; i < dataLength; i++) {
vertex(xLeft + xList[i] / xScale, yBottom + (yList2[i] + yList1[i]) / yScale);
}
vertex(xRight, yBottom);
vertex(xLeft, yBottom);
endShape(CLOSE);
fill(255, 0, 0);
beginShape();
vertex(xLeft, yBottom);
for (var i = 0; i < dataLength; i++) {
vertex(xLeft + xList[i] / xScale, yBottom + yList1[i] / yScale);
}
vertex(xRight, yBottom);
vertex(xLeft, yBottom);
endShape(CLOSE);
}