Complete.Org: Mailing Lists: Archives: freeciv-dev: June 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: Sun, 20 Jun 2004 08:47:37 -0700
Reply-to: rt@xxxxxxxxxxx

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

In three places the formula for determining "square distance" is coded 
as a pythagorean distance:

   dx * dx + dy * dy

This patch unifies those into one function, map_vector_to_sq_distance. 
The adantage of this is that it allows changing this formula.  For 
instance in a hex topology the "circle" determined by the sq distance is 
better done as a hexagon, so for instance city with radius 2 just goes 
for 2 tiles in each direction.  This is easy to change, if the formula 
is only executed in one place.

A similar function may be useful for the "real map distance".  However 
this is currently only used in one place (though this may change).

jason

Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.220
diff -u -r1.220 city.c
--- common/city.c       16 Jun 2004 03:01:02 -0000      1.220
+++ common/city.c       20 Jun 2004 15:43:50 -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.170
diff -u -r1.170 map.c
--- common/map.c        12 Jun 2004 17:42:27 -0000      1.170
+++ common/map.c        20 Jun 2004 15:43:50 -0000
@@ -430,6 +430,14 @@
   return NULL;
 }
 
+/****************************************************************************
+  Return the sq_distance for a given vector.
+****************************************************************************/
+int map_vector_to_sq_distance(int dx, int dy)
+{
+  return dx * dx + dy * dy;
+}
+
 /***************************************************************
 ...
 ***************************************************************/
@@ -452,8 +460,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);
 }
 
 /***************************************************************
Index: common/map.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.h,v
retrieving revision 1.188
diff -u -r1.188 map.h
--- common/map.h        12 Jun 2004 17:42:27 -0000      1.188
+++ common/map.h        20 Jun 2004 15:43:50 -0000
@@ -169,9 +169,11 @@
 const char *map_get_tile_fpt_text(int x, int y);
 struct tile *map_get_tile(int x, int y);
 
+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 <=