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]
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: directional system: more magic code cleanups
From: Jason Dorje Short <jshort@xxxxxxxxxxxxx>
Date: Tue, 18 Sep 2001 15:54:55 -0400
Reply-to: jdorje@xxxxxxxxxxxxxxxxxxxxx

Raimar Falke wrote:
> 
> 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.

It looks ugly (IMO) but correct.

jason


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