trunk 20080912
[gitenigma.git] / include / lib / base / erect.h
blob46bae3f0d538d53ce71dd099958652fb2fff83e2
1 #ifndef ERECT_H
2 #define ERECT_H
4 #include <lib/base/esize.h>
5 #include <lib/base/epoint.h>
8 // x2 = x1 + width (AND NOT, NEVER, NEVER EVER +1 or -1 !!!!)
10 class eRect // rectangle class
12 public:
13 eRect() { x1 = y1 = x2 = y2 = 0; }
14 eRect( const ePoint &topleft, const ePoint &bottomright );
16 // we use this contructor very often... do it inline...
17 eRect( const ePoint &topleft, const eSize &size )
19 x1 = topleft.x();
20 y1 = topleft.y();
21 x2 = (x1+size.width());
22 y2 = (y1+size.height());
25 eRect( int left, int top, int width, int height );
27 bool isNull() const;
28 bool isEmpty() const;
29 bool isValid() const;
30 eRect normalize() const;
32 int left() const;
33 int top() const;
34 int right() const;
35 int bottom() const;
36 int &rLeft();
37 int &rTop();
38 int &rRight();
39 int &rBottom();
41 int x() const;
42 int y() const;
43 void setLeft( int pos );
44 void setTop( int pos );
45 void setRight( int pos );
46 void setBottom( int pos );
47 void setX( int x );
48 void setY( int y );
50 ePoint topLeft() const;
51 ePoint bottomRight() const;
52 ePoint topRight() const;
53 ePoint bottomLeft() const;
54 ePoint center() const;
56 void rect( int *x, int *y, int *w, int *h ) const;
57 void coords( int *x1, int *y1, int *x2, int *y2 ) const;
59 void moveTopLeft( const ePoint &p );
60 void moveBottomRight( const ePoint &p );
61 void moveTopRight( const ePoint &p );
62 void moveBottomLeft( const ePoint &p );
63 void moveCenter( const ePoint &p );
65 void moveBy( int dx, int dy )
67 x1 += dx;
68 y1 += dy;
69 x2 += dx;
70 y2 += dy;
73 void setRect( int x, int y, int w, int h );
74 void setCoords( int x1, int y1, int x2, int y2 );
76 eSize size() const;
77 int width() const;
78 int height() const;
79 void setWidth( int w );
80 void setHeight( int h );
81 void setSize( const eSize &s );
83 eRect operator|(const eRect &r) const;
84 eRect operator&(const eRect &r) const;
85 eRect& operator|=(const eRect &r);
86 eRect& operator&=(const eRect &r);
88 bool contains( const ePoint &p) const;
89 bool contains( int x, int y) const;
90 bool contains( const eRect &r) const;
91 eRect unite( const eRect &r ) const;
92 eRect intersect( const eRect &r ) const;
93 bool intersects( const eRect &r ) const;
95 friend bool operator==( const eRect &, const eRect & );
96 friend bool operator!=( const eRect &, const eRect & );
98 private:
99 int x1;
100 int y1;
101 int x2;
102 int y2;
105 bool operator==( const eRect &, const eRect & );
106 bool operator!=( const eRect &, const eRect & );
109 /*****************************************************************************
110 eRect stream functions
111 *****************************************************************************/
112 namespace std
114 inline ostream &operator<<( ostream & s, const eRect & r )
116 s << r.left() << r.top()
117 << r.right() << r.bottom();
119 return s;
122 inline istream &operator>>( istream & s, eRect & r )
124 int x1, y1, x2, y2;
125 s >> x1 >> y1 >> x2 >> y2;
126 r.setCoords( x1, y1, x2, y2 );
127 return s;
131 /*****************************************************************************
132 eRect inline member functions
133 *****************************************************************************/
135 inline eRect::eRect( int left, int top, int width, int height )
137 x1 = left;
138 y1 = top;
139 x2 = left+width;
140 y2 = top+height;
143 inline bool eRect::isNull() const
144 { return x2 == x1 && y2 == y1; }
146 inline bool eRect::isEmpty() const
147 { return x1 >= x2 || y1 >= y2; }
149 inline bool eRect::isValid() const
150 { return x1 <= x2 && y1 <= y2; }
152 inline int eRect::left() const
153 { return x1; }
155 inline int eRect::top() const
156 { return y1; }
158 inline int eRect::right() const
159 { return x2; }
161 inline int eRect::bottom() const
162 { return y2; }
164 inline int &eRect::rLeft()
165 { return x1; }
167 inline int & eRect::rTop()
168 { return y1; }
170 inline int & eRect::rRight()
171 { return x2; }
173 inline int & eRect::rBottom()
174 { return y2; }
176 inline int eRect::x() const
177 { return x1; }
179 inline int eRect::y() const
180 { return y1; }
182 inline void eRect::setLeft( int pos )
183 { x1 = pos; }
185 inline void eRect::setTop( int pos )
186 { y1 = pos; }
188 inline void eRect::setRight( int pos )
189 { x2 = pos; }
191 inline void eRect::setBottom( int pos )
192 { y2 = pos; }
194 inline void eRect::setX( int x )
195 { x1 = x; }
197 inline void eRect::setY( int y )
198 { y1 = y; }
200 inline ePoint eRect::topLeft() const
201 { return ePoint(x1, y1); }
203 inline ePoint eRect::bottomRight() const
204 { return ePoint(x2, y2); }
206 inline ePoint eRect::topRight() const
207 { return ePoint(x2, y1); }
209 inline ePoint eRect::bottomLeft() const
210 { return ePoint(x1, y2); }
212 inline ePoint eRect::center() const
213 { return ePoint((x1+x2)/2, (y1+y2)/2); }
215 inline int eRect::width() const
216 { return x2 - x1; }
218 inline int eRect::height() const
219 { return y2 - y1; }
221 inline eSize eRect::size() const
222 { return eSize(x2-x1, y2-y1); }
224 inline bool eRect::contains( int x, int y) const
226 return x >= x1 && x < x2 && y >= y1 && y < y2;
229 #endif // eRect_H