1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
14 //! The Rect class defines a rectangle in the plane.
17 //! Constructs an invalid Rect
18 inline Rect(void) : _x1(0), _y1(0), _x2(0), _y2(0) { }
21 @param x The x component of the point defining the top left corner of the
23 @param y The y component of the point defining the top left corner of the
25 @param w The width of the rectangle
26 @param h The height of the rectangle
28 inline Rect(int x, int y, int w, int h)
29 : _x1(x), _y1(y), _x2(w + x - 1), _y2(h + y - 1) { }
30 //! Constructs a Rect from 2 Point objects
32 @param location The point defining the top left corner of the rectangle
33 @param size The width and height of the rectangle
35 inline Rect(const Point &location, const Point &size)
36 : _x1(location.x()), _y1(location.y()),
37 _x2(size.x() + location.x() - 1), _y2(size.y() + location.y() - 1) { }
38 //! Constructs a Rect from another Rect
40 @param rect The rectangle from which to construct this new one
42 inline Rect(const Rect &rect)
43 : _x1(rect._x1), _y1(rect._y1), _x2(rect._x2), _y2(rect._y2) { }
44 //! Constructs a Rect from an XRectangle
45 inline explicit Rect(const XRectangle& xrect)
46 : _x1(xrect.x), _y1(xrect.y), _x2(xrect.width + xrect.x - 1),
47 _y2(xrect.height + xrect.y - 1) { }
49 //! Returns the left coordinate of the Rect. Identical to Rect::x.
50 inline int left(void) const { return _x1; }
51 //! Returns the top coordinate of the Rect. Identical to Rect::y.
52 inline int top(void) const { return _y1; }
53 //! Returns the right coordinate of the Rect
54 inline int right(void) const { return _x2; }
55 //! Returns the bottom coordinate of the Rect
56 inline int bottom(void) const { return _y2; }
58 //! The x component of the point defining the top left corner of the Rect
59 inline int x(void) const { return _x1; }
60 //! The y component of the point defining the top left corner of the Rect
61 inline int y(void) const { return _y1; }
62 //! Returns the Point that defines the top left corner of the rectangle
63 inline Point location() const { return Point(_x1, _y1); }
65 //! Sets the x coordinate of the Rect.
67 @param x The new x component of the point defining the top left corner of
71 //! Sets the y coordinate of the Rect.
73 @param y The new y component of the point defining the top left corner of
77 //! Sets the x and y coordinates of the Rect.
79 @param x The new x component of the point defining the top left corner of
81 @param y The new y component of the point defining the top left corner of
84 void setPos(int x, int y);
85 //! Sets the x and y coordinates of the Rect.
87 @param location The point defining the top left corner of the rectangle.
89 void setPos(const Point &location);
91 //! The width of the Rect
92 inline int width(void) const { return _x2 - _x1 + 1; }
93 //! The height of the Rect
94 inline int height(void) const { return _y2 - _y1 + 1; }
95 //! Returns the size of the Rect
96 inline Point size() const { return Point(_x2 - _x1 + 1, _y2 - _y1 + 1); }
98 //! Sets the width of the Rect
100 @param w The new width of the rectangle
102 void setWidth(int w);
103 //! Sets the height of the Rect
105 @param h The new height of the rectangle
107 void setHeight(int h);
108 //! Sets the size of the Rect.
110 @param w The new width of the rectangle
111 @param h The new height of the rectangle
113 void setSize(int w, int h);
114 //! Sets the size of the Rect.
116 @param size The new size of the rectangle
118 void setSize(const Point &size);
120 //! Sets the position and size of the Rect
122 @param x The new x component of the point defining the top left corner of
124 @param y The new y component of the point defining the top left corner of
126 @param w The new width of the rectangle
127 @param h The new height of the rectangle
129 void setRect(int x, int y, int w, int h);
130 //! Sets the position and size of the Rect
132 @param location The new point defining the top left corner of the rectangle
133 @param size The new size of the rectangle
135 void setRect(const Point &location, const Point &size);
137 //! Sets the position of all 4 sides of the Rect
139 @param l The new left coordinate of the rectangle
140 @param t The new top coordinate of the rectangle
141 @param r The new right coordinate of the rectangle
142 @param b The new bottom coordinate of the rectangle
144 void setCoords(int l, int t, int r, int b);
145 //! Sets the position of all 4 sides of the Rect
147 @param tl The new point at the top left of the rectangle
148 @param br The new point at the bottom right of the rectangle
150 void setCoords(const Point &tl, const Point &br);
152 //! Determines if two Rect objects are equal
154 The rectangles are considered equal if they are in the same position and
157 inline bool operator==(const Rect &a)
158 { return _x1 == a._x1 && _y1 == a._y1 && _x2 == a._x2 && _y2 == a._y2; }
159 //! Determines if two Rect objects are inequal
163 inline bool operator!=(const Rect &a) { return ! operator==(a); }
165 //! Returns the union of two Rect objects
167 The union of the rectangles will consist of the maximimum area that the two
168 rectangles can make up.
169 @param a A second Rect object to form a union with.
171 Rect operator|(const Rect &a) const;
172 //! Returns the intersection of two Rect objects
174 The intersection of the rectangles will consist of just the area where the
175 two rectangles overlap.
176 @param a A second Rect object to form an intersection with.
177 @return The intersection between this Rect and the one passed to the
180 Rect operator&(const Rect &a) const;
181 //! Sets the Rect to the union of itself with another Rect object
183 The union of the rectangles will consist of the maximimum area that the two
184 rectangles can make up.
185 @param a A second Rect object to form a union with.
186 @return The union between this Rect and the one passed to the function
188 inline Rect &operator|=(const Rect &a) { *this = *this | a; return *this; }
189 //! Sets the Rect to the intersection of itself with another Rect object
191 The intersection of the rectangles will consist of just the area where the
192 two rectangles overlap.
193 @param a A second Rect object to form an intersection with.
195 inline Rect &operator&=(const Rect &a) { *this = *this & a; return *this; }
197 //! Returns if the Rect is valid
199 A rectangle is valid only if its right and bottom coordinates are larger
200 than its left and top coordinates (i.e. it does not have a negative width
202 @return true if the Rect is valid; otherwise, false
204 inline bool valid(void) const { return _x2 > _x1 && _y2 > _y1; }
206 //! Determines if this Rect intersects another Rect
208 The rectangles intersect if any part of them overlaps.
209 @param a Another Rect object to compare this Rect with
210 @return true if the Rect objects overlap; otherwise, false
212 bool intersects(const Rect &a) const;
213 //! Determines if this Rect contains a point
215 The rectangle contains the point if it falls within the rectangle's
217 @param x The x coordinate of the point to operate on
218 @param y The y coordinate of the point to operate on
219 @return true if the point is contained within this Rect; otherwise, false
221 bool contains(int x, int y) const;
222 //! Determines if this Rect contains a point
224 The rectangle contains the point if it falls within the rectangle's
226 @param p The point to operate on
227 @return true if the point is contained within this Rect; otherwise, false
229 bool contains(const Point &p) const;
230 //! Determines if this Rect contains another Rect entirely
232 This rectangle contains the second rectangle if it is entirely within this
233 rectangle's boundaries.
234 @param a The Rect to test for containment inside of this Rect
235 @return true if the second Rect is contained within this Rect; otherwise,
238 bool contains(const Rect &a) const;
241 //! The left coordinate of the Rect
243 //! The top coordinate of the Rect
245 //! The right coordinate of the Rect
247 //! The bottom coordinate of the Rect
251 //! A list for Rect objects
252 typedef std::vector<Rect> RectList;