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 square_size = 100;
- scalar_t hand_x = world_width * 0.5;
- scalar_t hand_y = world_height * 0.5;
+ scalar_t grab_start_x = world_width * 0.5;
+ scalar_t grab_start_y = world_height * 0.75;
Universe *universe;
Polygon *grabbed_polygon;
- int nb_attempts = 0;
- srand48(atoi(argv[1]));
-
- do {
- universe = new Universe(10, world_width, world_height);
-
- // scalar_t object_center_x = world_width * 0.5;
- // scalar_t object_center_y = world_height * 0.5;
-
- scalar_t object_center_x = world_width * drand48();
- scalar_t object_center_y = world_height * drand48();
+ if(argc < 2 || argc > 3) {
+ cerr << argv[0] << " <nb pairs to generate> [<dir> [<seed>]]" << endl;
+ exit(1);
+ }
- scalar_t x[] = {
- - square_size * 0.5,
- + square_size * 0.5,
- + square_size * 0.5,
- - square_size * 0.5,
- };
+ int nb_pairs = atoi(argv[1]);
- scalar_t y[] = {
- - square_size * 0.5,
- - square_size * 0.5,
- + square_size * 0.5,
- + square_size * 0.5,
- };
+ char dir[1024] = "/tmp/";
- Polygon *pol = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
- pol->set_position(object_center_x, object_center_y, M_PI/3);
- pol->set_speed(0, 0, 0);
- universe->initialize_polygon(pol);
- universe->add_polygon(pol);
+ if(argc > 2) {
+ strncpy(dir, argv[2], sizeof(dir) / sizeof(char) - 1);
+ }
- grabbed_polygon = universe->pick_polygon(hand_x, hand_y);
+ if(argc > 3) {
+ srand48(atoi(argv[1]));
+ }
- nb_attempts++;
- if(nb_attempts > 1000) {
- cerr << "Could not initialize the universe with a grabbed polygon after 1000 attempts. Aborting().";
- abort();
+ 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);
}
- } while(!grabbed_polygon);
+ 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);
+ }
- {
- FILE *file = fopen("universe1.png", "w");
- generate_png(universe, 0.25, file);
- }
+ 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);
+ }
+ */
+ }
- scalar_t grab_relative_x = grabbed_polygon->relative_x(hand_x, hand_y);
- scalar_t grab_relative_y = grabbed_polygon->relative_y(hand_x, hand_y);
-
- scalar_t dt = 1.0;
- for(int i = 0; i < 10; 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);
- scalar_t force_x = (hand_x - xf) * 10;
- scalar_t force_y = (hand_y - yf) * 10;
- grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0);
- universe->update(dt);
- }
+ {
+ 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);
+ }
- {
- FILE *file = fopen("universe2.png", "w");
- generate_png(universe, 0.25, 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;