-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
192 lines (161 loc) · 4.57 KB
/
Copy pathmain.cpp
File metadata and controls
192 lines (161 loc) · 4.57 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
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Rectangle.h"
#include "Matrix.h"
#define LOG(x) std::cout << x << std::endl
#include <math.h>
#include "Math.h"
#include "Timing.h"
int screenWidth = 800;
int screenHeight = 800;
sf::RenderWindow app(sf::VideoMode(screenWidth, screenHeight), "Disease Simulator", sf::Style::Close);
Math math;
int const rectangles = 150;
Rectangle recs[rectangles];
int n = 1;
int const numberOfRotations = 50;
double norm[numberOfRotations] = {};
int rot = 0;
float initialSpeed = 0.2;
float immunityRate = 4;
float deathRate = 2.2;
#include <fstream>
std::ofstream data("data.csv");
void init()
{
LOG("Yee haw howdy partner");
for (int i=0; i<rectangles; i++)
{
recs[i] = Rectangle(rand()%(50+(screenWidth-50)), rand()%(50+(screenHeight-50)), 20, 20, sf::Color(200,110,120));
float xvel = rand()%10;
float yvel = rand()%10;
recs[i].setVelocity(0.01-xvel/(50/initialSpeed), 0.01-yvel/(50/initialSpeed));
}
math.normal(numberOfRotations, norm);
recs[6].changeCondition(recs[6].sick);
data << "Time,Susceptible,Sick,Immune,Dead,\n";
}
float sneezeSpeed = 0.2;
bool animationForRecI[rectangles];
void sneezeAnimation(int x, int y, float sneezeRadius, int i)
{
recs[i].sneezeRadius += sneezeSpeed;
if (sneezeRadius < 100)
{
sf::CircleShape circle(sneezeRadius);
circle.setFillColor(sf::Color(200,255,200,200-sneezeRadius*2));
circle.setPosition(sf::Vector2f(x-sneezeRadius, y-sneezeRadius));
app.draw(circle);
}
else
{
animationForRecI[i] = false;
recs[i].sneezeRadius = 0;
}
}
void update()
{
for (int i=0;i<100;i++)
{
if (recs[i].condition == recs[0].sick && rand()%2000 == 1)
{
recs[i].sneeze(100, rectangles, recs);
animationForRecI[i] = true;
if (recs[i].health > 0) {recs[i].health -= deathRate;}
else {
recs[i].changeCondition(Rectangle::dead);
}
}
if (animationForRecI[i] == true)
{
sneezeAnimation(recs[i].x, recs[i].y, recs[i].sneezeRadius, i);
}
if (recs[i].condition == Rectangle::sick && rand()%2000 == 1)
{
recs[i].immunity += immunityRate;
if (recs[i].immunity >= 10)
{
recs[i].changeCondition(Rectangle::immune);
}
}
}
for (int i=0; i<rectangles; i++)
{
recs[i].Move(recs[i].velVector[0],recs[i].velVector[1]);
app.draw(recs[i].getRect());
recs[i].avoidEdge(screenWidth, screenHeight);
}
if (n%20 == 0)
{
int X = rand()%rectangles;
recs[X].randomRotation(3, norm[rot]);
if (rot < numberOfRotations){rot++;} else{rot=0;}
}
if (n%50 == 0)
{
int statistics[4];
for (int i = 0; i<rectangles; i++)
{
statistics[0]+=(recs[i].condition == Rectangle::suceptible);
statistics[1]+=(recs[i].condition == Rectangle::sick);
statistics[2]+=(recs[i].condition == Rectangle::immune);
statistics[3]+=(recs[i].condition == Rectangle::dead);
}
//This if statement prevents a bug where there's like 30000 recorded immune people.
if (statistics[3] < rectangles)
{
data << n/50;
data << ",";
for (int i = 0; i<4; i++)
{
data << statistics[i];
data << ",";
}
}
data << "\n";
}
n++;
}
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
//This copy-paste code somehow makes the rest of the simulation non-deterministic. Don't ask why.
int test()
{
unsigned long j;
srand( (unsigned)time(NULL) );
for( j = 0; j < 100; ++j )
{
int n;
/* skip rand() readings that would make n%6 non-uniformly distributed
(assuming rand() itself is uniformly distributed from 0 to RAND_MAX) */
while( ( n = rand() ) > RAND_MAX - (RAND_MAX-5)%6 )
{ /* bad value retrieved so get next one */ }
}
return 0;
}
#include <string>
int main()
{
test();
init();
while (app.isOpen())
{
app.clear();
update();
// Process events
sf::Event event;
while (app.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
{
app.close();
data.close();
}
}
// Update the window
app.display();
}
return EXIT_SUCCESS;
}