Point.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 POINT_HH_
00018 # define POINT_HH_
00019 
00020 class Position;
00021 template <typename T> class TPoint;
00022 class Rect;
00023 
00024 template <typename T> bool operator== (const TPoint<T>&, const TPoint<T>&);
00025 template <typename T> bool operator!= (const TPoint<T>&, const TPoint<T>&);
00026 template <typename T> TPoint<T> operator+ (const TPoint<T>&, const TPoint<T>&);
00027 template <typename T> TPoint<T> operator- (const TPoint<T>&, const TPoint<T>&);
00028 template <typename T> TPoint<T> operator* (const TPoint<T>&, double factor);
00029 template <typename T> TPoint<T> operator/ (const TPoint<T>&, T factor);
00030 template <typename T> std::ostream& operator<< (std::ostream&, const TPoint<T>&);
00031 
00038 template <typename T>
00039 class TPoint
00040 {
00041 public:
00042 
00043   TPoint();
00044   TPoint(T x, T y);
00045   template <typename U>
00046   TPoint(const TPoint<U>& pos);
00047   TPoint(const Position& pos);
00048   TPoint(const Rect& pos);
00049 
00050   T     getRow() const;
00051   T     getCol() const;
00052   void  setRow(T row);
00053   void  setCol(T col);
00054   
00055   T     getX() const;
00056   T     getY() const;
00057   void  setX(T x);
00058   void  setY(T y);
00059 
00061   double distance(const TPoint& to) const;
00062 
00063   friend bool operator== <> (const TPoint<T>& lhs, const TPoint<T>& rhs);
00064   friend bool operator!= <> (const TPoint<T>& lhs, const TPoint<T>& rhs);
00065   friend TPoint<T> operator+ <> (const TPoint<T>& lhs, const TPoint<T>& rhs);
00066   friend TPoint<T> operator- <> (const TPoint<T>& lhs, const TPoint<T>& rhs);
00067   friend TPoint<T> operator* <> (const TPoint<T>& lhs, double factor);
00068   friend TPoint<T> operator/ <> (const TPoint<T>& lhs, T factor);
00069   friend std::ostream& operator<< <> (std::ostream& os, const TPoint<T>& p);
00070 
00071   T x;  
00072   T y;  
00073 };
00074 
00076 typedef TPoint<int>     Point;
00078 typedef TPoint<double>  DPoint;
00079 
00080 # include "misc/Position.hh"
00081 # include "misc/Rect.hh"
00082 
00083 template <typename T>
00084 inline TPoint<T>::TPoint()
00085   : x(-1),
00086     y(-1)
00087 {
00088 }
00089 
00090 template <typename T>
00091 inline TPoint<T>::TPoint(T x, T y)
00092   : x(x),
00093     y(y)
00094 {
00095 }
00096 
00097 template <typename T>
00098 template <typename U>
00099 TPoint<T>::TPoint(const TPoint<U>& pos)
00100   : x(static_cast<T>(pos.x)),
00101     y(static_cast<T>(pos.y))
00102 {
00103 }
00104 
00105 template <typename T>
00106 inline TPoint<T>::TPoint(const Position& pos)
00107   : x(pos.col),
00108     y(pos.row)
00109 {
00110 }
00111 
00112 template <typename T>
00113 inline TPoint<T>::TPoint(const Rect& r)
00114   : x(r.x),
00115     y(r.y)
00116 {
00117 }
00118 
00119 template <typename T>
00120 inline T   TPoint<T>::getRow() const
00121 {
00122   return y;
00123 }
00124 
00125 template <typename T>
00126 inline T   TPoint<T>::getCol() const
00127 {
00128   return x;
00129 }
00130 
00131 template <typename T>
00132 inline void  TPoint<T>::setRow(T row)
00133 {
00134   y = row;
00135 }
00136 
00137 template <typename T>
00138 inline void  TPoint<T>::setCol(T col)
00139 {
00140   x = col;
00141 }
00142   
00143 template <typename T>
00144 inline T   TPoint<T>::getX() const
00145 {
00146   return x;
00147 }
00148 
00149 template <typename T>
00150 inline T   TPoint<T>::getY() const
00151 {
00152   return y;
00153 }
00154 
00155 template <typename T>
00156 inline void  TPoint<T>::setX(T x)
00157 {
00158   this->x = x;
00159 }
00160 
00161 template <typename T>
00162 inline void  TPoint<T>::setY(T y)
00163 {
00164   this->y = y;
00165 }
00166 
00167 template <typename T>
00168 inline double TPoint<T>::distance(const TPoint& to) const
00169 {
00170   return sqrt((double)((to.x - x) * (to.x - x) + (to.y - y) * (to.y - y)));
00171 }
00172 
00173 template <typename T>
00174 inline bool operator== (const TPoint<T>& lhs, const TPoint<T>& rhs)
00175 {
00176   return lhs.x == rhs.x && lhs.y == rhs.y;
00177 }
00178 
00179 template <typename T>
00180 inline bool operator!= (const TPoint<T>& lhs, const TPoint<T>& rhs)
00181 {
00182   return !(lhs.x == rhs.x && lhs.y == rhs.y);
00183 }
00184 
00185 template <typename T>
00186 inline TPoint<T> operator+ (const TPoint<T>& lhs, const TPoint<T>& rhs)
00187 {
00188   return TPoint<T>(lhs.x + rhs.x, lhs.y + rhs.y);
00189 }
00190 
00191 template <typename T>
00192 inline TPoint<T> operator- (const TPoint<T>& lhs, const TPoint<T>& rhs)
00193 {
00194   return TPoint<T>(lhs.x - rhs.x, lhs.y - rhs.y);
00195 }
00196 
00197 template <typename T>
00198 inline TPoint<T> operator* (const TPoint<T>& lhs, double factor)
00199 {
00200   return TPoint<T>((T)(lhs.x * factor), (T)(lhs.y * factor));
00201 }
00202 
00203 template <typename T>
00204 inline TPoint<T> operator/ (const TPoint<T>& lhs, T factor)
00205 {
00206   return TPoint<T>((T)(lhs.x / factor), (T)(lhs.y / factor));
00207 }
00208 
00209 template <typename T>
00210 inline std::ostream& operator<< (std::ostream& os, const TPoint<T>& p)
00211 {
00212   os << "[x:" << p.x << " y:" << p.y << "]";
00213   return os;
00214 }
00215 
00216 #endif /* !POINT_HH_ */

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