home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / OOD / PRIM.H$ / PRIM
Encoding:
Text File  |  1991-10-22  |  4.9 KB  |  212 lines

  1. /*********************************************************************
  2.  
  3.  FILE: PRIM.H
  4.  
  5.  Defines primitive types Point and Rect.
  6.  
  7. **********************************************************************/
  8.  
  9. #if !defined( _PRIM_H_ )
  10.  
  11. #define _PRIM_H_
  12.  
  13. #define self (*this)
  14.  
  15. #define max(a,b) ((a) > (b) ? (a) : (b))
  16. #define min(a,b) ((a) < (b) ? (a) : (b))
  17.  
  18.  
  19. /*********************************************************************
  20.  
  21.  Point
  22.  
  23.  Describes two-dimensional points.
  24.  
  25.  Public Interface:
  26.  
  27.     Point - constructor taking no args
  28.     Point - constructor taking two integers
  29.     x, y - coordinates
  30.     operator==, operator!= - equality test
  31.     operator+, operator+= - addition
  32.     operator-, operator-= - subtraction
  33.     operator*, operator*= - multiplication (scale)
  34.     operator/, operator/= - division (scale)
  35.  
  36.  
  37. *********************************************************************/
  38.  
  39.  
  40. class Point
  41. {
  42. public:
  43.     Point();
  44.     Point( int xT, int yT );
  45.     // use default copy-constructor (memberwise initialization)
  46.     // use default operator= (memberwise assignment)
  47.     int   operator==( Point pt ) const;
  48.     int   operator!=( Point pt ) const;
  49.     Point operator+( Point pt ) const;
  50.     Point operator+=( Point pt );
  51.     Point operator-( Point pt ) const;
  52.     Point operator-=( Point pt );
  53.     Point operator*( Point pt ) const;
  54.     Point operator*=( Point pt );
  55.     Point operator/( Point pt ) const;
  56.     Point operator/=( Point pt );
  57.  
  58. // data members
  59.     int   x;
  60.     int   y;
  61. };
  62.  
  63. inline Point::Point()
  64. {
  65.     x = y = 0;
  66. }
  67.  
  68. inline Point::Point( int xT, int yT )
  69. {
  70.     x = xT;
  71.     y = yT;
  72. }
  73.  
  74. inline int Point::operator==( Point pt ) const
  75. {
  76.     return (x == pt.x && y == pt.y);
  77. }
  78.  
  79. inline int Point::operator!=( Point pt ) const
  80. {
  81.     return (x != pt.x || y != pt.y);
  82. }
  83.  
  84. #define _PointOp(_op_)  \
  85. inline Point Point::operator _op_ ( Point pt ) const \
  86.     { return Point(x _op_ pt.x, y _op_ pt.y); }      \
  87. inline Point Point::operator _op_##= ( Point pt )    \
  88.     { x _op_##= pt.x; y _op_##= pt.y; return self; }
  89.  
  90. _PointOp(+)
  91. _PointOp(-)
  92. _PointOp(*)
  93. _PointOp(/)
  94.  
  95.  
  96. /**********************************************************************
  97.  
  98.  Rect
  99.  
  100.  Describes rectangles.
  101.  
  102.  Public Interface:
  103.  
  104.     Rect - constructor taking no args
  105.     Rect - constructor taking four integers
  106.     left, right, top, bottom - coordinates
  107.     width, length - return dimension
  108.     operator==, operator!= - equality test
  109.     isEmpty - tests for null rectangle
  110.     Contains - tests if point is within rectangle border
  111.     operator-, operator-=,
  112.     operator+, operator+= - offset rectangle by a vector (use Point)
  113.     operator&, operator&= - intersection of two rectangles
  114.     operator|, operator|= - union of two rectangles
  115.  
  116. ***********************************************************************/
  117.  
  118. class Rect
  119. {
  120. public:
  121.     Rect();
  122.     Rect( int leftT, int topT, int rightT, int bottomT );
  123.     Rect( Point topLeft, Point bottomRight );
  124.     // use default copy-constructor (memberwise initialization)
  125.     // use default operator= (memberwise assignment)
  126.     int  width() const;
  127.     int  height() const;
  128.     int  operator==( const Rect& r ) const;
  129.     int  operator!=( const Rect& r ) const;
  130.     int  isEmpty() const;
  131.     int  Contains( Point pt ) const;
  132.     Rect operator-( Point pt ) const;
  133.     Rect operator-=( Point pt );
  134.     Rect operator+( Point pt ) const;
  135.     Rect operator+=( Point pt );
  136.     Rect operator&( const Rect& other ) const;
  137.     Rect operator&=( const Rect& other );
  138.     Rect operator|( const Rect& other ) const;
  139.     Rect operator|=( const Rect& other );
  140.  
  141. // data members
  142.     int  left;
  143.     int  top;
  144.     int  right;
  145.     int  bottom;
  146. };
  147.  
  148. inline Rect::Rect()
  149. {
  150.     left = top = right = bottom = 0;
  151. }
  152.  
  153. // construct from two points (NOT x,y,dx,dy !!!)
  154. inline Rect::Rect( int leftT, int topT, int rightT, int bottomT )
  155. {
  156.     left = leftT;
  157.     top = topT;
  158.     right = rightT;
  159.     bottom = bottomT;
  160. }
  161.  
  162. inline Rect::Rect( Point topLeft, Point bottomRight )
  163. {
  164.     left = topLeft.x;
  165.     top = topLeft.y;
  166.     right = bottomRight.x;
  167.     bottom = bottomRight.y;
  168. }
  169.  
  170. inline int Rect::width() const
  171. {
  172.     return right - left;
  173. }
  174.  
  175. inline int Rect::height() const
  176. {
  177.     return bottom - top;
  178. }
  179.  
  180.  
  181. inline int Rect::operator==( const Rect& r ) const
  182. {
  183.     return( left == r.left   &&
  184.             top == r.top     &&
  185.             right == r.right &&
  186.             bottom == r.bottom );
  187. }
  188.  
  189. inline int Rect::operator!=( const Rect& r ) const
  190. {
  191.     return( left != r.left   ||
  192.             top != r.top     ||
  193.             right != r.right ||
  194.             bottom != r.bottom);
  195. }
  196.  
  197. inline int Rect::isEmpty() const
  198. {
  199.     return (left >= right || top >= bottom);
  200. }
  201.  
  202. inline int Rect::Contains( Point pt ) const
  203. {
  204.     return( pt.x >= left  &&
  205.             pt.x <= right &&
  206.             pt.y >= top   &&
  207.             pt.y <= bottom);
  208. }
  209.  
  210.  
  211. #endif // _PRIM_H_
  212.