4 flatland is a simple 2d physical simulator
6 Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
7 Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 This file is part of flatland
11 flatland is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License version 3 as
13 published by the Free Software Foundation.
15 flatland is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with flatland. If not, see <http://www.gnu.org/licenses/>.
32 #include "xfig_tracer.h"
36 #include "simple_window.h"
45 int _nb_polygons; // We need to know the total number of polygons in
48 scalar_t _mass, _moment_of_inertia, _radius;
50 scalar_t *_relative_x, *_relative_y;
51 scalar_t _last_center_x, _last_center_y, _last_theta;
60 inline void intersection(scalar_t xa2, scalar_t ya2,
61 scalar_t xa1, scalar_t ya1,
62 scalar_t xb2, scalar_t yb2,
63 scalar_t xb1, scalar_t yb1,
64 scalar_t &det, scalar_t &s1, scalar_t &s2) {
65 scalar_t m11, m12, m21, m22, n11, n12, n21, n22, v1, v2;
66 m11 = xa1 - xa2; m12 = xb2 - xb1; m21 = ya1 - ya2; m22 = yb2 - yb1;
67 det = m11 * m22 - m12 * m21;
69 n11 = + m22 / det; n12 = - m12 / det; n21 = - m21 / det; n22 = + m11 / det;
70 v1 = xb2 - xa2; v2 = yb2 - ya2;
71 s1 = n11 * v1 + n12 * v2; s2 = n21 * v1 + n22 * v2;
80 scalar_t _red, _green, _blue;
81 scalar_t _center_x, _center_y, _theta;
82 scalar_t _dcenter_x, _dcenter_y, _dtheta;
84 // The x and y parameters are ignored if null. Useful to initialize
87 Polygon(scalar_t mass,
88 scalar_t red, scalar_t green, scalar_t blue,
89 scalar_t *x, scalar_t *y,
97 void color_xfig(XFigTracer *tracer);
98 void print_xfig(XFigTracer *tracer);
102 void draw(SimpleWindow *window);
103 void draw_contours(SimpleWindow *window);
106 void draw(Canvas *canvas);
107 void draw_contours(Canvas *canvas);
109 void set_vertex(int k, scalar_t x, scalar_t y);
110 void set_position(scalar_t center_x, scalar_t center_y, scalar_t theta);
111 void set_speed(scalar_t dcenter_x, scalar_t dcenter_y, scalar_t dtheta);
112 bool contain(scalar_t x, scalar_t y);
113 void triangularize(int &nt, int nb, int *index);
114 void initialize(int nb_polygons);
115 bool update(scalar_t dt);
116 scalar_t relative_x(scalar_t ax, scalar_t ay);
117 scalar_t relative_y(scalar_t ax, scalar_t ay);
118 scalar_t absolute_x(scalar_t rx, scalar_t ry);
119 scalar_t absolute_y(scalar_t rx, scalar_t ry);
121 void apply_force(scalar_t dt, scalar_t x, scalar_t y, scalar_t fx, scalar_t fy);
122 void apply_border_forces(scalar_t dt, scalar_t xmin, scalar_t ymin, scalar_t xmax, scalar_t ymax);
123 void apply_collision_forces(scalar_t dt, int n_polygon, Polygon *p);
125 bool collide_with_borders(scalar_t xmin, scalar_t ymin,
126 scalar_t xmax, scalar_t ymax);
128 bool collide(Polygon *p);