-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChart.pde
110 lines (99 loc) · 3.82 KB
/
Chart.pde
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
class Chart {
// Constants
final int X_POS = 200;
final int Y_POS = Constants.Gui.STANDARD_PADDING;
final int CHART_HEIGHT = Constants.View.TOP_LINE_POS - (2 * Constants.Gui.STANDARD_PADDING);
// Private variables
private CompletionCallback delegate;
private PShape healthyArea;
private PShape infectedArea;
private PShape recoveredArea;
private ArrayList<Integer> healthyPoints = new ArrayList<Integer>();
private ArrayList<Integer> infectedPoints = new ArrayList<Integer>();
private ArrayList<Integer> recoveredPoints = new ArrayList<Integer>();
private int chartWidth;
private int totalItemCount;
private int totalCalls;
private float xInterval;
private boolean completed;
Chart(int totalItemCount, int duration, CompletionCallback delegate) {
this.delegate = delegate;
this.chartWidth = Constants.View.SCREEN_WIDTH - (X_POS + Constants.Gui.STANDARD_PADDING);
this.totalItemCount = totalItemCount;
this.totalCalls = Constants.View.FRAME_RATE * duration;
this.xInterval = (float)chartWidth / (float)totalCalls;
this.completed = false;
}
void display(int healthyCount, int infectedCount, int recoveredCount, boolean gameOver) {
fill(Constants.Color.WHITE);
rect(X_POS, Y_POS, chartWidth, CHART_HEIGHT);
if (!gameOver) {
updateHealthyPoints(healthyCount);
updateInfectedPoints(infectedCount);
updateRecoveredPoints(recoveredCount);
}
updateHealthyShape();
updateInfectedShape();
updateRecoveredShape();
shape(healthyArea, X_POS, Y_POS);
shape(infectedArea, X_POS, Y_POS);
shape(recoveredArea, X_POS, Y_POS);
}
void updateHealthyShape() {
float xPos = 0;
healthyArea = createShape();
healthyArea.beginShape();
healthyArea.fill(Constants.Color.COVID_GREEN);
healthyArea.vertex(0, 0);
healthyArea.vertex(0, CHART_HEIGHT);
xPos = xInterval * healthyPoints.size() < chartWidth ? xInterval * healthyPoints.size() : chartWidth;
healthyArea.vertex(xPos, CHART_HEIGHT);
healthyArea.vertex(xPos, 0);
healthyArea.endShape(CLOSE);
if (xPos >= chartWidth && !completed) {
delegate.simulationComplete();
completed = true;
}
}
void updateInfectedShape() {
float xPos = 0;
infectedArea = createShape();
infectedArea.beginShape();
infectedArea.fill(Constants.Color.COVID_RED);
infectedArea.vertex(xPos, CHART_HEIGHT);
for(Integer point : infectedPoints) {
if (xPos < chartWidth) {
xPos += xInterval;
}
float yPos = CHART_HEIGHT - (((float)point.intValue() / totalItemCount) * CHART_HEIGHT);
infectedArea.vertex((int)xPos, (int)yPos);
}
infectedArea.vertex(xPos, CHART_HEIGHT);
infectedArea.endShape(CLOSE);
}
void updateRecoveredShape() {
float xPos = 0;
recoveredArea = createShape();
recoveredArea.beginShape();
recoveredArea.fill(Constants.Color.COVID_PURPLE);
recoveredArea.vertex(xPos, 0);
for(Integer point : recoveredPoints) {
if (xPos < chartWidth) {
xPos += xInterval;
}
float yPos = ((float)point.intValue() / totalItemCount) * CHART_HEIGHT;
recoveredArea.vertex((int)xPos, (int)yPos);
}
recoveredArea.vertex(xPos, 0);
recoveredArea.endShape(CLOSE);
}
void updateHealthyPoints(int healthyCount) {
healthyPoints.add(new Integer(healthyCount));
}
void updateInfectedPoints(int infectedCount) {
infectedPoints.add(new Integer(infectedCount));
}
void updateRecoveredPoints(int recoveredCount) {
recoveredPoints.add(new Integer(recoveredCount));
}
}