From: Kristian Høgsberg <krh@redhat.com>
Date: Wed, 24 May 2006 19:42:48 +0000 (-0400)
Subject: Add init function for springs and clean up grid init.
X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=1c5cfec7a396c39cfcdeadfcf48c2dd15ba21ea8;p=akamaru

Add init function for springs and clean up grid init.
---

diff --git a/akamaru.c b/akamaru.c
index d158f3e..eee05e2 100644
--- a/akamaru.c
+++ b/akamaru.c
@@ -42,6 +42,14 @@ spring_init (Spring *spring, Object *a, Object *b, double length)
   spring->length = length;
 }
 
+void
+string_init (String *string, Object *a, Object *b, double length)
+{
+  string->a = a;
+  string->b = b;
+  string->length = length;
+}
+
 void
 offset_spring_init (OffsetSpring *spring, Object *a, Object *b,
 		    double dx, double dy)
diff --git a/akamaru.h b/akamaru.h
index 81eeb85..1d4796c 100644
--- a/akamaru.h
+++ b/akamaru.h
@@ -88,6 +88,7 @@ void object_init (Object *object, double x, double y, double mass);
 void offset_spring_init (OffsetSpring *spring,
 			 Object *a, Object *b, double dx, double dy);
 void spring_init (Spring *spring, Object *a, Object *b, double length);
+void string_init (String *string, Object *a, Object *b, double length);
 
 void polygon_init (Polygon *p, int num_points, ...);
 void polygon_init_diamond (Polygon *polygon, double x, double y);
diff --git a/main.c b/main.c
index 3cef5e9..2423b4f 100644
--- a/main.c
+++ b/main.c
@@ -152,8 +152,9 @@ model_init_grid (Model *model)
     (num_ropes - 1) * num_rope_objects;
   const int string_length = 20;
   const int rope_offset = 20;
-  double x, y;
-  int i, j, index, string_index;
+  Object *object;
+  String *string;
+  int i, j;
 
   memset (model, 0, sizeof *model);
   model->objects = g_new (Object, num_objects);
@@ -169,27 +170,18 @@ model_init_grid (Model *model)
   model->offsets[0].dx = rope_offset;
   model->offsets[0].dy = 0;
 
+  object = model->objects;
+  string = model->strings;
   for (i = 0; i < num_ropes; i++) {
     for (j = 0; j < num_rope_objects; j++) {
-      x = 200 + i * rope_offset;
-      y = 40 + j * string_length;
-      index = i * num_rope_objects + j;
-      object_init (&model->objects[index], x, y, 1);
-
-      if (i + 1 < num_ropes) {
-	string_index = i * num_rope_objects + j;
-	model->strings[string_index].a = &model->objects[index];
-	model->strings[string_index].b = &model->objects[index + num_rope_objects];
-	model->strings[string_index].length = string_length;
-      }
+      object_init (object, 200 + i * rope_offset, 40 + j * string_length, 1);
 
-      if (j + 1 < num_rope_objects) {
-	string_index =
-	  (num_ropes - 1) * num_rope_objects + i * (num_rope_objects - 1) + j;
-	model->strings[string_index].a = &model->objects[index];
-	model->strings[string_index].b = &model->objects[index + 1];
-	model->strings[string_index].length = string_length;
-      }
+      if (i + 1 < num_ropes)
+	string_init (string++,
+		     object, object + num_rope_objects, string_length);
+      if (j + 1 < num_rope_objects)
+	string_init (string++, object, object + 1, string_length);
+      object++;
     }
 
     model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];