Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2001:
[Freeciv-Dev] Re: directional system: more magic code cleanups
Home

[Freeciv-Dev] Re: directional system: more magic code cleanups

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: jdorje@xxxxxxxxxxxxxxxxxxxxx
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: directional system: more magic code cleanups
From: Raimar Falke <hawk@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 18 Sep 2001 18:07:09 +0200
Reply-to: rf13@xxxxxxxxxxxxxxxxxxxxxx

On Mon, Sep 17, 2001 at 01:01:51PM -0400, Jason Dorje Short wrote:
> static int straightest_direction(int src_x, int src_y, int dest_x, int
> dest_y)
> {
>   int best_dir = -1, dir;
>   float best = -1;
>   int diff_x, diff_y;
> 
>   /* This code is topology-dependent */
>   diff_x = dest_x - src_x;
>   if (diff_x < -map.xsize / 2) diff_x += map.xsize;
>   if (diff_x > map.xsize / 2) diff_x -= map.xsize;
>   diff_y = dest_y - src_y;
> 
>   for (dir=0; dir<8; dir++) {
>     float product = diff_x * DIR_DX[dir] + diff_y * DIR_DY[dir];
>     product /= sqrt(DIR_DX[dir] * DIR_DX[dir] + DIR_DY[dir] *
> DIR_DY[dir]);
> 
>     if (product > best) {
>       best = product;
>       best_dir = dir;
>     }
>   }
> 
>   assert(best >= 0 && best_dir >= 0);
>   return best_dir;
> }

I have attached a version which doesn't use float and sqrt. Please
review.

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
  Living on earth may be expensive, but it includes an annual free trip
  around the sun.

Attachment: diff
Description: Text document


[Prev in Thread] Current Thread [Next in Thread]