-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
109 lines (92 loc) · 3.2 KB
/
main.cpp
File metadata and controls
109 lines (92 loc) · 3.2 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
#include "cstdlib"
#include "sfml.hpp"
int main() {
std::vector<Boid> flock;
int n;
double s;
double a;
double c;
// Input control: boids' number
while (std::cout << "Please, insert the number of boids (it must be an "
"integer greater than 2,\nand can be changed during the "
"simulation): " &&
!(std::cin >> n)) {
std::cin.clear();
std::string input;
std::getline(std::cin, input);
std::cerr << "Input \"" << input << "\" is invalid.\n";
// Ctrl+D control
if (std::cin.eof()) {
std::cerr << "Unable to read input. EOF reached.\n";
return EXIT_FAILURE;
}
}
// Progam is terminated if the number given is < 2 or if it is not an integer
if (std::cin.fail() || n < 2) {
throw std::runtime_error{
"The chosen value must be an integer number greater than 2!"};
}
std::cout
<< "Please, insert parameters of separation, alignment and cohesion - "
"respectevly.\nSuggested values are s = 0.07, a = 0.06, c = 0.015 "
"(they must be positive real numbers,\nand can be changed during the "
"simulation):\n";
// Input control: parameter s
while (std::cout << "Parameter s: " && !(std::cin >> s)) {
std::cin.clear();
std::string input;
std::getline(std::cin, input);
std::cerr << "Input \"" << input << "\" is invalid.\n";
// Ctrl+D control
if (std::cin.eof()) {
std::cerr << "Unable to read input. EOF reached.\n";
return EXIT_FAILURE;
}
}
// Input control: parameter a
while (std::cout << "Parameter a: " && !(std::cin >> a)) {
std::cin.clear();
std::string input;
std::getline(std::cin, input);
std::cerr << "Input \"" << input << "\" is invalid.\n";
// Ctrl+D control
if (std::cin.eof()) {
std::cerr << "Unable to read input. EOF reached.\n";
return EXIT_FAILURE;
}
}
// Input control: parameter c
while (std::cout << "Parameter c: " && !(std::cin >> c)) {
std::cin.clear();
std::string input;
std::getline(std::cin, input);
std::cerr << "Input \"" << input << "\" is invalid.\n";
// Ctrl+D control
if (std::cin.eof()) {
std::cerr << "Unable to read input. EOF reached.\n";
return EXIT_FAILURE;
}
}
// Progam is terminated if parameters are negative (otherwise it would be
// senseless to refer to flight rules when building the simulation) or if cin
// has failed
if (std::cin.fail() || s < 0 || a < 0 || c < 0) {
throw std::runtime_error{
"The parameters must be non-negative real numbers!"};
}
std::cout << "\nThe simulation is starting, press \"H\" for help...\n\n";
Stats st{1., 20., s, a, c, 0., 100., 100., 0., 12.5, 10., 2.};
// Gaussian generator for boids
std::default_random_engine generator;
std::normal_distribution<double> pos_d(st.u_b / 2., st.u_b * 2.);
std::normal_distribution<double> vel_d(0., st.v_max / 2.);
// Creating predator
Predator p{{st.r_b / 4., st.u_b / 4.}, {0., 0.}};
for (int i{}; i != n; ++i) {
Boid b_i{{pos_d(generator), pos_d(generator)},
{vel_d(generator), vel_d(generator)}};
flock.push_back(b_i);
}
// sfml window is rendered
run_simulation(flock, p, st);
}