d7caa9d9853647dc9dc28ce7f254cd34c90304db
[universe.git] / polygon.h
1
2 ////////////////////////////////////////////////////////////////////////////////
3 // This program is free software; you can redistribute it and/or              //
4 // modify it under the terms of the GNU General Public License                //
5 // version 2 as published by the Free Software Foundation.                    //
6 //                                                                            //
7 // This program is distributed in the hope that it will be useful, but        //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of                 //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU          //
10 // General Public License for more details.                                   //
11 //                                                                            //
12 // Written and (C) by François Fleuret                                        //
13 // Contact <francois.fleuret@epfl.ch> for comments & bug reports              //
14 ////////////////////////////////////////////////////////////////////////////////
15
16 #ifndef POLYGON_H
17 #define POLYGON_H
18
19 #include "misc.h"
20 #include "simple_window.h"
21
22 class Polygon {
23   struct Triangle {
24     int a, b, c;
25   };
26
27   int _nb_max_polygons;
28   int _nb_polygons; // We need to know the total number of polygons in
29                     // the universe
30
31   scalar_t _mass, _moment_of_inertia, _radius;
32
33   scalar_t *_relative_x, *_relative_y;
34   scalar_t _last_center_x, _last_center_y, _last_theta;
35
36   int _total_nb_dots;
37   int *_nb_dots;
38   int *_effecting_edge;
39
40   scalar_t *_length;
41   Triangle *_triangles;
42
43   inline void intersection(scalar_t xa2, scalar_t ya2,
44                            scalar_t xa1, scalar_t ya1,
45                            scalar_t xb2, scalar_t yb2,
46                            scalar_t xb1, scalar_t yb1,
47                            scalar_t &det, scalar_t &s1, scalar_t &s2) {
48     scalar_t m11, m12, m21, m22, n11, n12, n21, n22, v1, v2;
49     m11 = xa1 - xa2; m12 = xb2 - xb1; m21 = ya1 - ya2; m22 = yb2 - yb1;
50     det = m11 * m22 - m12 * m21;
51     if(det != 0) {
52       n11 = + m22 / det; n12 = - m12 / det; n21 = - m21 / det; n22 = + m11 / det;
53       v1 = xb2 - xa2; v2 = yb2 - ya2;
54       s1 = n11 * v1 + n12 * v2; s2 = n21 * v1 + n22 * v2;
55     }
56   }
57
58 public:
59   bool _initialized;
60   bool _nailed;
61   int _nb_vertices;
62   scalar_t *_x, *_y;
63   scalar_t _red, _green, _blue;
64   scalar_t _center_x, _center_y, _theta;
65   scalar_t _dcenter_x, _dcenter_y, _dtheta;
66
67   // The x and y parameters are ignored if null. Useful to initialize
68   // directly.
69
70   Polygon(scalar_t mass,
71           scalar_t red, scalar_t green, scalar_t blue,
72           scalar_t *x, scalar_t *y,
73           int nv);
74
75   ~Polygon();
76
77   Polygon *clone();
78
79   void print_fig(ostream &os);
80   void draw(SimpleWindow *window);
81   void draw_contours(SimpleWindow *window);
82   void set_vertex(int k, scalar_t x, scalar_t y);
83   void set_position(scalar_t center_x, scalar_t center_y, scalar_t theta);
84   void set_speed(scalar_t dcenter_x, scalar_t dcenter_y, scalar_t dtheta);
85   bool contain(scalar_t x, scalar_t y);
86   void triangularize(int &nt, int nb, int *index);
87   void initialize(int nb_polygons);
88   bool update(scalar_t dt);
89   scalar_t relative_x(scalar_t ax, scalar_t ay);
90   scalar_t relative_y(scalar_t ax, scalar_t ay);
91   scalar_t absolute_x(scalar_t rx, scalar_t ry);
92   scalar_t absolute_y(scalar_t rx, scalar_t ry);
93
94   void apply_force(scalar_t dt, scalar_t x, scalar_t y, scalar_t fx, scalar_t fy);
95   void apply_border_forces(scalar_t dt, scalar_t xmax, scalar_t ymax);
96   void apply_collision_forces(scalar_t dt, int n_polygon, Polygon *p);
97
98   bool collide(Polygon *p);
99 };
100
101 #endif