Repository navigation
Expand file tree
/
Copy pathContagionSimulation.java
More file actions
246 lines (208 loc) · 7.89 KB
/
Copy pathContagionSimulation.java
File metadata and controls
246 lines (208 loc) · 7.89 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import processing.core.*;
import java.util.ArrayList;
public class ContagionSimulation extends PApplet {
public static final int ballAmount = 50;
public float diameter = 20;
public static ArrayList<Ball> balls;
public static ArrayList<Integer> infectionData = new ArrayList<>();
public static ArrayList<Integer> immunityData = new ArrayList<>();
public static ArrayList<Integer> neutralData = new ArrayList<>();
public float spreadChance = 75;
public float spreadChanceImmune = 30;
public float immuneChance = 65;
public float immuneChanceSpread = 15;
public static int infected;
public static int immune;
public static int neutral;
public static DataStore dataStore = new DataStore();
double start;
boolean runtimeCheck = true;
public void settings() {
size(600, 600);
}
public void setup() {
String[] graphArgs = {"GraphingWindow"};
SecondApplet graphingWindow = new SecondApplet();
PApplet.runSketch(graphArgs, graphingWindow);
String[] graphArgs2 = {"ImmuneGraphingWindow"};
ThirdApplet immuneGraphingWindow = new ThirdApplet();
PApplet.runSketch(graphArgs2, immuneGraphingWindow);
String[] graphArgs3 = {"NeutralGraphingWindow"};
FourthApplet neutralGraphingWindow = new FourthApplet();
PApplet.runSketch(graphArgs3, neutralGraphingWindow);
background(255);
balls = new ArrayList<>();
initBalls();
}
public void draw() {
if(runtimeCheck == true) {
start = System.currentTimeMillis();
runtimeCheck = false;
}
background(255);
for (Ball ball : balls) {
ball.move();
ball.generateGraphics();
}
if(checkVictory() != null) {
long winKey = dataStore.findNextWinKey();
long timeKey = dataStore.findNextTimeKey();
double runtime = (System.currentTimeMillis() - start)/1000;
// System.out.println(winKey);
// System.out.println(timeKey);
dataStore.putWin(winKey, checkVictory());
dataStore.putTime(timeKey, runtime);
outputData();
// System.out.println(dataStore.getTime(timeKey));
// System.out.println(dataStore.getWin(winKey));
exit();
}
}
void initBalls() {
for (int i = 0; i < ballAmount; i++) {
PVector pos;
boolean overlap;
do {
overlap = false;
pos = new PVector(random((diameter / 2) + 1, width - ((diameter / 2) + 1)), random((diameter / 2) + 1, height - ((diameter / 2) + 1)));
for (Ball existingBall : balls) {
float distance = dist(pos.x, pos.y, existingBall.pos.x, existingBall.pos.y);
if (distance < (diameter / 2 + existingBall.diam / 2)) {
overlap = true;
break;
}
}
} while (overlap);
PVector vel = PVector.random2D().mult(2);
if (i == 0) {
balls.add(new Ball(pos, vel, diameter, diameter, true, false));
} else if (i == 1) {
balls.add(new Ball(pos, vel, diameter, diameter, false, true));
} else {
balls.add(new Ball(pos, vel, diameter, diameter, false, false));
}
}
}
static String checkVictory() {
int immuneCounter = 0;
int infectedCounter = 0;
for(Ball ball : balls) {
if(ball.immunity == true) {
immuneCounter++;
} else if (ball.status == true) {
infectedCounter++;
}
}
if(immuneCounter == ballAmount) {
return "Immune";
} else if (infectedCounter == ballAmount) {
return "Infected";
}
return null;
}
void outputData() {
System.out.println("The average runtime of this simulation is: " + dataStore.averageRuntime() + " seconds.");
System.out.println("Immune ball win percentage: " + dataStore.immuneWinPercentage() + "%");
System.out.println("Infected ball win percentage: " + dataStore.infectedWinPercentage() + "%");
}
class Ball {
PVector pos;
PVector vel;
float diam;
boolean status;
boolean immunity;
Ball(PVector pos, PVector vel, float diam, float diameter, boolean status, boolean immunity) {
this.pos = pos;
this.vel = vel;
this.diam = diam;
this.status = status;
this.immunity = immunity;
}
void move() {
pos.add(vel);
if (pos.x - (diam / 2) < 0) {
pos.x = diam / 2;
vel.x *= -1;
} else if (pos.x + (diam / 2) > width) {
pos.x = width - diam / 2;
vel.x *= -1;
}
if (pos.y - (diam / 2) < 0) {
pos.y = diam / 2;
vel.y *= -1;
} else if (pos.y + (diam / 2) > height) {
pos.y = height - diam / 2;
vel.y *= -1;
}
for (Ball otherBall : balls) {
if (otherBall != this && collisionChecker(otherBall)) {
float overlap = (diam / 2 + otherBall.diam / 2) - dist(pos.x, pos.y, otherBall.pos.x, otherBall.pos.y);
PVector unstuck = PVector.sub(pos, otherBall.pos).setMag(overlap / 2);
pos.add(unstuck);
otherBall.pos.sub(unstuck);
PVector saver = vel;
vel = otherBall.vel;
otherBall.vel = saver;
if (spread() && otherBall.status && !this.status && this.immunity == false) {
status = true;
} else if (spreadToImmune() && otherBall.status && this.status == false && this.immunity == true) {
status = false;
immunity = false;
} else if (spreadImmune() && otherBall.status == false && this.status == false && this.immunity == false && otherBall.immunity == true) {
immunity = true;
} else if (spreadToInfected() && otherBall.status == false && this.status && this.immunity == false && otherBall.immunity == true) {
immunity = false;
status = false;
}
}
}
}
void generateGraphics() {
if (status) {
fill(238, 75, 43);
} else if (immunity) {
fill(50,205,50);
} else {
fill(30, 144, 255);
}
ellipse(pos.x, pos.y, diam, diam);
}
boolean spread() {
if (random(100) < spreadChance) {
return true;
} else {
return false;
}
}
boolean spreadToImmune() {
if(random(100) < spreadChanceImmune) {
return true;
} else {
return false;
}
}
boolean spreadImmune() {
if(random(100) < immuneChance) {
return true;
} else {
return false;
}
}
boolean spreadToInfected() {
if(random(100) < immuneChanceSpread) {
return true;
} else {
return false;
}
}
boolean collisionChecker(Ball otherBall) {
float distance = dist(pos.x, pos.y, otherBall.pos.x, otherBall.pos.y);
return distance < (diam / 2 + otherBall.diam / 2);
}
}
public static void main(String[] args) {
PApplet.main("ContagionSimulation", args);
// dataStore.wipeData();
dataStore.printDataStore();
}
}