home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / ibmcli / irect.hp_ / IRECT.HPP
Encoding:
C/C++ Source or Header  |  1992-10-26  |  16.0 KB  |  296 lines

  1. #if !defined( _IRectangle_)
  2.   #define _IRectangle_
  3.  
  4. /**************************************************************/
  5. /* CLASS NAME:      IRectangle                                */
  6. /*                                                            */
  7. /* DESCRIPTION  : This file defines a class that represents   */
  8. /*    a rectangular area defined by two points that form      */
  9. /*    opposite corners of the rectangle.  The rectangle is    */
  10. /*    represented via the lower left ('origin') and upper     */
  11. /*    right ('corner') corners.                               */
  12. /*                                                            */
  13. /*    'Mathematically,' a rectangle is considered to include  */
  14. /*    all the points on the lines that form its lower and     */
  15. /*    left edges.  It does *not* include the points that lie  */
  16. /*    on either its upper or right edges.                     */
  17. /*                                                            */
  18. /*    Such Rectangles are used by the various graphics and    */
  19. /*    windowing classes and their member functions.           */
  20. /*                                                            */
  21. /*    Hungarian notation is "rect".                           */
  22. /*                                                            */
  23. /* NOTES:  This class is based closely on the Rectangle       */
  24. /*  class provided by Bill Law in Cary. Thanks Bill.          */
  25. /*                                                            */
  26. /* CHANGE ACTIVITY:                                           */
  27. /*   DATE:     INITIAL:        DESCRIPTION                    */
  28. /*                                                            */
  29. /*   101091    RDL             New Component                  */
  30. /*   012292    RDL             Release 2.0 Changes            */
  31. /*                                                            */
  32. /**************************************************************/
  33. /* Copyright (c) IBM Corporation 1992                         */
  34. /**************************************************************/
  35.  
  36. class IRectangle;
  37.  
  38. #include <ibasetyp.hpp>
  39. #include <ipoint.hpp>
  40.  
  41.  
  42. class IRectangle  /* rect */
  43.    {
  44.    public:
  45.  
  46. /*****************************************************************/
  47. /*                         Instance Creation                     */
  48. /*                                                               */
  49. /*  There are five basic categories of constructors:             */
  50. /*                                                               */
  51. /*  1. Construct a null rectangle (0,0)x(0,0).                   */
  52. /*                                                               */
  53. /*  2. Construct a rectangle with two given points at opposite   */
  54. /*     corners.                                                  */
  55. /*                                                               */
  56. /*  3. Construct a rectangle given 4 (long) integers representing*/
  57. /*     the coordinates for two points at opposite corners of the */
  58. /*     rectangle.                                                */
  59. /*                                                               */
  60. /*  4. Construct a rectangle with opposite corners at (0,0) and  */
  61. /*     (width, height); the width and height and be given either */
  62. /*     as two (long) integers, an ISize,  or an IPoint.          */
  63. /*                                                               */
  64. /*  5. Construct a copy of a given IRectangle.                   */
  65. /*                                                               */
  66. /*  Note: Rectangles are constucted by giving two points that    */
  67. /*        are taken as opposite corners, *not* as origin/corner  */
  68. /*        (or lower-left/upper-right corners); this ensures that */
  69. /*        internally the origin/corner points always are such    */
  70. /*        that origin < corner.                                  */
  71. /*                                                               */
  72. /*****************************************************************/
  73.          IRectangle (                     );
  74.  
  75.          IRectangle ( const IPoint& pt1,
  76.                      const IPoint& pt2 );
  77.  
  78.          IRectangle ( const IPoint& pt,
  79.                      const ISize& siz );
  80.  
  81.          IRectangle ( COORD coordXLeft,
  82.                       COORD coordYBottom,
  83.                       COORD coordXRight,
  84.                       COORD coordYTop     );
  85. #if defined(OS2DEF_INCLUDED)
  86.          IRectangle ( const RECTL& rectl);
  87. #endif
  88.  
  89.          IRectangle ( COORD coordWidth,
  90.                       COORD coordHeight );
  91.          IRectangle ( const IPoint& pt );
  92.          IRectangle ( const ISize&  siz );
  93.  
  94.          IRectangle ( const IRectangle& rect );
  95.  
  96. virtual ~IRectangle()  {}
  97. /*****************************************************************/
  98. /*                         Displaying                            */
  99. /*                                                               */
  100. /* This function can be used to display a Rectangle on an output */
  101. /* stream, using the traditional left-shift operator (<<).  The  */
  102. /* rectangle will be displayed as "Rectangle(l,b,r,t)".          */
  103. /*                                                               */
  104. /*****************************************************************/
  105.         friend class ostream&
  106.              operator<< ( class ostream&   aStream,
  107.                           const IRectangle& rect );
  108.     IString dump() const;
  109.  
  110. /********************************************************************/
  111. /*                               Accessing                          */
  112. /*                                                                  */
  113. /* These functions provide access to various tidbits of information */
  114. /* about the receiver.                                              */
  115. /*                                                                  */
  116. /* One can query any of eight points on the rectangle perimeter or  */
  117. /* its center via functions with names like:                        */
  118. /*                                                                  */
  119. /*    < top | bottom >< Left | Center | Right >                     */
  120. /*                    or                                            */
  121. /*    < left | right >< Center >                                    */
  122. /*                    or                                            */
  123. /*    center                                                        */
  124. /*                                                                  */
  125. /* Other characteristics of the rectangle can be queried via:       */
  126. /*                                                                  */
  127. /*    area     -  returns the area of the receiver                  */
  128. /*    bottom   -  returns the y-coordinate of the horizontal line   */
  129. /*                forming the bottom of the receiver rectangle      */
  130. /*    extent   -  returns the ISize( width, height )                */
  131. /*    height   -  returns the height of the receiver                */
  132. /*    left     -  returns the x-coordinate of the vertical line     */
  133. /*                forming the left side of the receiver rectangle   */
  134. /*    right    -  returns the x-coordinate of the vertical line     */
  135. /*                forming the right side of the receiver rectangle  */
  136. /*    top      -  returns the y-coordinate of the horizontal line   */
  137. /*                forming the top of the receiver rectangle         */
  138. /*    width    -  returns the width of the receiver                 */
  139. /********************************************************************/
  140.  
  141.    IPoint         bottomLeft   ( ) const;
  142.    IPoint         bottomCenter ( ) const;
  143.    IPoint         bottomRight  ( ) const;
  144.    IPoint         center       ( ) const;
  145.    IPoint         leftCenter   ( ) const;
  146.    IPoint         rightCenter  ( ) const;
  147.    IPoint         topLeft      ( ) const;
  148.    IPoint         topCenter    ( ) const;
  149.    IPoint         topRight     ( ) const;
  150.  
  151.    ISize          extent       ( ) const;
  152.  
  153.    COORD          area   ( ) const;
  154.    COORD          bottom ( ) const;
  155.    COORD          height ( ) const;
  156.    COORD          left   ( ) const;
  157.    COORD          right  ( ) const;
  158.    COORD          top    ( ) const;
  159.    COORD          width  ( ) const;
  160. #if defined(OS2DEF_INCLUDED)
  161.    RECTL          rectl  ( ) const;
  162.    POINTL         pointl ( ) const;
  163. #endif
  164. #if defined(GPI_INCLUDED)
  165.    SIZEL          sizel  ( ) const;
  166. #endif
  167.  
  168. /**********************************************************************/
  169. /*                               Comparison                           */
  170. /*                                                                    */
  171. /*    Two rectangles can be compared for equality or inequality.  The */
  172. /*    other comparison operators don't have reasonable interpretations*/
  173. /*    so are not defined.                                             */
  174. /**********************************************************************/
  175.  
  176. Boolean           operator == ( const IRectangle& rect ) const;
  177. Boolean           operator != ( const IRectangle& rect ) const;
  178.  
  179. /**********************************************************************/
  180. /*                           Manipulation                             */
  181. /*                                                                    */
  182. /* These functions modify the receiving rectangle, changing its       */
  183. /* size, proportions, and/or location.                                */
  184. /*                                                                    */
  185. /* centerOn    -  move the receiver so that its center is at          */
  186. /*                the argument point                                  */
  187. /* expandBy    -  move the corners of the receiver outward from       */
  188. /*                the center by the given amount; the argument can    */
  189. /*                be either a scalar (long int) or a point            */
  190. /* moveBy      -  move the receiver by the amount specified by the    */
  191. /*                argument point                                      */
  192. /* moveTo      -  move the receiver so that its bottom left corner    */
  193. /*                is at the argument point                            */
  194. /* operator =  -  assign the argument rectangle to the receiver;      */
  195. /*                this function is implemented as the default         */
  196. /*                assignment operator *should* be, but there          */
  197. /*                seems to be a Zortech bug so we must do it          */
  198. /*                ourselves                                           */
  199. /* operator &= -  reset the receiver to its intersection with the     */
  200. /*                argument rectangle                                  */
  201. /* operator |= -  reset the receiver to the smallest rectangle that   */
  202. /*                encompasses both it and the argument rectangle      */
  203. /* scaleBy     -  scale the rectangle by the argument amount; it can  */
  204. /*                be either a scalar (long int or double) or a point  */
  205. /*                (which scales by different amounts in the x- and    */
  206. /*                y- directions)                                      */
  207. /* shrinkBy    -  move the corners of the receiver inward toward      */
  208. /*                the center by the given amount (again, either a     */
  209. /*                scalar or point); shrinkBy( lAmount ) is always     */
  210. /*                equivalent to expandBy( - lAmount ), and vice       */
  211. /*                versa                                               */
  212. /*                                                                    */
  213. /* There are corresponding versions of each of these that return the  */
  214. /* same result rectangle, but leave the receiver unmodified:          */
  215. /*                                                                    */
  216. /* centeredOn                                                         */
  217. /* expandedBy                                                         */
  218. /* movedBy                                                            */
  219. /* movedTo                                                            */
  220. /* operator &                                                         */
  221. /* operator |                                                         */
  222. /* scaledBy                                                           */
  223. /* shrunkBy                                                           */
  224. /*                                                                    */
  225. /**********************************************************************/
  226.  
  227.    IRectangle&     centerOn    ( const IPoint&     pt      );
  228.    IRectangle&     expandBy    ( const ISize&      siz     );
  229.    IRectangle&     expandBy    ( COORD             coord   );
  230.    IRectangle&     moveBy      ( const ISize&      siz     );
  231.    IRectangle&     moveTo      ( const IPoint&     pt      );
  232.    IRectangle&     sizeTo      ( const ISize&      siz     );
  233.  
  234.    IRectangle&     operator =  ( const IRectangle& rect    );
  235.    IRectangle&     operator &= ( const IRectangle& rect    );
  236.    IRectangle&     operator |= ( const IRectangle& rect    );
  237.  
  238.    IRectangle&     scaleBy     ( const ISize&      siz     );
  239.    IRectangle&     scaleBy     ( COORD             coord   );
  240.    IRectangle&     scaleBy     ( double            dAmount );
  241.  
  242.    IRectangle&     shrinkBy    ( const ISize&      siz     );
  243.    IRectangle&     shrinkBy    ( COORD             coord   );
  244.    IRectangle      centeredOn  ( const IPoint&     pt      ) const;
  245.    IRectangle      expandedBy  ( const ISize&      pt      ) const;
  246.    IRectangle      expandedBy  ( COORD             coord   ) const;
  247.  
  248.    IRectangle      movedBy     ( const ISize&      siz  ) const;
  249.    IRectangle      movedTo     ( const IPoint&     pt   ) const;
  250.    IRectangle      sizedTo     ( const ISize&      siz  ) const;
  251.  
  252.    IRectangle      operator &  ( const IRectangle& rect    ) const;
  253.    IRectangle      operator |  ( const IRectangle& rect    ) const;
  254.  
  255.    IRectangle      scaledBy    ( const ISize&      siz     ) const;
  256.    IRectangle      scaledBy    ( COORD             coord   ) const;
  257.    IRectangle      scaledBy    ( double            dAmount ) const;
  258.    IRectangle      shrunkBy    ( const ISize&      siz     ) const;
  259.    IRectangle      shrunkBy    ( COORD             coord   ) const;
  260.  
  261. /************************************************************************/
  262. /*                                Testing                               */
  263. /*                                                                      */
  264. /*    These functions test various attributes of a rectangle:           */
  265. /*                                                                      */
  266. /*    contains    -  returns true iff the receiver contains the argument*/
  267. /*                   point or rectangle                                 */
  268. /*    intersects  -  returns true iff the receiver and the argument     */
  269. /*                   rectangle overlap                                  */
  270. /*                                                                      */
  271. /************************************************************************/
  272. Boolean              contains   ( const IPoint&     pt    ) const;
  273. Boolean              contains   ( const IRectangle& rect  ) const;
  274.  
  275. Boolean              intersects ( const IRectangle& rect  ) const;
  276.  
  277.    protected://------------------Protected--------------------------------------
  278.  
  279. /************************************************************************/
  280. /*                              Data Members                            */
  281. /*                                                                      */
  282. /*    The rectangle object is maintained internally by its bottomLeft   */
  283. /*    and topRight corners, named 'origin' and 'corner', respectively.  */
  284. /*                                                                      */
  285. /************************************************************************/
  286.  
  287.       IPoint    origin;
  288.       IPoint    corner;
  289.  
  290. /*    Function to 'correct' invalid rectangle after expansion or
  291.       intersection.
  292. */
  293.       IRectangle&     validate ( );
  294.    };
  295. #endif
  296.