home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / pascal / rehack / general / misc.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-29  |  5.0 KB  |  263 lines

  1. #ifndef _MISC_HPP
  2. #define _MISC_HPP
  3.  
  4. // ------------------------------------------------------------------
  5. // File:        MISC.HPP
  6. // Path:        ...\REHACK\GENERAL\MISC.HPP
  7. // Version:        0.01
  8. // Author:        Pat Reilly
  9. // CIS Id:        71333,2764
  10. // Created On:    6/18/93
  11. // Modified On:    6/27/93
  12. // Description:    Miscellaneous REHACK classes and definitions; Point,
  13. //            Rect. See MISC.TXT for more details.
  14. // Tabs:        4
  15. // ------------------------------------------------------------------
  16.  
  17. #ifndef _TYPES_HPP
  18. #include "..\GENERAL\TYPES.HPP"
  19. #endif
  20.  
  21. // Class Point
  22.  
  23. struct Point
  24. {
  25.     int x,y;
  26.  
  27.     Point& operator += (const Point& v);
  28.     Point& operator -= (const Point& v);
  29.     Point  operator -  () const;
  30.     Point  operator +  () const;
  31. };
  32.  
  33. // In-line definitions.
  34.  
  35. inline Point& Point::operator += (const Point& p)
  36. {
  37.     x += p.x;
  38.     y += p.y;
  39.     return *this;
  40. }
  41.  
  42. inline Point& Point::operator -= (const Point& p)
  43. {
  44.     x -= p.x;
  45.     y -= p.y;
  46.     return *this;
  47. }
  48.  
  49. inline Point Point::operator - () const
  50. {
  51.     Point t;
  52.     t.x = -x;
  53.     t.y = -y;
  54.     return t;
  55. }
  56.  
  57. inline Point Point::operator + () const
  58. {
  59.     Point t;
  60.     t = *this;
  61.     return t;
  62. }
  63.  
  64. // Related operators.
  65.  
  66. inline bool operator == (const Point& p1, const Point& p2)
  67. {
  68.     return bool(p1.x == p2.x && p1.y == p2.y);
  69. }
  70.  
  71. inline bool operator != (const Point& p1, const Point& p2)
  72. {
  73.     return bool(p1.x != p2.x || p1.y != p2.y);
  74. }
  75.  
  76. inline Point operator + (const Point& point, const Point& p)
  77. {
  78.     Point pt = point;
  79.     pt += p;
  80.     return pt;
  81. }
  82.  
  83. inline Point operator - (const Point& point, const Point& p)
  84. {
  85.     Point pt = point;
  86.     pt -= p;
  87.     return pt;
  88. }
  89.  
  90. // Class Rect
  91.  
  92. struct Rect
  93. {
  94.     Point topLeft,bottomRight;
  95.  
  96.     Rect();
  97.     Rect(int, int, int, int);
  98.     Rect(const Point&, const Point&);
  99.     Rect(const Rect&);
  100.  
  101.     bool isEmpty() const;
  102.     bool contains(const Point&) const;
  103.     void intersectWith(const Rect&);
  104.     void unionWith(const Rect&);
  105.     void move(const Point&);
  106.     int width() const;
  107.     int height() const;
  108.  
  109.     bool operator == (const Rect&) const;
  110.     bool operator != (const Rect&) const;
  111.     Rect& operator += (const Point&);
  112.     Rect& operator -= (const Point&);
  113.     Rect& operator |= (const Rect&);
  114.     Rect& operator &= (const Rect&);
  115. };
  116.  
  117. inline Rect::Rect()
  118. {}
  119.  
  120. inline Rect::Rect(int left, int top, int right, int bottom)
  121. {
  122.     topLeft.x = left;
  123.     topLeft.y = top;
  124.     bottomRight.x = right;
  125.     bottomRight.y = bottom;
  126. }
  127.  
  128. inline Rect::Rect(const Point& tl, const Point& br)
  129. {
  130.     topLeft = tl;
  131.     bottomRight = br;
  132. }
  133.  
  134. inline Rect::Rect(const Rect& rect)
  135. {
  136.     topLeft = rect.topLeft;
  137.     bottomRight = rect.bottomRight;
  138. }
  139.  
  140. inline bool Rect::isEmpty() const
  141. {
  142.     return bool(bottomRight.x <= topLeft.x || bottomRight.y <= topLeft.y);
  143. }
  144.  
  145. inline bool Rect::contains(const Point& point) const
  146. {
  147.     return bool(point.x >= topLeft.x && point.x < bottomRight.x &&
  148.                    point.y >= topLeft.y && point.y < bottomRight.y);
  149. }
  150.  
  151. inline void Rect::intersectWith(const Rect& rect)
  152. {
  153.     if(rect.topLeft.x > topLeft.x)
  154.         topLeft.x = rect.topLeft.x;
  155.     if(rect.bottomRight.x < bottomRight.x)
  156.         bottomRight.x = rect.bottomRight.x;
  157.     if(rect.topLeft.y > topLeft.y)
  158.         topLeft.y = rect.topLeft.y;
  159.     if(rect.bottomRight.y < bottomRight.y)
  160.         bottomRight.y = rect.bottomRight.y;
  161. }
  162.  
  163. inline void Rect::unionWith(const Rect& rect)
  164. {
  165.     // Handle special case of *this or rect being empty
  166.     if(isEmpty())
  167.         *this = rect;
  168.     else if(!rect.isEmpty())
  169.         {
  170.         if(rect.topLeft.x < topLeft.x)
  171.             topLeft.x = rect.topLeft.x;
  172.         if(rect.bottomRight.x > bottomRight.x)
  173.             bottomRight.x = rect.bottomRight.x;
  174.         if(rect.topLeft.y < topLeft.y)
  175.             topLeft.y = rect.topLeft.y;
  176.         if(rect.bottomRight.y > bottomRight.y)
  177.             bottomRight.y = rect.bottomRight.y;
  178.         }
  179. }
  180.  
  181. inline void Rect::move(const Point& point)
  182. {
  183.     topLeft += point;
  184.     bottomRight += point;
  185. }
  186.  
  187. inline int Rect::width() const
  188. {
  189.     int n = bottomRight.x - topLeft.x;
  190.     return (n >= 0) ? n : 0;
  191. }
  192.  
  193. inline int Rect::height() const
  194. {
  195.     int n = bottomRight.y - topLeft.y;
  196.     return (n >= 0) ? n : 0;
  197. }
  198.  
  199. inline bool Rect::operator == (const Rect& rect) const
  200. {
  201.     return bool(topLeft == rect.topLeft && bottomRight == rect.bottomRight);
  202. }
  203.  
  204. inline bool Rect::operator != (const Rect& rect) const
  205. {
  206.     return bool(topLeft != rect.topLeft || bottomRight != rect.bottomRight);
  207. }
  208.  
  209. inline Rect& Rect::operator += (const Point& point)
  210. {
  211.     move(point);
  212.     return *this;
  213. }
  214.  
  215. inline Rect& Rect::operator -= (const Point& point)
  216. {
  217.     move(-point);
  218.     return *this;
  219. }
  220.  
  221. inline Rect& Rect::operator |= (const Rect& rect)
  222. {
  223.     unionWith(rect);
  224.     return *this;
  225. }
  226.  
  227. inline Rect& Rect::operator &= (const Rect& rect)
  228. {
  229.     intersectWith(rect);
  230.     return *this;
  231. }
  232.  
  233. // Related operators.
  234. inline Rect operator + (const Rect& rect, const Point& point)
  235. {
  236.     Rect r = rect;
  237.     r += point;
  238.     return r;
  239. }
  240.  
  241. inline Rect operator - (const Rect& rect, const Point& point)
  242. {
  243.     Rect r = rect;
  244.     r -= point;
  245.     return r;
  246. }
  247.  
  248. inline Rect operator | (const Rect& r1, const Rect& r2)
  249. {
  250.     Rect r = r1;
  251.     r |= r2;
  252.     return r;
  253. }
  254.  
  255. inline Rect operator & (const Rect& r1, const Rect& r2)
  256. {
  257.     Rect r = r1;
  258.     r &= r2;
  259.     return r;
  260. }
  261.  
  262. #endif    // _MISC_HPP
  263.