};
+class rect
+{
+public:
+ rect( int v_x = 0, int v_y = 0, int v_width = 0, int v_height = 0 )
+ : x( v_x ), y( v_y ), width( v_width ), height( v_height ) { }
+ ~rect() { }
+ int x;
+ int y;
+ int width;
+ int height;
+
+ // rect2 fully included in rect1
+ static bool isIncluded( rect& rect2, rect& rect1 )
+ {
+ int x1 = rect1.x;
+ int y1 = rect1.y;
+ int w1 = rect1.width;
+ int h1 = rect1.height;
+
+ int x2 = rect2.x;
+ int y2 = rect2.y;
+ int w2 = rect2.width;
+ int h2 = rect2.height;
+
+ return x2 >= x1 && x2 < x1 + w1
+ && y2 >= y1 && y2 < y1 + h1
+ && w2 <= w1
+ && h2 <= h1;
+ }
+
+ static bool areDisjunct( rect& rect2, rect& rect1 )
+ {
+ int x1 = rect1.x;
+ int y1 = rect1.y;
+ int w1 = rect1.width;
+ int h1 = rect1.height;
+
+ int x2 = rect2.x;
+ int y2 = rect2.y;
+ int w2 = rect2.width;
+ int h2 = rect2.height;
+
+ return y2 + h2 -1 < y1 // rect2 above rect1
+ || y2 > y1 + h1 - 1 // rect2 under rect1
+ || x2 > x1 + w1 -1 // rect2 right of rect1
+ || x2 + w2 - 1 < x1; // rect2 left of rect1
+ }
+
+ static bool intersect( rect& rect1, rect& rect2, rect* pRect )
+ {
+ int x1 = rect1.x;
+ int y1 = rect1.y;
+ int w1 = rect1.width;
+ int h1 = rect1.height;
+
+ int x2 = rect2.x;
+ int y2 = rect2.y;
+ int w2 = rect2.width;
+ int h2 = rect2.height;
+
+ if( areDisjunct( rect1, rect2 ) )
+ return false;
+ else
+ {
+ int left = max( x1, x2 );
+ int right = min( x1 + w1 - 1, x2 + w2 - 1 );
+ int top = max( y1, y2 );
+ int bottom = min( y1 + h1 - 1, y2 + h2 -1 );
+ pRect->x = left;
+ pRect->y = top;
+ pRect->width = right - left + 1;
+ pRect->height = bottom - top + 1;
+
+ return pRect->width > 0 && pRect->height > 0;
+ }
+ }
+
+ static bool join( rect& rect1, rect& rect2, rect* pRect )
+ {
+ int x1 = rect1.x;
+ int y1 = rect1.y;
+ int w1 = rect1.width;
+ int h1 = rect1.height;
+
+ int x2 = rect2.x;
+ int y2 = rect2.y;
+ int w2 = rect2.width;
+ int h2 = rect2.height;
+
+ int left = min( x1, x2 );
+ int right = max( x1 + w1 - 1, x2 + w2 - 1 );
+ int top = min( y1, y2 );
+ int bottom = max( y1 + h1 - 1, y2 + h2 -1 );
+ pRect->x = left;
+ pRect->y = top;
+ pRect->width = right - left + 1;
+ pRect->height = bottom - top + 1;
+
+ return pRect->width > 0 && pRect->height > 0;
+ }
+ static int min( int x, int y ) { return x < y ? x : y; }
+ static int max( int x, int y ) { return x < y ? y : x; }
+
+};
+
+
#endif