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. //
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. //
12 // Written and (C) by François Fleuret //
13 // Contact <francois.fleuret@epfl.ch> for comments & bug reports //
14 ////////////////////////////////////////////////////////////////////////////////
20 #include "simple_window.h"
28 int _nb_polygons; // We need to know the total number of polygons in
31 scalar_t _mass, _moment_of_inertia, _radius;
33 scalar_t *_relative_x, *_relative_y;
34 scalar_t _last_center_x, _last_center_y, _last_theta;
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;
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;
63 scalar_t _red, _green, _blue;
64 scalar_t _center_x, _center_y, _theta;
65 scalar_t _dcenter_x, _dcenter_y, _dtheta;
67 // The x and y parameters are ignored if null. Useful to initialize
70 Polygon(scalar_t mass,
71 scalar_t red, scalar_t green, scalar_t blue,
72 scalar_t *x, scalar_t *y,
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);
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);
98 bool collide(Polygon *p);