00001 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- 00002 #ifndef __rect_hh 00003 #define __rect_hh 00004 00005 extern "C" { 00006 #include <X11/Xlib.h> 00007 } 00008 00009 #include "point.hh" 00010 #include <vector> 00011 00012 namespace otk { 00013 00014 //! The Rect class defines a rectangle in the plane. 00015 class Rect { 00016 public: 00017 //! Constructs an invalid Rect 00018 inline Rect(void) : _x1(0), _y1(0), _x2(0), _y2(0) { } 00019 //! Constructs a Rect 00020 /*! 00021 @param x The x component of the point defining the top left corner of the 00022 rectangle 00023 @param y The y component of the point defining the top left corner of the 00024 rectangle 00025 @param w The width of the rectangle 00026 @param h The height of the rectangle 00027 */ 00028 inline Rect(int x, int y, int w, int h) 00029 : _x1(x), _y1(y), _x2(w + x - 1), _y2(h + y - 1) { } 00030 //! Constructs a Rect from 2 Point objects 00031 /*! 00032 @param location The point defining the top left corner of the rectangle 00033 @param size The width and height of the rectangle 00034 */ 00035 inline Rect(const Point &location, const Point &size) 00036 : _x1(location.x()), _y1(location.y()), 00037 _x2(size.x() + location.x() - 1), _y2(size.y() + location.y() - 1) { } 00038 //! Constructs a Rect from another Rect 00039 /*! 00040 @param rect The rectangle from which to construct this new one 00041 */ 00042 inline Rect(const Rect &rect) 00043 : _x1(rect._x1), _y1(rect._y1), _x2(rect._x2), _y2(rect._y2) { } 00044 //! Constructs a Rect from an XRectangle 00045 inline explicit Rect(const XRectangle& xrect) 00046 : _x1(xrect.x), _y1(xrect.y), _x2(xrect.width + xrect.x - 1), 00047 _y2(xrect.height + xrect.y - 1) { } 00048 00049 //! Returns the left coordinate of the Rect. Identical to Rect::x. 00050 inline int left(void) const { return _x1; } 00051 //! Returns the top coordinate of the Rect. Identical to Rect::y. 00052 inline int top(void) const { return _y1; } 00053 //! Returns the right coordinate of the Rect 00054 inline int right(void) const { return _x2; } 00055 //! Returns the bottom coordinate of the Rect 00056 inline int bottom(void) const { return _y2; } 00057 00058 //! The x component of the point defining the top left corner of the Rect 00059 inline int x(void) const { return _x1; } 00060 //! The y component of the point defining the top left corner of the Rect 00061 inline int y(void) const { return _y1; } 00062 //! Returns the Point that defines the top left corner of the rectangle 00063 inline Point location() const { return Point(_x1, _y1); } 00064 00065 //! Sets the x coordinate of the Rect. 00066 /*! 00067 @param x The new x component of the point defining the top left corner of 00068 the rectangle 00069 */ 00070 void setX(int x); 00071 //! Sets the y coordinate of the Rect. 00072 /*! 00073 @param y The new y component of the point defining the top left corner of 00074 the rectangle 00075 */ 00076 void setY(int y); 00077 //! Sets the x and y coordinates of the Rect. 00078 /*! 00079 @param x The new x component of the point defining the top left corner of 00080 the rectangle 00081 @param y The new y component of the point defining the top left corner of 00082 the rectangle 00083 */ 00084 void setPos(int x, int y); 00085 //! Sets the x and y coordinates of the Rect. 00086 /*! 00087 @param location The point defining the top left corner of the rectangle. 00088 */ 00089 void setPos(const Point &location); 00090 00091 //! The width of the Rect 00092 inline int width(void) const { return _x2 - _x1 + 1; } 00093 //! The height of the Rect 00094 inline int height(void) const { return _y2 - _y1 + 1; } 00095 //! Returns the size of the Rect 00096 inline Point size() const { return Point(_x2 - _x1 + 1, _y2 - _y1 + 1); } 00097 00098 //! Sets the width of the Rect 00099 /*! 00100 @param w The new width of the rectangle 00101 */ 00102 void setWidth(int w); 00103 //! Sets the height of the Rect 00104 /*! 00105 @param h The new height of the rectangle 00106 */ 00107 void setHeight(int h); 00108 //! Sets the size of the Rect. 00109 /*! 00110 @param w The new width of the rectangle 00111 @param h The new height of the rectangle 00112 */ 00113 void setSize(int w, int h); 00114 //! Sets the size of the Rect. 00115 /*! 00116 @param size The new size of the rectangle 00117 */ 00118 void setSize(const Point &size); 00119 00120 //! Sets the position and size of the Rect 00121 /*! 00122 @param x The new x component of the point defining the top left corner of 00123 the rectangle 00124 @param y The new y component of the point defining the top left corner of 00125 the rectangle 00126 @param w The new width of the rectangle 00127 @param h The new height of the rectangle 00128 */ 00129 void setRect(int x, int y, int w, int h); 00130 //! Sets the position and size of the Rect 00131 /*! 00132 @param location The new point defining the top left corner of the rectangle 00133 @param size The new size of the rectangle 00134 */ 00135 void setRect(const Point &location, const Point &size); 00136 00137 //! Sets the position of all 4 sides of the Rect 00138 /*! 00139 @param l The new left coordinate of the rectangle 00140 @param t The new top coordinate of the rectangle 00141 @param r The new right coordinate of the rectangle 00142 @param b The new bottom coordinate of the rectangle 00143 */ 00144 void setCoords(int l, int t, int r, int b); 00145 //! Sets the position of all 4 sides of the Rect 00146 /*! 00147 @param tl The new point at the top left of the rectangle 00148 @param br The new point at the bottom right of the rectangle 00149 */ 00150 void setCoords(const Point &tl, const Point &br); 00151 00152 //! Determines if two Rect objects are equal 00153 /*! 00154 The rectangles are considered equal if they are in the same position and 00155 are the same size. 00156 */ 00157 inline bool operator==(const Rect &a) 00158 { return _x1 == a._x1 && _y1 == a._y1 && _x2 == a._x2 && _y2 == a._y2; } 00159 //! Determines if two Rect objects are inequal 00160 /*! 00161 @see operator== 00162 */ 00163 inline bool operator!=(const Rect &a) { return ! operator==(a); } 00164 00165 //! Returns the union of two Rect objects 00166 /*! 00167 The union of the rectangles will consist of the maximimum area that the two 00168 rectangles can make up. 00169 @param a A second Rect object to form a union with. 00170 */ 00171 Rect operator|(const Rect &a) const; 00172 //! Returns the intersection of two Rect objects 00173 /*! 00174 The intersection of the rectangles will consist of just the area where the 00175 two rectangles overlap. 00176 @param a A second Rect object to form an intersection with. 00177 @return The intersection between this Rect and the one passed to the 00178 function 00179 */ 00180 Rect operator&(const Rect &a) const; 00181 //! Sets the Rect to the union of itself with another Rect object 00182 /*! 00183 The union of the rectangles will consist of the maximimum area that the two 00184 rectangles can make up. 00185 @param a A second Rect object to form a union with. 00186 @return The union between this Rect and the one passed to the function 00187 */ 00188 inline Rect &operator|=(const Rect &a) { *this = *this | a; return *this; } 00189 //! Sets the Rect to the intersection of itself with another Rect object 00190 /*! 00191 The intersection of the rectangles will consist of just the area where the 00192 two rectangles overlap. 00193 @param a A second Rect object to form an intersection with. 00194 */ 00195 inline Rect &operator&=(const Rect &a) { *this = *this & a; return *this; } 00196 00197 //! Returns if the Rect is valid 00198 /*! 00199 A rectangle is valid only if its right and bottom coordinates are larger 00200 than its left and top coordinates (i.e. it does not have a negative width 00201 or height). 00202 @return true if the Rect is valid; otherwise, false 00203 */ 00204 inline bool valid(void) const { return _x2 > _x1 && _y2 > _y1; } 00205 00206 //! Determines if this Rect intersects another Rect 00207 /*! 00208 The rectangles intersect if any part of them overlaps. 00209 @param a Another Rect object to compare this Rect with 00210 @return true if the Rect objects overlap; otherwise, false 00211 */ 00212 bool intersects(const Rect &a) const; 00213 //! Determines if this Rect contains a point 00214 /*! 00215 The rectangle contains the point if it falls within the rectangle's 00216 boundaries. 00217 @param x The x coordinate of the point to operate on 00218 @param y The y coordinate of the point to operate on 00219 @return true if the point is contained within this Rect; otherwise, false 00220 */ 00221 bool contains(int x, int y) const; 00222 //! Determines if this Rect contains a point 00223 /*! 00224 The rectangle contains the point if it falls within the rectangle's 00225 boundaries. 00226 @param p The point to operate on 00227 @return true if the point is contained within this Rect; otherwise, false 00228 */ 00229 bool contains(const Point &p) const; 00230 //! Determines if this Rect contains another Rect entirely 00231 /*! 00232 This rectangle contains the second rectangle if it is entirely within this 00233 rectangle's boundaries. 00234 @param a The Rect to test for containment inside of this Rect 00235 @return true if the second Rect is contained within this Rect; otherwise, 00236 false 00237 */ 00238 bool contains(const Rect &a) const; 00239 00240 private: 00241 //! The left coordinate of the Rect 00242 int _x1; 00243 //! The top coordinate of the Rect 00244 int _y1; 00245 //! The right coordinate of the Rect 00246 int _x2; 00247 //! The bottom coordinate of the Rect 00248 int _y2; 00249 }; 00250 00251 //! A list for Rect objects 00252 typedef std::vector<Rect> RectList; 00253 00254 } 00255 00256 #endif // __rect_hh