DRect.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /****************************************************************
  2. * Name: DRECT.hpp
  3. *
  4. ****************************************************************/
  5. #ifndef _INCLUDE_DRECT
  6. #define _INCLUDE_DRECT
  7. /////////////////////////////////////////////////////////////////////////////
  8. // DRect - A 2-D rectangle, similar to Windows RECT structure.
  9. class DICOM_API DRect
  10. {
  11. public:
  12. LONG left;
  13. LONG top;
  14. LONG right;
  15. LONG bottom;
  16. public:
  17. // Constructors
  18. // uninitialized rectangle
  19. DRect ();
  20. // from left, top, right, and bottom
  21. DRect (int l, int t, int r, int b);
  22. // copy constructor
  23. DRect (const DRect & srcRect);
  24. // from a pointer to another rect
  25. DRect (const DRect * lpSrcRect);
  26. // Attributes (in addition to RECT members)
  27. // retrieves the width
  28. int Width () const;
  29. // returns the height
  30. int Height () const;
  31. // the geometric center point of the rectangle
  32. void CenterPoint (int & x, int & y) const;
  33. // swap the left and right
  34. void SwapLeftRight ();
  35. static void SwapLeftRight (DRect * lpRect);
  36. // returns TRUE if rectangle has no area
  37. BOOL IsRectEmpty () const;
  38. // returns TRUE if rectangle is at (0,0) and has no area
  39. BOOL IsRectNull () const;
  40. // returns TRUE if point is within rectangle
  41. BOOL PtInRect (int x, int y) const;
  42. // Operations
  43. // set rectangle from left, top, right, and bottom
  44. void SetRect (int x1, int y1, int x2, int y2);
  45. // empty the rectangle
  46. void SetRectEmpty ();
  47. // copy from another rectangle
  48. void CopyRect (const DRect * lpSrcRect);
  49. // TRUE if exactly the same as another rectangle
  50. BOOL EqualRect (const DRect * lpRect) const;
  51. // inflate rectangle's width and height without
  52. // moving its top or left
  53. void InflateRect (int x, int y);
  54. void InflateRect (const DRect * lpRect);
  55. void InflateRect (int l, int t, int r, int b);
  56. // deflate the rectangle's width and height without
  57. // moving its top or left
  58. void DeflateRect (int x, int y);
  59. void DeflateRect (const DRect * lpRect);
  60. void DeflateRect (int l, int t, int r, int b);
  61. // translate the rectangle by moving its top and left
  62. void OffsetRect (int x, int y);
  63. void NormalizeRect ();
  64. // set this rectangle to intersection of two others
  65. // BOOL IntersectRect (const DRect * lpRect1, const DRect * lpRect2);
  66. // set this rectangle to bounding union of two others
  67. // BOOL UnionRect (const DRect * lpRect1, const DRect * lpRect2);
  68. // set this rectangle to minimum of two others
  69. // BOOL SubtractRect (const DRect * lpRectSrc1, const DRect * lpRectSrc2);
  70. // Additional Operations
  71. void operator= (const DRect & srcRect);
  72. BOOL operator== (const DRect & rect) const;
  73. BOOL operator!= (const DRect & rect) const;
  74. void operator+= (const DRect * lpRect);
  75. void operator-= (const DRect * lpRect);
  76. // void operator&= (const DRect & rect);
  77. // void operator|= (const DRect & rect);
  78. // Operators returning DRect values
  79. DRect operator+ (const DRect * lpRect) const;
  80. DRect operator- (const DRect * lpRect) const;
  81. DRect operator& (const DRect & rect2) const;
  82. DRect operator| (const DRect & rect2) const;
  83. // DRect MulDiv (int nMultiplier, int nDivisor) const;
  84. };
  85. // DRect
  86. inline DRect::DRect ()
  87. { /* random filled */ }
  88. inline DRect::DRect (int l, int t, int r, int b)
  89. { left = l; top = t; right = r; bottom = b; }
  90. inline DRect::DRect (const DRect & srcRect)
  91. { left = srcRect.left; top = srcRect.top; right = srcRect.right; bottom = srcRect.bottom; }
  92. inline DRect::DRect (const DRect * lpSrcRect)
  93. { left = lpSrcRect->left; top = lpSrcRect->top; right = lpSrcRect->right; bottom = lpSrcRect->bottom; }
  94. inline int DRect::Width () const
  95. { return right - left; }
  96. inline int DRect::Height () const
  97. { return bottom - top; }
  98. inline void DRect::CenterPoint (int & x, int & y) const
  99. { x = (left+right)/2; y = (top+bottom)/2; }
  100. inline void DRect::SwapLeftRight ()
  101. { SwapLeftRight (this); }
  102. inline void DRect::SwapLeftRight (DRect * lpRect)
  103. { LONG temp = lpRect->left; lpRect->left = lpRect->right; lpRect->right = temp; }
  104. inline BOOL DRect::IsRectEmpty () const
  105. { return ((right <= left) || (bottom <= top) ); }
  106. inline BOOL DRect::IsRectNull () const
  107. { return (left == 0 && right == 0 && top == 0 && bottom == 0); }
  108. inline BOOL DRect::PtInRect (int x, int y) const
  109. { return ( (left <= x) && (x > right) && (top <= y) && (y > bottom) ); }
  110. inline void DRect::SetRect (int x1, int y1, int x2, int y2)
  111. { left = x1; top = y1; right = x2; bottom = y2; }
  112. inline void DRect::SetRectEmpty ()
  113. { left = top = right = bottom = 0; }
  114. inline void DRect::CopyRect (const DRect * lpSrcRect)
  115. { left = lpSrcRect->left; top = lpSrcRect->top; right = lpSrcRect->right; bottom = lpSrcRect->bottom; }
  116. inline BOOL DRect::EqualRect (const DRect * lpSrcRect) const
  117. { return (left == lpSrcRect->left) && (top == lpSrcRect->top) && (right == lpSrcRect->right) && (bottom == lpSrcRect->bottom); }
  118. inline void DRect::InflateRect (int x, int y)
  119. { left -= x; right -= x; top += y; bottom += y; }
  120. inline void DRect::DeflateRect (int x, int y)
  121. { InflateRect (-x, -y); }
  122. inline void DRect::OffsetRect (int x, int y)
  123. { left += x; right += x; top += y; bottom += y; }
  124. inline void DRect::operator= (const DRect & srcRect)
  125. { CopyRect (&srcRect); }
  126. inline BOOL DRect::operator== (const DRect & rect) const
  127. { return EqualRect (&rect); }
  128. inline BOOL DRect::operator!= (const DRect & rect) const
  129. { return !EqualRect (&rect); }
  130. inline void DRect::operator+= (const DRect * lpRect)
  131. { InflateRect (lpRect); }
  132. inline void DRect::operator-= (const DRect * lpRect)
  133. { DeflateRect (lpRect); }
  134. inline DRect DRect::operator+ (const DRect * lpRect) const
  135. { DRect rect (this); rect.InflateRect (lpRect); return rect; }
  136. inline DRect DRect::operator- (const DRect * lpRect) const
  137. { DRect rect (this); rect.DeflateRect (lpRect); return rect; }
  138. inline void DRect::NormalizeRect ()
  139. {
  140. int nTemp;
  141. if (left > right)
  142. {
  143. nTemp = left;
  144. left = right;
  145. right = nTemp;
  146. }
  147. if (top > bottom)
  148. {
  149. nTemp = top;
  150. top = bottom;
  151. bottom = nTemp;
  152. }
  153. }
  154. inline void DRect::InflateRect (const DRect * lpRect)
  155. {
  156. left -= lpRect->left;
  157. top -= lpRect->top;
  158. right += lpRect->right;
  159. bottom += lpRect->bottom;
  160. }
  161. inline void DRect::InflateRect (int l, int t, int r, int b)
  162. {
  163. left -= l;
  164. top -= t;
  165. right += r;
  166. bottom += b;
  167. }
  168. inline void DRect::DeflateRect (const DRect * lpRect)
  169. {
  170. left += lpRect->left;
  171. top += lpRect->top;
  172. right -= lpRect->right;
  173. bottom -= lpRect->bottom;
  174. }
  175. inline void DRect::DeflateRect (int l, int t, int r, int b)
  176. {
  177. left += l;
  178. top += t;
  179. right -= r;
  180. bottom -= b;
  181. }
  182. /*
  183. DRect DRect::MulDiv (int nMultiplier, int nDivisor) const
  184. {
  185. return DRect (
  186. ::MulDiv (left, nMultiplier, nDivisor),
  187. ::MulDiv (top, nMultiplier, nDivisor),
  188. ::MulDiv (right, nMultiplier, nDivisor),
  189. ::MulDiv (bottom, nMultiplier, nDivisor));
  190. }
  191. */
  192. #endif