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