Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2004:
[Freeciv-Dev] (PR#9043) map_vector_to_sq_distance function
Home

[Freeciv-Dev] (PR#9043) map_vector_to_sq_distance function

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#9043) map_vector_to_sq_distance function
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 9 Jul 2004 21:28:16 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=9043 >

To help clarify the naming situation, I have made similar functions for
the other distance functions.

  map_distance      => map_vector_to_distance
  real_map_distance => map_vector_to_real_distance
  map_distance_sq   => map_vector_to_distance_sq

"map" here is a pretty overused term.  It could just mean the distance
is on the map, or it could mean that the parameters are in "map"
coordinates.

The first function, map_vector_to_distance, I made static.  It should
never be used, and is included only for completeness.  (Of course I want
to remove map_distance entirely, but that's another issue.)

The second function, map_vector_to_real_distance, I made public even
though it doesn't have any users yet.  I plan to use this function to
make a hex-friendly and much simpler version of iterate_outward.  There
may be other potential users out there as well.

The third function, map_vector_to_distance_sq, is used in the creation
of the citymap indices for city_map_iterate_outward, and also for the
check in is_valid_city_pos.

The naming may still be up for discussion.  However I think that these
names should be consistent, so that the three functions in each column
(from above) are consistent and the two functions in each row are also
consistent.  You could argue that the existing three functions are not
named consistently and should all begin with map_.

jason

Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.225
diff -u -r1.225 city.c
--- common/city.c       9 Jul 2004 19:40:23 -0000       1.225
+++ common/city.c       10 Jul 2004 04:21:10 -0000
@@ -45,6 +45,9 @@
 **************************************************************************/
 bool is_valid_city_coords(const int city_x, const int city_y)
 {
+  int dist = map_vector_to_sq_distance(city_x - CITY_MAP_RADIUS,
+                                      city_y - CITY_MAP_RADIUS);
+
   /* The city's valid positions are in a circle of radius CITY_MAP_RADIUS
    * around the city center.  Depending on the value of CITY_MAP_RADIUS
    * this circle will be:
@@ -61,13 +64,7 @@
    *
    * FIXME: this won't work for hexagonal tiles.
    */
-  if (CITY_MAP_RADIUS * CITY_MAP_RADIUS + 1
-      >= ((city_x - CITY_MAP_RADIUS) * (city_x - CITY_MAP_RADIUS) +
-         (city_y - CITY_MAP_RADIUS) * (city_y - CITY_MAP_RADIUS))) {
-    return TRUE;
-  } else {
-    return FALSE;
-  }
+  return (CITY_MAP_RADIUS * CITY_MAP_RADIUS + 1 >= dist);
 }
 
 /**************************************************************************
@@ -200,7 +197,7 @@
       if (is_valid_city_coords(dx + CITY_MAP_RADIUS, dy + CITY_MAP_RADIUS)) {
        array[i].dx = dx;
        array[i].dy = dy;
-       array[i].dist = dx * dx + dy * dy;
+       array[i].dist = map_vector_to_sq_distance(dx, dy);
        i++;
       }
     }
Index: common/map.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.c,v
retrieving revision 1.176
diff -u -r1.176 map.c
--- common/map.c        9 Jul 2004 19:30:58 -0000       1.176
+++ common/map.c        10 Jul 2004 04:21:10 -0000
@@ -452,6 +452,31 @@
   return NULL;
 }
 
+/****************************************************************************
+  Return the "distance" (which is really the Manhattan distance, and should
+  rarely be used) for a given vector.
+****************************************************************************/
+static int map_vector_to_distance(int dx, int dy)
+{
+  return abs(dx) + abs(dy);
+}
+
+/****************************************************************************
+  Return the "real" distance for a given vector.
+****************************************************************************/
+int map_vector_to_real_distance(int dx, int dy)
+{
+  return MAX(abs(dx), abs(dy));
+}
+
+/****************************************************************************
+  Return the sq_distance for a given vector.
+****************************************************************************/
+int map_vector_to_sq_distance(int dx, int dy)
+{
+  return dx * dx + dy * dy;
+}
+
 /***************************************************************
 ...
 ***************************************************************/
@@ -460,8 +485,7 @@
   int dx, dy;
 
   map_distance_vector(&dx, &dy, x0, y0, x1, y1);
-
-  return MAX(abs(dx), abs(dy));
+  return map_vector_to_real_distance(dx, dy);
 }
 
 /***************************************************************
@@ -474,8 +498,7 @@
   int dx, dy;
 
   map_distance_vector(&dx, &dy, x0, y0, x1, y1);
-
-  return (dx*dx + dy*dy);
+  return map_vector_to_sq_distance(dx, dy);
 }
 
 /***************************************************************
@@ -488,8 +511,7 @@
   int dx, dy;
 
   map_distance_vector(&dx, &dy, x0, y0, x1, y1);
-
-  return abs(dx) + abs(dy);
+  return map_vector_to_distance(dx, dy);
 }
 
 /***************************************************************
Index: common/map.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.h,v
retrieving revision 1.192
diff -u -r1.192 map.h
--- common/map.h        9 Jul 2004 19:30:58 -0000       1.192
+++ common/map.h        10 Jul 2004 04:21:11 -0000
@@ -198,9 +198,12 @@
 const char *map_get_tile_fpt_text(int x, int y);
 struct tile *map_get_tile(int x, int y);
 
+int map_vector_to_real_distance(int dx, int dy);
+int map_vector_to_sq_distance(int dx, int dy);
 int map_distance(int x0, int y0, int x1, int y1);
 int real_map_distance(int x0, int y0, int x1, int y1);
 int sq_map_distance(int x0, int y0, int x1, int y1);
+
 bool same_pos(int x1, int y1, int x2, int y2);
 bool base_get_direction_for_step(int start_x, int start_y, int end_x,
                                int end_y, int *dir);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#9043) map_vector_to_sq_distance function, Jason Short <=