Direction.h

Go to the documentation of this file.
00001 /*
00002  *      Direction.h - Directions.
00003  *
00004  *  Copyright (C) 1998-1999  Jeffrey S. Freedman
00005  *  Copyright (C) 2000-2001  The Exult Team
00006  *  Copyright (C) 2002-2003  The Pentagram Team
00007  *
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program; if not, write to the Free Software
00020  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 #ifndef DIRECTION_H
00024 #define DIRECTION_H
00025 
00026 /*
00027  *      Directions:
00028  */
00029 enum Direction
00030 {
00031         north = 0,
00032         northeast = 1,
00033         east = 2,
00034         southeast = 3,
00035         south = 4,
00036         southwest = 5,
00037         west = 6,
00038         northwest = 7
00039 };
00040 
00041 /*
00042  * Tables to map a Direction to x/y deltas
00043  */
00044 const int x_fact[] = {  0, +1, +1, +1,  0, -1, -1, -1 };
00045 const int y_fact[] = { -1, -1,  0, +1, +1, +1,  0, -1 };
00046 
00047 /*
00048  *      Return the direction for a given slope (0-7).
00049  *      NOTE:  Assumes cartesian coords, NOT screen coords. (which have y
00050  *              growing downwards).
00051  *
00052  *  NOTE: The returned direction is rotated 45 degrees clockwise! This is
00053  *  how U8 things should be.
00054  */
00055 
00056 inline Direction Get_direction (int deltay, int deltax)
00057 {
00058         if (deltax == 0)
00059                 return deltay > 0 ? northwest : southeast;
00060         int dydx = (1024*deltay)/deltax;// Figure 1024*tan.
00061         if (dydx >= 0)
00062                 if (deltax > 0) // Top-right
00063                         return dydx <= 424 ? northeast : dydx <= 2472 ? north
00064                                                                 : northwest;
00065                 else                    // Bottom-left.
00066                         return dydx <= 424 ? southwest : dydx <= 2472 ? south
00067                                                                 : southeast;
00068         else
00069                 if (deltax > 0) // Bottom-right.
00070                         return dydx >= -424 ? northeast : dydx >= -2472 ? east
00071                                                                 : southeast;
00072                 else                    // Top-left
00073                         return dydx >= -424 ? southwest : dydx >= -2472 ? west
00074                                                                 : northwest;
00075 }
00076 
00077 
00078 inline Direction Get_WorldDirection (int deltay, int deltax)
00079 {
00080         if (deltax == 0)
00081                 return deltay > 0 ? south : north;
00082         int dydx = (1024*deltay)/deltax;
00083 
00084         if (dydx >= 0)
00085                 if (deltax > 0) // south-east
00086                         return dydx <= 424 ? east : dydx <= 2472 ? southeast : south;
00087                 else            // north-west
00088                         return dydx <= 424 ? west : dydx <= 2472 ? northwest : north;
00089         else
00090                 if (deltax > 0) // north-east
00091                         return dydx >= -424 ? east : dydx >= -2472 ? northeast : north;
00092                 else            // south-west
00093                         return dydx >= -424 ? west : dydx >= -2472 ? southwest : south;
00094 }
00095 
00096 
00097 #endif

Generated on Fri Jul 27 22:27:13 2007 for pentagram by  doxygen 1.4.7