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 virtual const char* className() { return "vl::Rect"; }
00047 Rect(const Rect& other)
00048 {
00049 mX = other.x();
00050 mY = other.y();
00051 mWidth = other.width();
00052 mHeight = other.height();
00053 }
00054 Rect()
00055 {
00056
00057 mX = 0;
00058 mY = 0;
00059 mWidth = -1;
00060 mHeight = -1;
00061 }
00062 Rect(T x, T y, T width, T height)
00063 {
00064 mX = x;
00065 mY = y;
00066 mWidth = width;
00067 mHeight = height;
00068 }
00069
00070 T x() const { return mX; }
00071 T y() const { return mY; }
00072 T width() const { return mWidth; }
00073 T height() const { return mHeight; }
00074 T bottom() const { return mY; }
00075 T left() const { return mX; }
00076 T top() const { return mY+mHeight-1; }
00077 T right() const { return mX+mWidth-1; }
00078
00079 void setX(T x) { mX = x; }
00080 void setY(T y) { mY = y; }
00081 void setWidth(T w) { mWidth = w; }
00082 void setHeight(T h) { mHeight = h; }
00083
00084 bool isNull() const { return width() < 0 || height() < 0; }
00085 bool isPoint() const { return width() == 0 && height() == 0; }
00086
00087 Rect intersected(const Rect& other) const
00088 {
00089 if (isNull() || other.isNull())
00090 return other;
00091 T Ax1 = left();
00092 T Ax2 = right();
00093 T Ay1 = bottom();
00094 T Ay2 = top();
00095 T Bx1 = other.left();
00096 T Bx2 = other.right();
00097 T By1 = other.bottom();
00098 T By2 = other.top();
00099 if (Ax1 < Bx1) Ax1 = Bx1;
00100 if (Ay1 < By1) Ay1 = By1;
00101 if (Ax2 > Bx2) Ax2 = Bx2;
00102 if (Ay2 > By2) Ay2 = By2;
00103 return Rect(Ax1,Ay1,Ax2-Ax1+1,Ay2-Ay1+1);
00104 }
00105
00106 Rect united(const Rect& other) const
00107 {
00108 if (other.isNull())
00109 return *this;
00110 if (isNull())
00111 return other;
00112 T Ax1 = left();
00113 T Ax2 = right();
00114 T Ay1 = bottom();
00115 T Ay2 = top();
00116 T Bx1 = other.left();
00117 T Bx2 = other.right();
00118 T By1 = other.bottom();
00119 T By2 = other.top();
00120 if (Ax1 > Bx1) Ax1 = Bx1;
00121 if (Ay1 > By1) Ay1 = By1;
00122 if (Ax2 < Bx2) Ax2 = Bx2;
00123 if (Ay2 < By2) Ay2 = By2;
00124 return Rect(Ax1,Ay1,Ax2-Ax1+1,Ay2-Ay1+1);
00125 }
00126
00130 bool operator<(const Rect& other) const
00131 {
00132 if (mX != other.mX)
00133 return mX < other.mX;
00134 else
00135 if (mWidth != other.mWidth)
00136 return mWidth < other.mWidth;
00137 else
00138 if (mHeight != other.mHeight)
00139 return mHeight < other.mHeight;
00140 else
00141 if (mY != other.mY)
00142 return mY < other.mY;
00143 else
00144 return false;
00145 }
00146
00147 protected:
00148 T mX;
00149 T mY;
00150 T mWidth;
00151 T mHeight;
00152 };
00153
00164 class RectI: public Rect<int>
00165 {
00166 public:
00167 virtual const char* className() { return "vl::RectI"; }
00168 RectI() {}
00169 RectI(int x, int y, int width, int height) { mX=x; mY=y; mWidth=width; mHeight=height; }
00170 RectI(const Rect<int>& other) { *this = other; }
00171 RectI(const RectI& other): Rect<int>(other) { *this = other; }
00172
00173 RectI& operator=(const Rect<int>& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00174 RectI& operator=(const RectI& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00175 int top() const { return mY+mHeight-1; }
00176 int right() const { return mX+mWidth-1; }
00177 ivec2 bottomLeft() const { return ivec2(bottom(),left()); }
00178 ivec2 topRight() const { return ivec2(top(),right()); }
00179 };
00180
00191 class RectF: public Rect<float>
00192 {
00193 public:
00194 virtual const char* className() { return "vl::RectF"; }
00195 RectF() {}
00196 RectF(float x, float y, float width, float height) { mX=x; mY=y; mWidth=width; mHeight=height; }
00197 RectF(const RectF& other): Rect<float>(other) { *this = other; }
00198 RectF(const Rect<float>& other) { *this = other; }
00199
00200 RectF& operator=(const Rect<float>& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00201 RectF& operator=(const RectF& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00202 float top() const { return mY+mHeight; }
00203 float right() const { return mX+mWidth; }
00204 fvec2 bottomLeft() const { return fvec2(bottom(),left()); }
00205 fvec2 topRight() const { return fvec2(top(),right()); }
00206 void addPoint(const fvec2& p)
00207 {
00208 fvec2 tr = topRight();
00209 fvec2 bl = bottomLeft();
00210 if (p.x() < bl.x())
00211 bl.x() = p.x();
00212 if (p.y() < bl.y())
00213 bl.y() = p.y();
00214 if (p.x() > tr.x())
00215 tr.x() = p.x();
00216 if (p.y() > tr.y())
00217 tr.y() = p.y();
00218 mX = bl.x();
00219 mY = bl.y();
00220 mWidth = tr.x() - bl.x();
00221 mHeight = tr.y() - bl.y();
00222 }
00223 };
00224 }
00225
00226 #endif