Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_DIMENSION_RECT2D_H__
00009 #define __SP_DIMENSION_RECT2D_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013 #include "Base/spDimensionPoint2D.hpp"
00014 #include "Base/spDimensionSize2D.hpp"
00015 #include "Base/spMathCore.hpp"
00016
00017
00018 namespace sp
00019 {
00020 namespace dim
00021 {
00022
00023
00024 template <typename T> class rect2d
00025 {
00026
00027 public:
00028
00029 rect2d() :
00030 Left (0),
00031 Right (0),
00032 Top (0),
00033 Bottom (0)
00034 {
00035 }
00036 rect2d(T X, T Y) :
00037 Left (X),
00038 Right (X),
00039 Top (Y),
00040 Bottom (Y)
00041 {
00042 }
00043 rect2d(T NewLeft, T NewTop, T NewRight, T NewBottom) :
00044 Left (NewLeft ),
00045 Right (NewRight ),
00046 Top (NewTop ),
00047 Bottom (NewBottom )
00048 {
00049 }
00050 rect2d(const rect2d<T> &Other) :
00051 Left (Other.Left ),
00052 Right (Other.Right ),
00053 Top (Other.Top ),
00054 Bottom (Other.Bottom )
00055 {
00056 }
00057 ~rect2d()
00058 {
00059 }
00060
00061
00062
00063 inline bool operator == (const rect2d<T> &other) const
00064 {
00065 return Left == other.Left && Top == other.Top && Right == other.Right && Bottom == other.Bottom;
00066 }
00067 inline bool operator != (const rect2d<T> &other) const
00068 {
00069 return Left != other.Left && Top != other.Top && Right != other.Right && Bottom != other.Bottom;
00070 }
00071
00072 inline rect2d<T> operator + (const rect2d<T> &other) const
00073 {
00074 return rect2d<T>(Left + other.Left, Top + other.Top, Right + other.Right, Bottom + other.Bottom);
00075 }
00076 inline rect2d<T>& operator += (const rect2d<T> &other)
00077 {
00078 Left += other.Left; Top += other.Top; Right += other.Right; Bottom += other.Bottom; return *this;
00079 }
00080
00081 inline rect2d<T> operator - (const rect2d<T> &other) const
00082 {
00083 return rect2d<T>(Left - other.Left, Top - other.Top, Right - other.Right, Bottom - other.Bottom);
00084 }
00085 inline rect2d<T>& operator -= (const rect2d<T> &other)
00086 {
00087 Left -= other.Left; Top -= other.Top; Right -= other.Right; Bottom -= other.Bottom; return *this;
00088 }
00089
00090 inline rect2d<T> operator / (const rect2d<T> &other) const
00091 {
00092 return rect2d<T>(Left / other.Left, Top / other.Top, Right / other.Right, Bottom / other.Bottom);
00093 }
00094 inline rect2d<T>& operator /= (const rect2d<T> &other)
00095 {
00096 Left /= other.Left; Top /= other.Top; Right /= other.Right; Bottom /= other.Bottom; return *this;
00097 }
00098
00099 inline rect2d<T> operator * (const rect2d<T> &other) const
00100 {
00101 return rect2d<T>(Left * other.Left, Top * other.Top, Right * other.Right, Bottom * other.Bottom);
00102 }
00103 inline rect2d<T>& operator *= (const rect2d<T> &other)
00104 {
00105 Left *= other.Left; Top *= other.Top; Right *= other.Right; Bottom *= other.Bottom; return *this;
00106 }
00107
00108 inline rect2d<T> operator - () const
00109 {
00110 return rect2d<T>(-Left, -Top, -Right, -Bottom);
00111 }
00112
00113
00114
00116 inline void setSize(const size2d<T> &Size)
00117 {
00118 Right = Left + Size.Width;
00119 Bottom = Top + Size.Height;
00120 }
00122 inline size2d<T> getSize() const
00123 {
00124 return size2d<T>(Right - Left, Bottom - Top);
00125 }
00126
00128 inline void setCenter(const point2d<T> &Center)
00129 {
00130 *this += rect2d<T>(Center - getCenter());
00131 }
00133 inline point2d<T> getCenter() const
00134 {
00135 return point2d<T>((Right + Left)/2, (Bottom + Top)/2);
00136 }
00137
00139 inline void setLTPoint(const point2d<T> &Point)
00140 {
00141 Left = Point.X;
00142 Top = Point.Y;
00143 }
00145 inline point2d<T> getLTPoint() const
00146 {
00147 return point2d<T>(Left, Top);
00148 }
00149
00151 inline void setRBPoint(const point2d<T> &Point)
00152 {
00153 Right = Point.X;
00154 Bottom = Point.Y;
00155 }
00157 inline point2d<T> getRBPoint() const
00158 {
00159 return point2d<T>(Right, Bottom);
00160 }
00161
00163 inline T getWidth() const
00164 {
00165 return Right - Left;
00166 }
00168 inline T getHeight() const
00169 {
00170 return Bottom - Top;
00171 }
00173 inline bool empty() const
00174 {
00175 return Left == Right == Top == Bottom == 0;
00176 }
00178 inline bool valid() const
00179 {
00180 return getWidth() >= 0 && getHeight() >= 0;
00181 }
00182
00183 inline rect2d<T>& repair()
00184 {
00185 if (Left > Right)
00186 math::Swap(Left, Right);
00187 if (Top > Bottom)
00188 math::Swap(Top, Bottom);
00189 return *this;
00190 }
00191
00192 inline bool overlap(const point2d<T> &Point) const
00193 {
00194 return (Point.X >= Left && Point.X < Right && Point.Y >= Top && Point.Y < Bottom);
00195 }
00196 inline bool overlap(const rect2d<T> &other) const
00197 {
00198 return (Bottom > other.Top && Top < other.Bottom && Right > other.Left && Left < other.Right);
00199 }
00200
00201 template <typename B> inline rect2d<B> cast() const
00202 {
00203 return rect2d<B>(static_cast<B>(Left), static_cast<B>(Top), static_cast<B>(Right), static_cast<B>(Bottom));
00204 }
00205
00206
00207
00208 T Left, Right, Top, Bottom;
00209
00210 };
00211
00212 typedef rect2d<s32> rect2di;
00213 typedef rect2d<f32> rect2df;
00214
00215
00216 }
00217
00218 }
00219
00220
00221 #endif
00222
00223
00224
00225