stechec/tools/misc/Position.hh

00001 /*
00002 ** TowBowlTactics, an adaptation of the tabletop game Blood Bowl.
00003 ** 
00004 ** Copyright (C) 2006 The TBT Team.
00005 ** 
00006 ** This program is free software; you can redistribute it and/or
00007 ** modify it under the terms of the GNU General Public License
00008 ** as published by the Free Software Foundation; either version 2
00009 ** of the License, or (at your option) any later version.
00010 ** 
00011 ** The complete GNU General Public Licence Notice can be found as the
00012 ** `NOTICE' file in the root directory.
00013 ** 
00014 ** The TBT Team consists of people listed in the `AUTHORS' file.
00015 */
00016 
00017 #ifndef POSITION_HH_
00018 # define POSITION_HH_
00019 
00021 template <typename T> class TPoint;
00022 typedef TPoint<int> Point;
00023 
00032 class Position
00033 {
00034 public:
00035   Position();
00036   Position(int row, int col);
00037   Position(const Point& pos);
00038 
00039   int   getRow() const;
00040   int   getCol() const;
00041   void  setRow(int row);
00042   void  setCol(int col);
00043   
00044   int   getX() const;
00045   int   getY() const;
00046   void  setX(int x);
00047   void  setY(int y);
00048 
00053   bool isNear(const Position& from, int dist = 1) const;
00054 
00056   float distance(const Position& to) const;
00057   
00058   friend bool operator== (const Position& lhs, const Position& rhs);
00059   friend bool operator!= (const Position& lhs, const Position& rhs);
00060   friend Position operator+ (const Position& lhs, const Position& rhs);
00061   friend Position operator- (const Position& lhs, const Position& rhs);
00062   friend Position operator* (int factor, const Position& rhs);
00063   friend std::ostream& operator<< (std::ostream& os, const Position& p);
00064   Position& operator+= (const Position& rhs);
00065 
00066   int   row;  
00067   int   col;  
00068 };
00069 
00070 # include "misc/Point.hh"
00071 
00073 typedef std::deque<Position>    PosList;
00075 typedef PosList::const_iterator PosIter;
00076 
00077 inline Position::Position()
00078   : row(-1),
00079     col(-1)
00080 {
00081 }
00082 
00083 inline Position::Position(int row, int col)
00084   : row(row),
00085     col(col)
00086 {
00087 }
00088 
00089 inline Position::Position(const Point& pos)
00090   : row(pos.y),
00091     col(pos.x)
00092 {
00093 }
00094 
00095 
00096 inline int   Position::getRow() const
00097 {
00098   return row;
00099 }
00100 
00101 inline int   Position::getCol() const
00102 {
00103   return col;
00104 }
00105 
00106 inline void  Position::setRow(int row)
00107 {
00108   this->row = row;
00109 }
00110 
00111 inline void  Position::setCol(int col)
00112 {
00113   this->col = col;
00114 }
00115 
00116 inline int   Position::getX() const
00117 {
00118   return col;
00119 }
00120 
00121 inline int   Position::getY() const
00122 {
00123   return row;
00124 }
00125 
00126 inline void  Position::setX(int x)
00127 {
00128   col = x;
00129 }
00130 
00131 inline void  Position::setY(int y)
00132 {
00133   row = y;
00134 }
00135 
00136 
00137 inline bool Position::isNear(const Position& from, int dist) const
00138 {
00139   return abs(from.row - row) <= dist && abs(from.col - col) <= dist;
00140 }
00141 
00142 inline float Position::distance(const Position& to) const
00143 {
00144   return sqrtf((float) (to.row - row)*(to.row - row)
00145             + (to.col - col)*(to.col - col));
00146 }
00147 
00148 inline bool operator== (const Position& lhs, const Position& rhs)
00149 {
00150   return lhs.row == rhs.row && lhs.col == rhs.col;
00151 }
00152 
00153 inline bool operator!= (const Position& lhs, const Position& rhs)
00154 {
00155   return lhs.row != rhs.row || lhs.col != rhs.col;
00156 }
00157 
00158 inline Position operator+ (const Position& lhs, const Position& rhs)
00159 {
00160   return Position(lhs.row + rhs.row, lhs.col + rhs.col);
00161 }
00162 
00163 inline Position operator- (const Position& lhs, const Position& rhs)
00164 {
00165   return Position(lhs.row - rhs.row, lhs.col - rhs.col);
00166 }
00167 
00168 inline Position operator* (int factor, const Position& rhs)
00169 {
00170   return Position(rhs.row * factor, rhs.col * factor);
00171 }
00172 
00173 inline std::ostream& operator<< (std::ostream& os, const Position& p)
00174 {
00175   os << "[row/y:" << p.row << " col/x:" << p.col << "]";
00176   return os;
00177 }
00178 
00179 inline Position& Position::operator+= (const Position& rhs)
00180 {
00181   row += rhs.row;
00182   col += rhs.col;
00183   return *this;
00184 }
00185 
00186 #endif /* !POSITION_HH_ */

Generated on Sat Jun 23 16:07:23 2007 for Stechec/TBT by  doxygen 1.4.7