dir.cc

Go to the documentation of this file.
00001 /*
00002  *  dir.cc - Directions.
00003  *
00004  *  Copyright (C) 1998-1999  Jeffrey S. Freedman
00005  *  Copyright (C) 2000-2001  The Exult Team
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020  */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #  include <config.h>
00024 #endif
00025 
00026 #include "dir.h"
00027 
00028 /*
00029  *  Return the direction for a given slope (0-7).
00030  *  NOTE:  Assumes cartesian coords, NOT screen coords. (which have y
00031  *    growing downwards).
00032  */
00033 
00034 Direction Get_direction
00035   (
00036   int deltay,
00037   int deltax
00038   )
00039 {
00040   if (deltax == 0)
00041     return deltay > 0 ? north : south;
00042   int dydx = (1024*deltay)/deltax;// Figure 1024*tan.
00043   if (dydx >= 0)
00044     if (deltax >= 0)  // Top-right quadrant?
00045       return dydx <= 424 ? east : dydx <= 2472 ? northeast
00046                 : north;
00047     else      // Lower-left.
00048       return dydx <= 424 ? west : dydx <= 2472 ? southwest
00049                 : south;
00050   else
00051     if (deltax >= 0)  // Lower-right.
00052       return dydx >= -424 ? east : dydx >= -2472 ? southeast
00053                 : south;
00054     else      // Top-left?
00055       return dydx >= -424 ? west : dydx >= -2472 ? northwest
00056                 : north;
00057 }
00058 
00059 /*
00060  *  Return the direction for a given slope (0-7), rounded to NSEW.
00061  *  NOTE:  Assumes cartesian coords, NOT screen coords. (which have y
00062  *    growing downwards).
00063  */
00064 
00065 Direction Get_direction4
00066   (
00067   int deltay,
00068   int deltax
00069   )
00070 {
00071   if (deltax >= 0)    // Right side?
00072     return (deltay > deltax ? north : deltay < -deltax ? south
00073                 : east);
00074   else        // Left side.
00075     return (deltay > -deltax ? north : deltay < deltax ? south
00076                 : west);
00077 }
00078 
00079 /*
00080  *  Return the direction for a given slope (0-15).
00081  *  NOTE:  Assumes cartesian coords, NOT screen coords. (which have y
00082  *    growing downwards).
00083  */
00084 
00085 int Get_direction16
00086   (
00087   int deltay,
00088   int deltax
00089   )
00090 {
00091   if (deltax == 0)
00092     return deltay > 0 ? 0 : 8;
00093   int dydx = (1024*deltay)/deltax;// Figure 1024*tan.
00094   int adydx = dydx < 0 ? -dydx : dydx;
00095   int angle = 0;
00096   if (adydx < 1533)   // 1024*tan(5*11.25)
00097   {
00098     if (adydx < 204)  // 1024*tan(11.25).
00099       angle = 4;
00100     else if (adydx < 684) // 1024*tan(3*11.25).
00101       angle = 3;
00102     else
00103       angle = 2;
00104   }
00105   else
00106   {
00107     if (adydx < 5148) // 1024*tan(7*11.25).
00108       angle = 1;
00109     else
00110       angle = 0;
00111   }
00112 
00113   if (deltay < 0)     // Check quadrants.
00114     if (deltax > 0)
00115       angle = 8 - angle;
00116     else
00117       angle += 8;
00118   else if (deltax < 0)
00119     angle = 16 - angle;
00120   return angle % 16;
00121 }

Generated on Mon Jul 9 14:42:45 2007 for ExultEngine by  doxygen 1.5.1