trunk 20080912
[gitenigma.git] / lib / base / erect.cpp
blob28f2d79c848c7e84a5060e32e003aa12cb9a9e16
1 #include <lib/base/erect.h>
2 #include <iostream>
4 /*****************************************************************************
5 eRect member functions
6 *****************************************************************************/
8 eRect::eRect( const ePoint &topLeft, const ePoint &bottomRight )
10 x1 = topLeft.x();
11 y1 = topLeft.y();
12 x2 = bottomRight.x();
13 y2 = bottomRight.y();
16 eRect eRect::normalize() const
18 eRect r;
19 if ( x2 < x1 ) { // swap bad x values
20 r.x1 = x2;
21 r.x2 = x1;
22 } else {
23 r.x1 = x1;
24 r.x2 = x2;
26 if ( y2 < y1 ) { // swap bad y values
27 r.y1 = y2;
28 r.y2 = y1;
29 } else {
30 r.y1 = y1;
31 r.y2 = y2;
33 return r;
36 void eRect::rect( int *x, int *y, int *w, int *h ) const
38 *x = x1;
39 *y = y1;
40 *w = x2-x1;
41 *h = y2-y1;
44 void eRect::coords( int *xp1, int *yp1, int *xp2, int *yp2 ) const
46 *xp1 = x1;
47 *yp1 = y1;
48 *xp2 = x2;
49 *yp2 = y2;
52 void eRect::moveTopLeft( const ePoint &p )
54 x2 += (p.x() - x1);
55 y2 += (p.y() - y1);
56 x1 = p.x();
57 y1 = p.y();
60 void eRect::moveBottomRight( const ePoint &p )
62 x1 += (p.x() - x2);
63 y1 += (p.y() - y2);
64 x2 = p.x();
65 y2 = p.y();
68 void eRect::moveTopRight( const ePoint &p )
70 x1 += (p.x() - x2);
71 y2 += (p.y() - y1);
72 x2 = p.x();
73 y1 = p.y();
76 void eRect::moveBottomLeft( const ePoint &p )
78 x2 += (p.x() - x1);
79 y1 += (p.y() - y2);
80 x1 = p.x();
81 y2 = p.y();
84 void eRect::moveCenter( const ePoint &p )
86 int w = x2 - x1;
87 int h = y2 - y1;
88 x1 = (p.x() - w/2);
89 y1 = (p.y() - h/2);
90 x2 = x1 + w;
91 y2 = y1 + h;
94 void eRect::setRect( int x, int y, int w, int h )
96 x1 = x;
97 y1 = y;
98 x2 = (x+w);
99 y2 = (y+h);
102 void eRect::setCoords( int xp1, int yp1, int xp2, int yp2 )
104 x1 = xp1;
105 y1 = yp1;
106 x2 = xp2;
107 y2 = yp2;
110 void eRect::setWidth( int w )
112 x2 = x1 + w;
115 void eRect::setHeight( int h )
117 y2 = y1 + h;
120 void eRect::setSize( const eSize &s )
122 x2 = s.width() +x1;
123 y2 = s.height()+y1;
126 bool eRect::contains( const ePoint &p) const
128 return p.x() >= x1 && p.x() < x2 &&
129 p.y() >= y1 && p.y() < y2;
132 bool eRect::contains( const eRect &r) const
134 return r.x1 >= x1 &&
135 r.x2 <= x2 &&
136 r.y1 >= y1 &&
137 r.y2 <= y2;
140 eRect& eRect::operator|=(const eRect &r)
142 *this = *this | r;
143 return *this;
146 eRect& eRect::operator&=(const eRect &r)
148 *this = *this & r;
149 return *this;
152 eRect eRect::operator|(const eRect &r) const
154 if ( isValid() ) {
155 if ( r.isValid() ) {
156 eRect tmp;
157 tmp.setLeft( MIN( x1, r.x1 ) );
158 tmp.setRight( MAX( x2, r.x2 ) );
159 tmp.setTop( MIN( y1, r.y1 ) );
160 tmp.setBottom( MAX( y2, r.y2 ) );
161 return tmp;
162 } else {
163 return *this;
165 } else {
166 return r;
170 eRect eRect::unite( const eRect &r ) const
172 return *this | r;
175 eRect eRect::operator&( const eRect &r ) const
177 eRect tmp;
178 tmp.x1 = MAX( x1, r.x1 );
179 tmp.x2 = MIN( x2, r.x2 );
180 tmp.y1 = MAX( y1, r.y1 );
181 tmp.y2 = MIN( y2, r.y2 );
182 return tmp;
185 eRect eRect::intersect( const eRect &r ) const
187 return *this & r;
190 bool eRect::intersects( const eRect &r ) const
192 return ( MAX( x1, r.x1 ) < MIN( x2, r.x2 ) &&
193 MAX( y1, r.y1 ) < MIN( y2, r.y2 ) );
196 bool operator==( const eRect &r1, const eRect &r2 )
198 return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2;
201 bool operator!=( const eRect &r1, const eRect &r2 )
203 return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2;