Update.
authorFrancois Fleuret <francois@fleuret.org>
Fri, 9 Sep 2016 19:09:16 +0000 (21:09 +0200)
committerFrancois Fleuret <francois@fleuret.org>
Fri, 9 Sep 2016 19:09:16 +0000 (21:09 +0200)
generate.cc

index b0dfd87..c338a1e 100644 (file)
@@ -38,76 +38,112 @@ void generate_png(Universe *universe, scalar_t scale, FILE *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();
-
-    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,
-    };
+  if(argc < 2 || argc > 3) {
+    cerr << argv[0] << " <nb pairs to generate> [<seed>]" << endl;
+    exit(1);
+  }
 
-    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);
+  int nb_pairs = atoi(argv[1]);
 
-    grabbed_polygon = universe->pick_polygon(hand_x, hand_y);
+  if(argc > 2) {
+    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 = drand48() * 0.8;
+          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;
+
+    {
+      char buffer[1024];
+      sprintf(buffer, "/tmp/universe_%03d_0.png", n);
+      FILE *file = fopen(buffer, "w");
+      generate_png(universe, scaling, file);
     }
 
-  } while(!grabbed_polygon);
-
-  {
-    FILE *file = fopen("universe1.png", "w");
-    generate_png(universe, 0.25, 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_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);
-  }
+    scalar_t dt = 0.1;
+    for(int i = 0; i < 500; 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);
+    }
 
-  {
-    FILE *file = fopen("universe2.png", "w");
-    generate_png(universe, 0.25, file);
+    {
+      char buffer[1024];
+      sprintf(buffer, "/tmp/universe_%03d_1.png", n);
+      FILE *file = fopen(buffer, "w");
+      generate_png(universe, scaling, file);
+    }
   }
 
   delete universe;