void generate_png(Universe *universe, scalar_t scale, FILE *file) {
CanvasCairo canvas(scale, universe->width(), universe->height());
+ canvas.set_line_width(2.0);
universe->draw(&canvas);
canvas.write_png(file);
}
int main(int argc, char **argv) {
+ scalar_t world_width = 400;
+ scalar_t world_height = 400;
+ scalar_t grab_start_x = world_width * 0.5;
+ scalar_t grab_start_y = world_height * 0.75;
+ Universe *universe;
+ Polygon *grabbed_polygon;
+
+ if(argc < 2 || argc > 3) {
+ cerr << argv[0] << " <nb pairs to generate> [<dir> [<seed>]]" << endl;
+ exit(1);
+ }
+
+ int nb_pairs = atoi(argv[1]);
+
+ char dir[1024] = "/tmp/";
+
+ if(argc > 2) {
+ strncpy(dir, argv[2], sizeof(dir) / sizeof(char) - 1);
+ }
+
+ if(argc > 3) {
+ srand48(atoi(argv[1]));
+ }
+
+ universe = new Universe(10, world_width, world_height);
+
+ for(int n = 0; n < nb_pairs; n++) {
+ cout << "Example " << n << endl;
+
+ do {
+ universe->clear();
+
+ const int nb_attempts_max = 100;
+ int nb_attempts = 0;
+
+ for(int u = 0; u < 10; u++) {
+ Polygon *pol = 0;
+
+ nb_attempts = 0;
+
+ do {
+ scalar_t square_size = 80;
+
+ scalar_t x[] = {
+ - square_size * 0.5,
+ + square_size * 0.5,
+ + square_size * 0.5,
+ - square_size * 0.5,
+ };
+
+ scalar_t y[] = {
+ - square_size * 0.5,
+ - square_size * 0.5,
+ + square_size * 0.5,
+ + square_size * 0.5,
+ };
+
+ scalar_t delta = square_size / sqrt(2.0);
+ scalar_t object_center_x = delta + (world_width - 2 * delta) * drand48();
+ scalar_t object_center_y = delta + (world_height - 2 * delta) * drand48();
+ scalar_t red, green, blue;
+ red = 0.90;
+ green = red;
+ blue = red;
+ delete pol;
+ pol = new Polygon(0.5,
+ red, green, blue,
+ x, y, sizeof(x)/sizeof(scalar_t));
+ pol->set_position(object_center_x, object_center_y, M_PI * 2 * drand48());
+ pol->set_speed(0, 0, 0);
+ universe->initialize_polygon(pol);
+ nb_attempts++;
+ } while(nb_attempts < nb_attempts_max && universe->collide(pol));
+
+ if(nb_attempts == nb_attempts_max) {
+ delete pol;
+ u = 0;
+ universe->clear();
+ nb_attempts = 0;
+ } else {
+ universe->add_polygon(pol);
+ }
+ }
+
+ grabbed_polygon = universe->pick_polygon(grab_start_x, grab_start_y);
+ } while(!grabbed_polygon);
+
+ const scalar_t scaling = 0.16;
+
+ CanvasCairo grab_trace(scaling, world_width, world_height);
+
+ {
+ char buffer[1024];
+ sprintf(buffer, "%s/dyn_%06d_world_0.png", dir, n);
+ FILE *file = fopen(buffer, "w");
+ generate_png(universe, scaling, file);
+ fclose(file);
+ }
+
+ scalar_t grab_relative_x = grabbed_polygon->relative_x(grab_start_x, grab_start_y);
+ scalar_t grab_relative_y = grabbed_polygon->relative_y(grab_start_x, grab_start_y);
+ // scalar_t grab_previous_x = grab_start_x, grab_previous_y = grab_start_y;
+
+ {
+ int n = 36;
+ scalar_t xp[n], yp[n];
+ for(int k = 0; k < n; k++) {
+ scalar_t radius = 1/scaling;
+ scalar_t alpha = 2 * M_PI * scalar_t(k) / scalar_t(n);
+ xp[k] = grab_start_x + radius * cos(alpha);
+ yp[k] = grab_start_y + radius * sin(alpha);
+ }
+ grab_trace.set_drawing_color(0.0, 0.0, 0.0);
+ grab_trace.set_line_width(1.0);
+ grab_trace.draw_polygon(1, n, xp, yp);
+ }
+
+ const int nb_iterations = 250;
+ scalar_t dt = 0.1;
+ for(int i = 0; i < nb_iterations; i++) {
+ scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y);
+ scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y);
+ grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
+ universe->update(dt);
+
+ /*
+ {
+ scalar_t xp[2], yp[2];
+ xp[0] = grab_previous_x;
+ yp[0] = grab_previous_y;
+ xp[1] = xf;
+ yp[1] = yf;
+ grab_previous_x = xf;
+ grab_previous_y = yf;
+
+ grab_trace.set_drawing_color(0.0, 0.0, 0.0);
+ grab_trace.draw_polygon(1, 2, xp, yp);
+ }
+ */
+ }
+
+ {
+ char buffer[1024];
+ sprintf(buffer, "%s/dyn_%06d_world_1.png", dir, n);
+ FILE *file = fopen(buffer, "w");
+ generate_png(universe, scaling, file);
+ fclose(file);
+ }
+
+ {
+ char buffer[1024];
+ sprintf(buffer, "%s/dyn_%06d_grab.png", dir, n);
+ FILE *file = fopen(buffer, "w");
+ grab_trace.write_png(file);
+ fclose(file);
+ }
+ }
+
+ delete universe;
}