-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.cc
86 lines (71 loc) · 1.75 KB
/
simulation.cc
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
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <iostream>
#include "SDL/SDL.h"
#include "Particle.h"
#include "CollisionSystem.h"
int
main(int argc, char* argv[])
{
if (argc > 2)
{
cout << "Usage:" << endl << argv[0] << " N" << endl << argv[0] << " < input.txt" << endl;
return 0;
}
// we only need SDL video
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
// register SDL_Quit to be called at exit
atexit(SDL_Quit);
// set window title
SDL_WM_SetCaption("Colliding particles", 0);
// Set the screen up
SDL_Surface* screen = SDL_SetVideoMode(512, 512, 32, SDL_SWSURFACE);
if (screen == 0)
{
fprintf(stderr, "Unable to set 512x512 video: %s\n", SDL_GetError());
exit(1);
}
// the array of particles
vector<Particle> particles;
if (argc == 1)
{
// read particles from cin
int N;
double rx;
double ry;
double vx;
double vy;
double radius;
double mass;
unsigned char r;
unsigned char g;
unsigned char b;
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
scanf("%lE %lE %lE %lE %lE %lE %hhu %hhu %hhu", &rx, &ry, &vx, &vy, &radius, &mass, &r, &g, &b);
particles.push_back(Particle(rx, ry, vx, vy, radius, mass, r, g, b));
}
}
if (argc == 2)
{
// N is number of particles to simulate
int N = atoi(argv[1]);
// seed random number generator
srand(time(0));
// generate N random particles
for (int i = 0; i < N; i++)
{
particles.push_back(Particle());
}
}
// create collision system and simulate
CollisionSystem system(particles, screen);
system.simulate(10000);
return 0;
}