faithtools/faith/rect.h

Go to the documentation of this file.
00001 #ifndef KDE__RECT_H
00002 #define KDE__RECT_H
00003 
00004 #include <faith/point.h>
00005 #include <faith/size.h>
00006 
00007 namespace Faith
00008 {
00009 class FAITH_TOOLS_EXPORT Rect
00010 {
00011         Point mTopLeft;
00012         Size mSize;
00013 public:
00014         Rect()
00015                 : mSize(-1, -1)
00016         { }
00017         
00018         Rect(int x, int y, int w, int h)
00019                 : mTopLeft(x,y), mSize(w, h)
00020         { }
00021 
00022         Rect(int x, int y, const Size &size)
00023                 : mTopLeft(x,y), mSize(size)
00024         { }
00025 
00026         Rect(const Point &pt, const Size &size)
00027                 : mTopLeft(pt), mSize(size)
00028         { }
00029         
00030         Rect(const Point &pt, const Point &bottomRight)
00031                 : mTopLeft(pt), mSize(bottomRight.x()-pt.x(), bottomRight.y()-pt.y())
00032         { }
00033 
00034         Rect(const Point &pt, int w, int h)
00035                 : mTopLeft(pt), mSize(w, h)
00036         { }
00037 
00038         explicit Rect(const Size &size)
00039                 : mSize(size)
00040         { }
00041 
00042         bool operator ==(const Rect &other) const
00043         {
00044                 return
00045                         other.mTopLeft == mTopLeft
00046                         && other.mSize == mSize;
00047         }
00048         
00049         void operator +=(const Point &d)
00050         {
00051                 mTopLeft += d;
00052         }
00053         
00054         Rect operator + (const Point &d) const
00055         {
00056                 Rect r(*this);
00057                 r += d;
00058                 return r;
00059         }
00060         
00064         bool isInvalid() const
00065         {
00066                 return (mSize.width() < 0 || mSize.height() < 0);
00067         }
00068         
00069         bool isValid() const { return !isInvalid(); }
00070 
00071         int left() const { return x(); }
00072         int right() const { return x()+mSize.width()-1; }
00073 
00074         int top() const { return y(); }
00075         int bottom() const { return y()+mSize.height()-1; }
00076 
00077         Point topLeft() const { return mTopLeft; }
00078         void setTopLeft(const Point &pt) { mTopLeft = pt; }
00079         Point bottomLeft() const { return Point(x(), y()+height()); }
00080         Point bottomRight() const { return Point(x()+width(), y()+height()); }
00081         
00082         void moveBy(int x, int y)
00083         {
00084                 mTopLeft += Point(x, y);
00085         }
00086 
00087         int x() const { return mTopLeft.x(); }
00088         int y() const { return mTopLeft.y(); }
00089 
00090         int width() const { return mSize.width(); }
00091         int height() const { return mSize.height(); }
00092 
00093         void move(int x, int y) { mTopLeft = Point(x,y); }
00094         void move(const Point &pt) { mTopLeft = pt; }
00095         void resize(int x, int y) { mSize.resize(x,y); }
00096         void resize(const Size &size) { mSize = size; }
00097         
00098         bool contains(const Faith::Point &other)
00099         {
00100                 return
00101                         (other.x() >= mTopLeft.x()) &&
00102                         (other.y() >= mTopLeft.y()) &&
00103                         (other.x() <= mTopLeft.x() + mSize.width()) &&
00104                         (other.y() <= mTopLeft.y() + mSize.height());
00105         }
00106         
00107         Point center() const { return Point(x()+width()/2, y()+height()/2); }
00108 
00109         Size size() const { return mSize; }
00110         
00111         void setRect(int x, int y, int w, int h)
00112         {
00113                 mTopLeft = Point(x,y);
00114                 mSize = Size(w,h);
00115         }
00116         
00117         void rect(int *x, int *y, int *w, int *h) const
00118         {
00119                 *x = mTopLeft.x();
00120                 *y = mTopLeft.y();
00121                 *w = mSize.width();
00122                 *h = mSize.height();
00123         }
00124         
00125         void coords(int *x, int *y, int *bx, int *by) const
00126         {
00127                 *x = mTopLeft.x();
00128                 *y = mTopLeft.y();
00129                 *bx = bottom();
00130                 *by = left();
00131         }
00132         
00133         void setCoords(int x, int y, int bx, int by)
00134         {
00135                 setRect(x, y, bx-x, by-y);
00136         }
00137         
00144         void addCoords(int x, int y, int bx, int by)
00145         {
00146                 mTopLeft += Point(x,y);
00147                 mSize += Size(bx, by);
00148         }
00149         
00150         Rect unite(const Rect &other) const
00151         {
00152                 Rect r;
00153                 int ax1, ay1, ax2, ay2;
00154                 other.coords(&ax1, &ay1, &ax2, &ay2);
00155                 int bx1, by1, bx2, by2;
00156                 coords(&bx1, &by1, &bx2, &by2);
00157                 
00158                 r.setCoords(MIN(ax1, bx1), MIN(ay1, by1), MAX(ax2, bx2), MAX(ay2, by2));
00159                 return r;
00160         }
00161         
00162         void moveCenter(const Point &to)
00163         {
00164                 mTopLeft = Point(to.x()*2-width(), to.y()*2-height());
00165         }
00166 };
00167 
00168 inline Rect operator+(const Point &d, const Rect &r)
00169 {
00170         return r.operator+(d);
00171 }
00172 
00173 }
00174 
00175 #endif

Generated on Fri Sep 1 00:23:07 2006 for Faith by  doxygen 1.4.7