Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef Rect_INCLUDE_ONCE
00033 #define Rect_INCLUDE_ONCE
00034
00035 #include <vlCore/Vector2.hpp>
00036
00037 namespace vl
00038 {
00042 template<typename T>
00043 class Rect
00044 {
00045 public:
00046 Rect(const Rect& other)
00047 {
00048 mX = other.x();
00049 mY = other.y();
00050 mWidth = other.width();
00051 mHeight = other.height();
00052 }
00053 Rect()
00054 {
00055
00056 mX = 0;
00057 mY = 0;
00058 mWidth = -1;
00059 mHeight = -1;
00060 }
00061 Rect(T x, T y, T width, T height)
00062 {
00063 mX = x;
00064 mY = y;
00065 mWidth = width;
00066 mHeight = height;
00067 }
00068
00069 T x() const { return mX; }
00070 T y() const { return mY; }
00071 T width() const { return mWidth; }
00072 T height() const { return mHeight; }
00073 T bottom() const { return mY; }
00074 T left() const { return mX; }
00075 T top() const { return mY+mHeight-1; }
00076 T right() const { return mX+mWidth-1; }
00077
00078 void setX(T x) { mX = x; }
00079 void setY(T y) { mY = y; }
00080 void setWidth(T w) { mWidth = w; }
00081 void setHeight(T h) { mHeight = h; }
00082
00083 bool isNull() const { return width() < 0 || height() < 0; }
00084 bool isPoint() const { return width() == 0 && height() == 0; }
00085
00086 Rect intersected(const Rect& other) const
00087 {
00088 if (isNull() || other.isNull())
00089 return other;
00090 T Ax1 = left();
00091 T Ax2 = right();
00092 T Ay1 = bottom();
00093 T Ay2 = top();
00094 T Bx1 = other.left();
00095 T Bx2 = other.right();
00096 T By1 = other.bottom();
00097 T By2 = other.top();
00098 if (Ax1 < Bx1) Ax1 = Bx1;
00099 if (Ay1 < By1) Ay1 = By1;
00100 if (Ax2 > Bx2) Ax2 = Bx2;
00101 if (Ay2 > By2) Ay2 = By2;
00102 return Rect(Ax1,Ay1,Ax2-Ax1+1,Ay2-Ay1+1);
00103 }
00104
00105 Rect united(const Rect& other) const
00106 {
00107 if (other.isNull())
00108 return *this;
00109 if (isNull())
00110 return other;
00111 T Ax1 = left();
00112 T Ax2 = right();
00113 T Ay1 = bottom();
00114 T Ay2 = top();
00115 T Bx1 = other.left();
00116 T Bx2 = other.right();
00117 T By1 = other.bottom();
00118 T By2 = other.top();
00119 if (Ax1 > Bx1) Ax1 = Bx1;
00120 if (Ay1 > By1) Ay1 = By1;
00121 if (Ax2 < Bx2) Ax2 = Bx2;
00122 if (Ay2 < By2) Ay2 = By2;
00123 return Rect(Ax1,Ay1,Ax2-Ax1+1,Ay2-Ay1+1);
00124 }
00125
00129 bool operator<(const Rect& other) const
00130 {
00131 if (mX != other.mX)
00132 return mX < other.mX;
00133 else
00134 if (mWidth != other.mWidth)
00135 return mWidth < other.mWidth;
00136 else
00137 if (mHeight != other.mHeight)
00138 return mHeight < other.mHeight;
00139 else
00140 if (mY != other.mY)
00141 return mY < other.mY;
00142 else
00143 return false;
00144 }
00145
00146 protected:
00147 T mX;
00148 T mY;
00149 T mWidth;
00150 T mHeight;
00151 };
00152
00163 class RectI: public Rect<int>
00164 {
00165 public:
00166 RectI() {}
00167 RectI(int x, int y, int width, int height) { mX=x; mY=y; mWidth=width; mHeight=height; }
00168 RectI(const Rect<int>& other) { *this = other; }
00169 RectI(const RectI& other): Rect<int>(other) { *this = other; }
00170
00171 RectI& operator=(const Rect<int>& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00172 RectI& operator=(const RectI& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00173 int top() const { return mY+mHeight-1; }
00174 int right() const { return mX+mWidth-1; }
00175 ivec2 bottomLeft() const { return ivec2(bottom(),left()); }
00176 ivec2 topRight() const { return ivec2(top(),right()); }
00177 };
00178
00189 class RectF: public Rect<float>
00190 {
00191 public:
00192 RectF() {}
00193 RectF(float x, float y, float width, float height) { mX=x; mY=y; mWidth=width; mHeight=height; }
00194 RectF(const RectF& other): Rect<float>(other) { *this = other; }
00195 RectF(const Rect<float>& other) { *this = other; }
00196
00197 RectF& operator=(const Rect<float>& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00198 RectF& operator=(const RectF& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00199 float top() const { return mY+mHeight; }
00200 float right() const { return mX+mWidth; }
00201 fvec2 bottomLeft() const { return fvec2(bottom(),left()); }
00202 fvec2 topRight() const { return fvec2(top(),right()); }
00203 void addPoint(const fvec2& p)
00204 {
00205 fvec2 tr = topRight();
00206 fvec2 bl = bottomLeft();
00207 if (p.x() < bl.x())
00208 bl.x() = p.x();
00209 if (p.y() < bl.y())
00210 bl.y() = p.y();
00211 if (p.x() > tr.x())
00212 tr.x() = p.x();
00213 if (p.y() > tr.y())
00214 tr.y() = p.y();
00215 mX = bl.x();
00216 mY = bl.y();
00217 mWidth = tr.x() - bl.x();
00218 mHeight = tr.y() - bl.y();
00219 }
00220 };
00221 }
00222
00223 #endif