The printing function in Bridges was unable to cope with more than two
[sgt/puzzles] / grid.c
diff --git a/grid.c b/grid.c
index 7843111..dcc384a 100644 (file)
--- a/grid.c
+++ b/grid.c
@@ -50,7 +50,7 @@ void grid_free(grid *g)
 
 /* Used by the other grid generators.  Create a brand new grid with nothing
  * initialised (all lists are NULL) */
-static grid *grid_new()
+static grid *grid_new(void)
 {
     grid *g = snew(grid);
     g->faces = NULL;
@@ -76,13 +76,14 @@ static grid *grid_new()
  *
  * Combining gives: distance = determinant / line-length(a,b)
  */
-static double point_line_distance(int px, int py,
-                                  int ax, int ay,
-                                  int bx, int by)
+static double point_line_distance(long px, long py,
+                                  long ax, long ay,
+                                  long bx, long by)
 {
-    int det = ax*by - bx*ay + bx*py - px*by + px*ay - ax*py;
+    long det = ax*by - bx*ay + bx*py - px*by + px*ay - ax*py;
+    double len;
     det = max(det, -det);
-    double len = sqrt(SQ(ax - bx) + SQ(ay - by));
+    len = sqrt(SQ(ax - bx) + SQ(ay - by));
     return det / len;
 }
 
@@ -124,7 +125,7 @@ grid_edge *grid_nearest_edge(grid *g, int x, int y)
 
     for (;;) {
         /* Target to beat */
-        int dist = SQ(cur->x - x) + SQ(cur->y - y);
+        long dist = SQ((long)cur->x - (long)x) + SQ((long)cur->y - (long)y);
         /* Look for nearer dot - if found, store in 'new'. */
         grid_dot *new = cur;
         int i;
@@ -136,9 +137,10 @@ grid_edge *grid_nearest_edge(grid *g, int x, int y)
             int j;
             if (!f) continue;
             for (j = 0; j < f->order; j++) {
+               long new_dist;
                 grid_dot *d = f->dots[j];
                 if (d == cur) continue;
-                int new_dist = SQ(d->x - x) + SQ(d->y - y);
+                new_dist = SQ((long)d->x - (long)x) + SQ((long)d->y - (long)y);
                 if (new_dist < dist) {
                     new = d;
                     break; /* found closer dot */
@@ -155,23 +157,22 @@ grid_edge *grid_nearest_edge(grid *g, int x, int y)
             cur = new;
         }
     }
-    
     /* 'cur' is nearest dot, so find which of the dot's edges is closest. */
     best_edge = NULL;
 
     for (i = 0; i < cur->order; i++) {
         grid_edge *e = cur->edges[i];
-        int e2; /* squared length of edge */
-        int a2, b2; /* squared lengths of other sides */
+        long e2; /* squared length of edge */
+        long a2, b2; /* squared lengths of other sides */
         double dist;
 
         /* See if edge e is eligible - the triangle must have acute angles
          * at the edge's dots.
          * Pythagoras formula h^2 = a^2 + b^2 detects right-angles,
          * so detect acute angles by testing for h^2 < a^2 + b^2 */
-        e2 = SQ(e->dot1->x - e->dot2->x) + SQ(e->dot1->y - e->dot2->y);
-        a2 = SQ(e->dot1->x - x) + SQ(e->dot1->y - y);
-        b2 = SQ(e->dot2->x - x) + SQ(e->dot2->y - y);
+        e2 = SQ((long)e->dot1->x - (long)e->dot2->x) + SQ((long)e->dot1->y - (long)e->dot2->y);
+        a2 = SQ((long)e->dot1->x - (long)x) + SQ((long)e->dot1->y - (long)y);
+        b2 = SQ((long)e->dot2->x - (long)x) + SQ((long)e->dot2->y - (long)y);
         if (a2 >= e2 + b2) continue;
         if (b2 >= e2 + a2) continue;
          
@@ -185,9 +186,9 @@ grid_edge *grid_nearest_edge(grid *g, int x, int y)
          * Alternatively, we could check that the angle at the point is obtuse.
          * That would amount to testing a circular region with the edge as
          * diameter. */
-        dist = point_line_distance(x, y,
-                                   e->dot1->x, e->dot1->y,
-                                   e->dot2->x, e->dot2->y);
+        dist = point_line_distance((long)x, (long)y,
+                                   (long)e->dot1->x, (long)e->dot1->y,
+                                   (long)e->dot2->x, (long)e->dot2->y);
         /* Is dist more than half edge length ? */
         if (4 * SQ(dist) > e2)
             continue;
@@ -642,8 +643,14 @@ static grid_dot *grid_dot_add_new(grid *g, int x, int y)
  * Assumes g->dots has enough capacity allocated */
 static grid_dot *grid_get_dot(grid *g, tree234 *dot_list, int x, int y)
 {
-    grid_dot test = {0, NULL, NULL, x, y};
-    grid_dot *ret = find234(dot_list, &test, NULL);
+    grid_dot test, *ret;
+
+    test.order = 0;
+    test.edges = NULL;
+    test.faces = NULL;
+    test.x = x;
+    test.y = y;
+    ret = find234(dot_list, &test, NULL);
     if (ret)
         return ret;