6eafd6ce8d2f919fa07cbabd8b2d14517589ad3d
[universe.git] / mash.cc
1 // -*- compile-command:"g++ -lX11 -Wall -g -O3 -o mash misc.o universe.o polygon.o mash.cc -L/usr/X11R6/lib/"; -*-
2
3 #include <iostream>
4 #include <fstream>
5 #include <cmath>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <errno.h>
10 #include <string.h>
11
12 using namespace std;
13
14 #include "misc.h"
15 #include "universe.h"
16
17 int main(int argc, char **argv) {
18   int nb_heur = 100;
19   int w = 1000, h = 1000;
20   Universe universe(nb_heur + 1, w, h);
21
22   {
23     int nb = 100;
24     scalar_t xs[nb], ys[nb];
25     for(int k = 0; k < nb; k++) {
26       scalar_t alpha, radius;
27       if(k < nb/2) {
28         alpha = - (M_PI / scalar_t(nb/2)) * k;
29         radius = 400;
30       } else {
31         alpha = - (M_PI / scalar_t(nb/2)) * (nb - k - 1);
32         radius = 350;
33       }
34       xs[k] = scalar_t(w)/2 + radius * cos(-alpha);
35       ys[k] = scalar_t(h)/2 + radius * sin(-alpha);
36     }
37     Polygon *p;
38     p = new Polygon(1.0, 0.5, 0.5, 0.5, xs, ys, nb);
39     p->set_position(scalar_t(w/2), scalar_t(h) * 0.75, 0);
40     p->set_speed(0, 0, 0);
41     universe.initialize(p);
42     universe.add_polygon(p);
43   }
44
45   scalar_t xs[] = { -30, 30, 30, -30 };
46   scalar_t ys[] = { -10, -10, 10, 10 };
47
48   Polygon *blocks[nb_heur];
49
50   for(int h = 0; h < nb_heur; h++) {
51     blocks[h] = new Polygon(1.0, 1.0, 1.0, 0.0, xs, ys, 4);
52     do {
53       blocks[h]->set_position(drand48() * w, drand48() * h/10, M_PI * drand48());
54       blocks[h]->set_speed(0, 0, 0);
55     } while(universe.collide(blocks[h]));
56     universe.initialize(blocks[h]);
57     universe.add_polygon(blocks[h]);
58   }
59
60   scalar_t dt = 0.01;
61
62   for(int n = 0; n < 1000; n++) {
63     for(int h = 0; h < nb_heur; h++) {
64       blocks[h]->apply_force(dt, blocks[h]->_center_x, blocks[h]->_center_y, 0, 10);
65     }
66     universe.update(dt);
67   }
68
69   ofstream os("/tmp/mash.fig");
70   universe.print_fig(os);
71   exit(0);
72 }