home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / include / irect.inl < prev    next >
Encoding:
Text File  |  1996-02-22  |  12.8 KB  |  445 lines

  1. #ifndef _IRECT_INL_
  2. #define _IRECT_INL_ 0
  3. /*******************************************************************************
  4. * FILE NAME: irect.inl                                                         *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   This file contains the definition of the inline functions for the          *
  8. *   classes declared in irect.hpp.                                             *
  9. *                                                                              *
  10. * COPYRIGHT:                                                                   *
  11. *   IBM Open Class Library                                                     *
  12. *   (C) Copyright International Business Machines Corporation 1992, 1996       *
  13. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  14. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  15. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  16. *                                                                              *
  17. *******************************************************************************/
  18. #ifndef _IRECT_
  19.   #undef  _IRECT_INL_
  20.   #define _IRECT_INL_ 1
  21.   #include <irect.hpp>
  22. #endif
  23.  
  24. #if _IRECT_INL_
  25.   #define inline
  26. #endif
  27.  
  28. /*------------------------------- Constructors -------------------------------*/
  29. inline IRectangle :: IRectangle ( )
  30.   : origin( 0, 0 ), corner( 0, 0 )
  31.   {
  32.   }
  33. inline IRectangle :: IRectangle ( const IPoint &point1,
  34.                                   const IPoint &point2 )
  35.   : origin( point1.minimum( point2 ) ), corner( point1.maximum( point2 ) )
  36.   {
  37.   }
  38. inline IRectangle :: IRectangle ( const IPoint &aPoint,
  39.                                   const ISize  &aSize )
  40.   : origin( aPoint.minimum( aPoint+aSize ) ),
  41.     corner( aPoint.maximum( aPoint+aSize ) )
  42.   {
  43.   }
  44. inline IRectangle :: IRectangle ( Coord x1,
  45.                                   Coord y1,
  46.                                   Coord x2,
  47.                                   Coord y2 )
  48.    : origin( IPair( x1, y1 ).minimum( IPair( x2, y2 ) ) ),
  49.      corner( IPair( x1, y1 ).maximum( IPair( x2, y2 ) ) )
  50.   {
  51.   }
  52. #ifdef _WINDEF_
  53. inline IRectangle :: IRectangle ( const RECT &aRECTL)
  54.   : origin( IPair( aRECTL.left, aRECTL.bottom ).
  55.       minimum( IPair( aRECTL.right, aRECTL.top ) ) ),
  56.     corner( IPair( aRECTL.left, aRECTL.bottom ).
  57.       maximum( IPair( aRECTL.right, aRECTL.top  ) ) )
  58.   {
  59.   }
  60. inline IRectangle :: IRectangle ( const RECTL &aRECTL)
  61.   : origin( IPair( aRECTL.left, aRECTL.bottom ).
  62.       minimum( IPair( aRECTL.right, aRECTL.top ) ) ),
  63.     corner( IPair( aRECTL.left, aRECTL.bottom ).
  64.       maximum( IPair( aRECTL.right, aRECTL.top  ) ) )
  65.   {
  66.   }
  67. #endif
  68. #if defined(OS2DEF_INCLUDED)
  69. inline IRectangle :: IRectangle ( const RECTL &aRECTL)
  70.   : origin( IPair( aRECTL.xLeft, aRECTL.yBottom ).
  71.       minimum( IPair( aRECTL.xRight, aRECTL.yTop ) ) ),
  72.     corner( IPair( aRECTL.xLeft, aRECTL.yBottom ).
  73.       maximum( IPair( aRECTL.xRight, aRECTL.yTop  ) ) )
  74.   {
  75.   }
  76. #endif
  77. inline IRectangle :: IRectangle ( Coord width,
  78.                                   Coord height )
  79.   : origin( IPair( width, height ).minimum( IPair() ) ),
  80.     corner( IPair( width, height ).maximum( IPair() ) )
  81.   {
  82.   }
  83. inline IRectangle :: IRectangle ( const IPair &aPair )
  84.   : origin( aPair.minimum( IPair() ) ),
  85.     corner( aPair.maximum( IPair() ) )
  86.   {
  87.   }
  88. /*-------------------------------- Accessors ---------------------------------*/
  89. inline IRectangle::Coord IRectangle :: minY ( ) const
  90.   {
  91.   return origin.y();
  92.   }
  93. inline IRectangle::Coord IRectangle :: bottom ( ) const
  94.   {
  95.   return minY();
  96.   }
  97. inline IRectangle::Coord IRectangle :: height ( ) const
  98.   {
  99.   return corner.y() - origin.y();
  100.   }
  101. inline IRectangle::Coord IRectangle :: minX ( ) const
  102.   {
  103.   return origin.x();
  104.   }
  105. inline IRectangle::Coord IRectangle :: left ( ) const
  106.   {
  107.   return minX();
  108.   }
  109. inline IRectangle::Coord IRectangle :: maxX ( ) const
  110.   {
  111.   return corner.x();
  112.   }
  113. inline IRectangle::Coord IRectangle :: right ( ) const
  114.   {
  115.   return maxX();
  116.   }
  117. inline IRectangle::Coord IRectangle :: maxY ( ) const
  118.   {
  119.   return corner.y();
  120.   }
  121. inline IRectangle::Coord IRectangle :: top ( ) const
  122.   {
  123.   return maxY();
  124.   }
  125. inline IRectangle::Coord IRectangle :: width ( ) const
  126.   {
  127.   return corner.x() - origin.x();
  128.   }
  129. inline IPoint IRectangle :: centerXMinY ( ) const
  130.   {
  131.   return IPoint( ( maxX() + minX() ) / 2,
  132.                    minY() );
  133.   }
  134. inline IPoint IRectangle :: bottomCenter ( ) const
  135.   {
  136.   return centerXMinY( );
  137.   }
  138. inline IPoint IRectangle :: minXMinY ( ) const
  139.   {
  140.   return origin;
  141.   }
  142. inline IPoint IRectangle :: bottomLeft ( ) const
  143.   {
  144.   return minXMinY();
  145.   }
  146. inline IPoint IRectangle :: maxXMinY ( ) const
  147.   {
  148.   return IPoint( maxX(),
  149.                  minY() );
  150.   }
  151. inline IPoint IRectangle :: bottomRight ( ) const
  152.   {
  153.   return maxXMinY( );
  154.   }
  155. inline IPoint IRectangle :: centerXCenterY ( ) const
  156.   {
  157.   return ( origin + corner ) / 2;
  158.   }
  159. inline IPoint IRectangle :: center ( ) const
  160.   {
  161.   return centerXCenterY( );
  162.   }
  163. inline IPoint IRectangle :: minXCenterY ( ) const
  164.   {
  165.   return IPoint( minX(),
  166.                  ( minY() + maxY() ) / 2 );
  167.   }
  168. inline IPoint IRectangle :: leftCenter ( ) const
  169.   {
  170.   return minXCenterY( );
  171.   }
  172. inline IPoint IRectangle :: maxXCenterY ( ) const
  173.   {
  174.   return IPoint( maxX(),
  175.                  ( minY() + maxY() ) / 2 );
  176.   }
  177. inline IPoint IRectangle :: rightCenter ( ) const
  178.   {
  179.   return maxXCenterY();
  180.   }
  181. inline IPoint IRectangle :: centerXMaxY ( ) const
  182.   {
  183.   return IPoint( ( minX() + maxX() ) / 2,
  184.                  maxY() );
  185.   }
  186. inline IPoint IRectangle :: topCenter ( ) const
  187.   {
  188.   return centerXMaxY();
  189.   }
  190. inline IPoint IRectangle :: minXMaxY ( ) const
  191.   {
  192.   return IPoint( minX(),
  193.                  maxY() );
  194.   }
  195. inline IPoint IRectangle :: topLeft ( ) const
  196.   {
  197.   return minXMaxY( );
  198.   }
  199. inline IPoint IRectangle :: maxXMaxY ( ) const
  200.   {
  201.   return corner;
  202.   }
  203. inline IPoint IRectangle :: topRight ( ) const
  204.   {
  205.   return maxXMaxY( );
  206.   }
  207. inline ISize IRectangle :: size ( ) const
  208.   {
  209.   return corner - origin;
  210.   }
  211. inline IRectangle::Coord IRectangle :: area ( ) const
  212.   {
  213.   return height() * width();
  214.   }
  215. /*--------------------------- Comparison Operators ---------------------------*/
  216. inline IBase::Boolean IRectangle :: operator == ( const IRectangle &aRect ) const
  217.   {
  218.   return ( origin == aRect.origin
  219.            &&
  220.            corner == aRect.corner );
  221.   }
  222. inline IBase::Boolean IRectangle :: operator != ( const IRectangle& aRect ) const
  223.   {
  224.   return !( *this == aRect );
  225.   }
  226. /*------------------------------ Implementation ------------------------------*/
  227. inline IRectangle &IRectangle :: validate ( )
  228.   {
  229.   if ( (corner.coord1() < origin.coord1()) ||
  230.        (corner.coord2() < origin.coord2()) )
  231.   {
  232.     origin = corner = IPoint( 0, 0 );
  233.   }
  234.   return *this;
  235.   }
  236. /*-------------------------- Manipulation Operators --------------------------*/
  237. inline IRectangle &IRectangle :: operator &= ( const IRectangle &aRect )
  238.   {
  239.   origin = origin.maximum( aRect.origin );
  240.   corner = corner.minimum( aRect.corner );
  241.   return validate();
  242.   }
  243. inline IRectangle IRectangle :: operator &  ( const IRectangle &aRect ) const
  244.   {
  245.   IRectangle result( *this );
  246.   return ( result &= aRect );
  247.   }
  248. inline IRectangle &IRectangle :: operator |= ( const IRectangle &aRect )
  249.   {
  250.   origin = origin.minimum( aRect.origin ),
  251.   corner = corner.maximum( aRect.corner );
  252.   return *this;
  253.   }
  254. inline IRectangle IRectangle :: operator |  ( const IRectangle &aRect ) const
  255.   {
  256.   IRectangle result( *this );
  257.   return result |= aRect;
  258.   }
  259. /*------------------------------- Manipulation -------------------------------*/
  260. inline IRectangle &IRectangle :: moveBy ( const IPair &aPair )
  261.   {
  262.   origin += aPair;
  263.   corner += aPair;
  264.   return *this;
  265.   }
  266. inline IRectangle &IRectangle :: centerAt ( const IPoint &aPoint )
  267.   {
  268.   return moveBy( aPoint - centerXCenterY() );
  269.   }
  270. inline IRectangle IRectangle :: centeredAt ( const IPoint &aPoint ) const
  271.   {
  272.   IRectangle result( *this );
  273.   return result.centerAt( aPoint );
  274.   }
  275. inline IRectangle &IRectangle :: expandBy ( const IPair &aPair )
  276.   {
  277.   origin -= aPair;
  278.   corner += aPair;
  279.   return validate();
  280.   }
  281. inline IRectangle &IRectangle :: expandBy ( Coord coord )
  282.   {
  283.   origin -= coord,
  284.   corner += coord;
  285.   return validate();
  286.   }
  287. inline IRectangle IRectangle :: expandedBy ( const IPair &aPair ) const
  288.   {
  289.   IRectangle result( *this );
  290.   return result.expandBy( aPair );
  291.   }
  292. inline IRectangle IRectangle :: expandedBy ( Coord coord ) const
  293.   {
  294.   IRectangle result( *this );
  295.   return result.expandBy( coord );
  296.   }
  297. inline IRectangle IRectangle :: movedBy ( const IPair &aPair ) const
  298.   {
  299.   IRectangle result( *this );
  300.   return result.moveBy( aPair );
  301.   }
  302. inline IRectangle &IRectangle :: moveTo ( const IPoint &aPoint )
  303.   {
  304.   return moveBy( aPoint - origin );
  305.   }
  306. inline IRectangle IRectangle :: movedTo ( const IPoint &aPoint ) const
  307.   {
  308.   IRectangle result( *this );
  309.   return result.moveTo( aPoint );
  310.   }
  311. inline IRectangle &IRectangle :: scaleBy ( const IPair &aPair )
  312.   {
  313.   origin *= aPair;
  314.   corner *= aPair;
  315.   return *this;
  316.   }
  317. inline IRectangle &IRectangle :: scaleBy ( Coord coord )
  318.   {
  319.   origin *= coord,
  320.   corner *= coord;
  321.   return *this;
  322.   }
  323. inline IRectangle &IRectangle :: scaleBy ( double amt )
  324.   {
  325.   origin *= amt;
  326.   corner *= amt;
  327.   return *this;
  328.   }
  329. inline IRectangle &IRectangle :: scaleBy ( double xamt, double yamt )
  330.   {
  331.   origin.scaleBy( xamt, yamt );
  332.   corner.scaleBy( xamt, yamt );
  333.   return *this;
  334.   }
  335. inline IRectangle IRectangle :: scaledBy ( const IPair &aPair ) const
  336.   {
  337.   IRectangle result( *this );
  338.   return result.scaleBy( aPair );
  339.   }
  340. inline IRectangle IRectangle :: scaledBy ( Coord coord ) const
  341.   {
  342.   IRectangle result( *this );
  343.   return result.scaleBy( coord );
  344.   }
  345. inline IRectangle IRectangle :: scaledBy ( double amt ) const
  346.   {
  347.   IRectangle result( *this );
  348.   return result.scaleBy( amt );
  349.   }
  350. inline IRectangle IRectangle :: scaledBy ( double xamt, double yamt ) const
  351.   {
  352.   IRectangle result( *this );
  353.   return result.scaleBy( xamt, yamt );
  354.   }
  355. inline IRectangle &IRectangle :: shrinkBy ( const IPair &aPair )
  356.   {
  357.   return expandBy( -aPair );
  358.   }
  359. inline IRectangle &IRectangle :: shrinkBy ( Coord coord )
  360.   {
  361.   return expandBy( -coord );
  362.   }
  363. inline IRectangle IRectangle :: shrunkBy ( const IPair &aPair ) const
  364.   {
  365.   return expandedBy( -aPair );
  366.   }
  367. inline IRectangle IRectangle :: shrunkBy ( Coord coord ) const
  368.   {
  369.   return expandedBy( -coord );
  370.   }
  371. inline IRectangle &IRectangle :: sizeTo ( const IPair &aPair )
  372.   {
  373.   corner = origin + aPair;
  374.   return *this;
  375.   }
  376. inline IRectangle IRectangle :: sizedTo ( const IPair &aPair ) const
  377.   {
  378.   IRectangle result( *this );
  379.   return result.sizeTo( aPair );
  380.   }
  381. inline IRectangle &IRectangle :: sizeBy ( Coord factor )
  382.   {
  383.   IPoint save( minXMinY() );
  384.   moveTo( IPoint(0,0) ).scaleBy( factor ).moveTo( save );
  385.   return *this;
  386.   }
  387. inline IRectangle &IRectangle :: sizeBy ( const IPair &aPair )
  388.   {
  389.   IPoint save( minXMinY() );
  390.   moveTo( IPoint(0,0) ).scaleBy( aPair ).moveTo( save );
  391.   return *this;
  392.   }
  393. inline IRectangle &IRectangle :: sizeBy ( double factor )
  394.   {
  395.   IPoint save( minXMinY() );
  396.   moveTo( IPoint(0,0) ).scaleBy( factor ).moveTo( save );
  397.   return *this;
  398.   }
  399. inline IRectangle &IRectangle :: sizeBy ( double xfact, double yfact )
  400.   {
  401.   IPoint save( minXMinY() );
  402.   moveTo( IPoint(0,0) ).scaleBy( xfact, yfact ).moveTo( save );
  403.   return *this;
  404.   }
  405. inline IRectangle IRectangle :: sizedBy ( Coord factor ) const
  406.   {
  407.   IRectangle result( *this );
  408.   return result.sizeBy( factor );
  409.   }
  410. inline IRectangle IRectangle :: sizedBy ( const IPair &aPair ) const
  411.   {
  412.   IRectangle result( *this );
  413.   return result.sizeBy( aPair );
  414.   }
  415. inline IRectangle IRectangle :: sizedBy ( double factor ) const
  416.   {
  417.   IRectangle result( *this );
  418.   return result.sizeBy( factor );
  419.   }
  420. inline IRectangle IRectangle :: sizedBy ( double xfact, double yfact ) const
  421.   {
  422.   IRectangle result( *this );
  423.   return result.sizeBy( xfact, yfact );
  424.   }
  425. /*--------------------------------- Testing ----------------------------------*/
  426. inline IBase::Boolean IRectangle :: contains ( const IRectangle &aRect ) const
  427.   {
  428.   return ( origin <= aRect.origin
  429.            &&
  430.            aRect.corner <= corner );
  431.   }
  432. inline IBase::Boolean IRectangle :: contains ( const IPoint &aPoint ) const
  433.   {
  434.   return ( aPoint >= origin
  435.            &&
  436.            aPoint < corner );
  437.   }
  438. inline IBase::Boolean IRectangle :: intersects ( const IRectangle &aRect ) const
  439.   {
  440.   return ( origin < aRect.corner
  441.            &&
  442.            aRect.origin < corner );
  443.   }
  444. #endif /* _IRECT_INL_ */
  445.