00001
00007 #ifndef RECT_H
00008 #define RECT_H
00009
00013 #ifdef _WINDOWS_
00014 typedef RECT Rect;
00015 typedef POINT XY;
00016 #else
00017 typedef struct
00018 {
00019 int left;
00020 int top;
00021 int right;
00022 int bottom;
00023 } Rect;
00027 typedef struct
00028 {
00029 int x;
00030 int y;
00031 } XY;
00032 #endif
00033
00037 #define RectAutoXYWH(x,y,w,h) { (x), (y), (x) + (w), (y) + (h) }
00038
00042 #define RectSetXYWH(r,x,y,w,h) { (r).left = (x); (r).top = (y); (r).right = (x) + (w); (r).bottom = (y) + (h); }
00043
00047 #define RectAutoLTRB(l,t,r,b) { (l), (t), (r), (b) }
00048
00052 #define RectSetLTRB(rc,l,t,r,b) { (rc).left = (l); (rc).top = (t); (rc).right = (r); (rc).bottom = (b); }
00053
00057 #define RectAutoClear() { 0,0,0,0 }
00058
00062 #define RectClear(r) { (r).left = (r).top = (r).right = (r).bottom = 0; }
00063
00068 #define RectAutoMax() { (-(int)(~0u>>2)),(-(int)(~0u>>2)), ((int)(~0u>>2)),((int)(~0u>>2)) }
00069
00074 #define RectSetMax(r) { (r).left = (r).top = (-(int)(~0u>>2)); (r).right = (r).bottom = (int)(~0u>>2); }
00075
00079 #define RectEmpty(r) ( RectWide(r) < 1 || RectHigh(r) < 1 )
00080
00084 #define RectWide(r) ((r).right - (r).left)
00085
00089 #define RectHigh(r) ((r).bottom - (r).top)
00090
00094 #define RectPtInRect(r,x,y) ( (x)>=(r).left && (x)<(r).right && (y)>=(r).top && (y)<(r).bottom )
00095
00099 #define RectEqual(r1,r2) ((r1).left == (r2).left && (r1).top == (r2).top && (r1).right == (r2).right && (r1).bottom == (r2).bottom )
00100
00104 #define RectInRect(r,r2) (!RectOutside(r,r2))
00105
00109 #define RectOutside(r,r2) ( ((r).left>=(r2).right) || ((r).right<=(r2).left) || ((r).top>=(r2).bottom) || ((r).bottom<=(r2).top) )
00110
00114 #define RectContainsRect(r,r2) ( ((r2).left>=(r).left) && ((r2).top>=(r).top) && ((r2).right<=(r).right) && ((r2).bottom<=(r).bottom) )
00115
00119 #define RectCenterX(r) (((r).left+(r).right)/2)
00120
00124 #define RectCenterY(r) (((r).top+(r).bottom)/2)
00125
00129 #define RectCenterPt(r,x,y) { (x)=RectCenterX(r); (y)=RectCenterY(r); }
00130
00134 #define RectSetWide(r,w) ((r).right=(r).left+(w))
00135
00139 #define RectSetHigh(r,h) ((r).bottom=(r).top+(h))
00140
00144 #define RectLeftTo(r,x) {int _m_x = (x); int _m_tmp=RectWide(r); (r).left=_m_x; (r).right=_m_x+_m_tmp; }
00145
00149 #define RectTopTo(r,y) {int _m_y = (y); int _m_tmp=RectHigh(r); (r).top=_m_y; (r).bottom=_m_y+_m_tmp; }
00150
00154 #define RectRightTo(r,x) {int _m_x = (x); int _m_tmp=RectWide(r); (r).left=_m_x-_m_tmp;(r).right=_m_x;}
00155
00159 #define RectBottomTo(r,y) {int _m_y = (y); int _m_tmp=RectHigh(r); (r).top=_m_y-_m_tmp;(r).bottom=_m_y;}
00160
00164 #define RectMove(r,x,y) {RectLeftTo(r,x); RectTopTo(r,y);}
00165
00169 #define RectMoveBy(r,x,y) { (r).left += (x); (r).right += (x); (r).top += (y); (r).bottom += (y); }
00170
00174 #define RectGrow(r,x,y) { (r).left -= (x); (r).right += (x); (r).top -= (y); (r).bottom += (y); }
00175
00179 #define RectShrink(r,x,y) { (r).left += (x); (r).right -= (x); (r).top += (y); (r).bottom -= (y); }
00180
00184 #define RectHAlign(r,pow2) { const int rha_mask=~((pow2)-1); (r).left &= rha_mask; (r).right = ((r).right+~rha_mask) & rha_mask; }
00185
00189 #define RectVAlign(r,pow2) { const int rha_mask=~((pow2)-1); (r).top &= rha_mask; (r).bottom = ((r).bottom+~rha_mask) & rha_mask; }
00190
00194 #define RectUnionRect(r,r2) \
00195 { \
00196 if(RectEmpty(r))\
00197 {\
00198 (r)=(r2);\
00199 }\
00200 else if(!RectEmpty(r2))\
00201 {\
00202 if( (r).left > (r2).left ) (r).left = (r2).left;\
00203 if( (r).top > (r2).top ) (r).top = (r2).top;\
00204 if( (r).right < (r2).right ) (r).right = (r2).right;\
00205 if( (r).bottom < (r2).bottom ) (r).bottom = (r2).bottom;\
00206 }\
00207 }
00208
00212 #define RectUnionPt(r,x,y) \
00213 {\
00214 if(RectEmpty(r))\
00215 {\
00216 (r).left=(x);\
00217 (r).top=(y);\
00218 (r).right=(x)+1;\
00219 (r).bottom=(y)+1;\
00220 }\
00221 else\
00222 {\
00223 if( (r).left > (x) ) (r).left = (x);\
00224 if( (r).top > (y) ) (r).top = (y);\
00225 if( (r).right < (x) ) (r).right = (x);\
00226 if( (r).bottom < (y) ) (r).bottom = (y);\
00227 }\
00228 }
00229
00233 #define RectCenterOnX(r,x) RectLeftTo(r,(x)-(RectWide(r)/2))
00234
00238 #define RectCenterOnY(r,y) RectTopTo(r,(y)-(RectHigh(r)/2))
00239
00243 #define RectCenterOnPt(r,x,y) { int _m_mx = (x)-(RectWide(r)/2); int _m_my = (y)-(RectHigh(r)/2); RectMove(r,_m_mx,_m_my); }
00244
00248 #define RectCenterOnRect(r,r2) { int _m_x,_m_y; RectCenterPt(r2,_m_x,_m_y); RectCenterOnPt((r),_m_x,_m_y); }
00249
00254 #define RectIntRect(r,r2) \
00255 {\
00256 if( (r).left < (r2).left ) (r).left = (r2).left;\
00257 if( (r).top < (r2).top ) (r).top = (r2).top;\
00258 if( (r).right > (r2).right ) (r).right = (r2).right;\
00259 if( (r).bottom > (r2).bottom ) (r).bottom = (r2).bottom;\
00260 }
00261
00266 #define RectBoundtoRect(r,rb) \
00267 {\
00268 if((r).right>(rb).right) \
00269 RectRightTo(r,(rb).right);\
00270 if( (r).bottom > (rb).bottom ) \
00271 RectBottomTo(r,(rb).bottom);\
00272 if( (r).left < (rb).left ) \
00273 RectLeftTo( r,(rb).left );\
00274 if( (r).top < (rb).top ) \
00275 RectTopTo( r,(rb).top );\
00276 }
00277
00278
00279
00280
00281
00282
00289 #define IXY_X(decl) IXY_X_##decl
00290 #define IXY_X_X(v) (v)
00291 #define IXY_X_Y(v)
00292 #define IXY_Y(decl) IXY_Y_##decl
00293 #define IXY_Y_X(v)
00294 #define IXY_Y_Y(v) (v)
00295
00302 #define ILTRB_L(decl) ILTRB_L_##decl
00303 #define ILTRB_L_L(v) (v)
00304 #define ILTRB_L_T(v)
00305 #define ILTRB_L_R(v)
00306 #define ILTRB_L_B(v)
00307 #define ILTRB_T(decl) ILTRB_T_##decl
00308 #define ILTRB_T_L(v)
00309 #define ILTRB_T_T(v) (v)
00310 #define ILTRB_T_R(v)
00311 #define ILTRB_T_B(v)
00312 #define ILTRB_R(decl) ILTRB_R_##decl
00313 #define ILTRB_R_L(v)
00314 #define ILTRB_R_T(v)
00315 #define ILTRB_R_R(v) (v)
00316 #define ILTRB_R_B(v)
00317 #define ILTRB_B(decl) ILTRB_B_##decl
00318 #define ILTRB_B_L(v)
00319 #define ILTRB_B_T(v)
00320 #define ILTRB_B_R(v)
00321 #define ILTRB_B_B(v) (v)
00322
00328 #define ILTRB_WIDE(ir) (ir(ILTRB_R)-ir(ILTRB_L))
00329
00335 #define ILTRB_HIGH(ir) (ir(ILTRB_B)-ir(ILTRB_T))
00336
00342 #define ILTRB_CX(ir) ((ir(ILTRB_L)+ir(ILTRB_R))/2)
00343
00349 #define ILTRB_CY(ir) ((ir(ILTRB_T)+ir(ILTRB_B))/2)
00350
00358 #define ILTRB_SCALEX(ir,e,d) (ir(ILTRB_L)+((e)*ILTRB_WIDE(ir))/(d)))
00359
00367 #define ILTRB_SCALEY(ir,e,d) (ir(ILTRB_T)+((e)*ILTRB_HIGH(ir)/(d)))
00368
00375 #define XYIn_LTRB( xy, ir ) ( (xy).x >= ir(ILTRB_L) && (xy).x < ir(ILTRB_R) && (xy).y >= ir(ILTRB_T) && (xy).y < ir(ILTRB_B) )
00376
00384 #define PtIn_LTRB( x, y, ir ) ( (x) >= ir(ILTRB_L) && (x) < ir(ILTRB_R) && (y) >= ir(ILTRB_T) && (y) < ir(ILTRB_B) )
00385
00392 #define RectOutside_LTRB( r, ir ) ( ((r).left>=ir(ILTRB_R)) || ((r).right<=ir(ILTRB_L)) || ((r).top>=ir(ILTRB_B)) || ((r).bottom<=ir(ILTRB_T)) )
00393
00400 #define RectIn_LTRB( r, ir ) ( !RectOutside_LTRB( r, ir ) )
00401
00405 #define XY_AUTO_LT(ir) { ir(ILTRB_L), ir(ILTRB_T) }
00406 #define XY_AUTO_RB(ir) { ir(ILTRB_R)-1, ir(ILTRB_B)-1 }
00407 #define XY_AUTO_CTR(ir) { ILTRB_CX(ir), ILTRB_CY(ir) }
00408 #define XY_AUTO(ixy) { ixy(IXY_X), ixy(IXY_Y) }
00409 #define LTRB_AUTO(ir) { ir(ILTRB_L), ir(ILTRB_T), ir(ILTRB_R), ir(ILTRB_B) }
00410 #define LTRB_AUTO_IXY(ixy0,ixy1) { ixy0(IXY_X), ixy0(IXY_Y), ixy1(IXY_X), ixy1(IXY_Y) }
00411
00412 #endif
00413
00414