home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / IPOINT.HPP < prev    next >
C/C++ Source or Header  |  1993-10-22  |  20KB  |  368 lines

  1. #ifndef _IPOINT_
  2. #define _IPOINT_
  3. /*******************************************************************************
  4. * FILE NAME: ipoint.hpp                                                        *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the class(es):                                              *
  8. *     IPair  - General-purpose "ordered pair" of (long) integer values         *
  9. *     IPoint - Ordered pair representing a point as x- and y-coordinates in    *
  10. *              two dimensional space                                           *
  11. *     ISize  - Ordered pair representing a two-dimensional size as horizontal  *
  12. *              and vertical dimensions                                         *
  13. *     IRange - Ordered pair representing a range of values as starting and     *
  14. *              ending values                                                   *
  15. *                                                                              *
  16. * COPYRIGHT:                                                                   *
  17. *   Licensed Materials - Property of IBM                                       *
  18. *   (C) Copyright IBM Corporation 1992, 1993                                   *
  19. *   All Rights Reserved                                                        *
  20. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  21. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  22. *                                                                              *
  23. *******************************************************************************/
  24. #ifndef _IBASE_
  25.   #include <ibase.hpp>
  26. #endif
  27.  
  28. /*----------------------------------------------------------------------------*/
  29. /* Align classes on four byte boundary.                                       */
  30. /*----------------------------------------------------------------------------*/
  31. #pragma pack(4)
  32.  
  33. struct _POINTL;
  34. struct _RECTL;
  35. struct _SIZEL;
  36. class  ISize;
  37. class  IWindowHandle;
  38.  
  39. class IPair : public IBase {
  40. typedef IBase
  41.   Inherited;
  42. /*******************************************************************************
  43. * Objects of the IPair class are generic ordered pairs of coordinates.  The    *
  44. * class serves as the base for the specific ordered pair classes IPoint,       *
  45. * ISize, and IRange.                                                           *
  46. *                                                                              *
  47. * This class provides basic utilities to get and set the two coordinate        *
  48. * values.  In addition, it provides a full set of comparison and mathematical  *
  49. * operators to manipulate ordered pairs.                                       *
  50. *******************************************************************************/
  51. public:
  52. /*------------------------------ Related Types ---------------------------------
  53. | The following related type is defined for this class:                        |
  54. |   Coord - Type of the coordinate values (long int).                          |
  55. ------------------------------------------------------------------------------*/
  56. typedef long Coord;
  57.  
  58. /*------------------------------- Constructors ---------------------------------
  59. | You can construct instances of this class in the following ways:             |
  60. |    - From the default constructor.  This constructs an instance of the       |
  61. |      ordered pair (0,0).                                                     |
  62. |    - From a single coordinate.  This constructs an ordered pair with both    |
  63. |      coordinates set to the same argument value.                             |
  64. |    - From two coordinates.  This constructs an ordered pair with each        |
  65. |      coordinate set to a different argument value.                           |
  66. ------------------------------------------------------------------------------*/
  67.   IPair ( );
  68.   IPair ( Coord init );
  69.   IPair ( Coord coord1,
  70.           Coord coord2 );
  71.  
  72. /*-------------------------------- Accessors -----------------------------------
  73. | The following functions provide access to attributes of IPair objects:       |
  74. |   coord1    - Obtains the value of the first coordinate.                     |
  75. |   coord2    - Obtains the value of the second coordinate.                    |
  76. |   setCoord1 - Sets the value of the first coordinate.                        |
  77. |   setCoord2 - Sets the value of the second coordinate.                       |
  78. ------------------------------------------------------------------------------*/
  79. Coord
  80.   coord1 ( )  const,
  81.   coord2 ( )  const;
  82.  
  83. IPair
  84.  &setCoord1 ( Coord coord1 ),
  85.  &setCoord2 ( Coord coord2 );
  86.  
  87. /*----------------------------- Minimum/Maximum --------------------------------
  88. | minimum - Returns an ordered pair whose coordinates are the minimum of the   |
  89. |           corresponding coordinates of the IPair and the argument IPair.     |
  90. | maximum - Returns an ordered pair whose coordinates are the maximum of the   |
  91. |           corresponding coordinates of the IPair and the argument IPair.     |
  92. ------------------------------------------------------------------------------*/
  93. IPair
  94.   minimum ( const IPair &aPair ) const,
  95.   maximum ( const IPair &aPair ) const;
  96.  
  97. /*--------------------------------- Negation -----------------------------------
  98. | operator -  - (unary) Returns an ordered pair with negated coordinates.      |
  99. ------------------------------------------------------------------------------*/
  100. IPair
  101.   operator - ( ) const;
  102.  
  103. /*--------------------------- Comparison Operators -----------------------------
  104. | This class provides a complete set of operators for comparing one ordered    |
  105. | pair to another:                                                             |
  106. |   operator == - True if and only if both coordinates match those of the      |
  107. |                 argument.                                                    |
  108. |   operator != - True if and only if either coordinate differs.               |
  109. |   operator <  - True if and only if both coordinates are less than those of  |
  110. |                 the argument.                                                |
  111. |   operator >  - True if and only if both coordinates are greater than those  |
  112. |                 of the argument.                                             |
  113. |   operator <= - True if and only if both coordinates are less than or equal. |
  114. |   operator >= - True if and only if both coordinates are greater than or     |
  115. |                 equal.                                                       |
  116. ------------------------------------------------------------------------------*/
  117. Boolean
  118.   operator == ( const IPair &aPair ) const,
  119.   operator != ( const IPair &aPair ) const,
  120.   operator <  ( const IPair &aPair ) const,
  121.   operator >  ( const IPair &aPair ) const,
  122.   operator <= ( const IPair &aPair ) const,
  123.   operator >= ( const IPair &aPair ) const;
  124.  
  125.  
  126. /*-------------------------- Manipulation Operators ----------------------------
  127. | This class provides a complete set of arithmetic operators that can be       |
  128. | applied to two ordered pairs; these are friend (non-member) functions.       |
  129. |   operator +  - Returns an ordered pair whose coordinates are the sums of    |
  130. |                 the corresponding coordinates of the IPair and the argument. |
  131. |   operator *  - Returns an ordered pair whose coordinates are the product    |
  132. |                 of the IPair and the argument.                               |
  133. |   operator -  - Returns an ordered pair whose coordinates are the            |
  134. |                 difference between the corresponding coordinates of the      |
  135. |                 IPair and the argument.                                      |
  136. |   operator /  - Returns an ordered pair whose coordinates are the quotient   |
  137. |                 of the corresponding coordinates of the IPair and the        |
  138. |                 argument.                                                    |
  139. |   operator %  - Returns an ordered pair whose coordinates are the remainder  |
  140. |                 of the corresponding coordinates of the IPair and the        |
  141. |                 argument.                                                    |
  142. |   operator += - Adds to the coordinates of an ordered pair those of the      |
  143. |                 argument.                                                    |
  144. |   operator -= - Subtracts from the coordinates those of the argument.        |
  145. |   operator *= - Multiplies the coordinates by those of the argument.         |
  146. |   operator /= - Divides the coordinates by those of the argument.            |
  147. |   operator %= - Replaces the coordinates with the remainder when divided by  |
  148. |                 those of the argument.                                       |
  149. ------------------------------------------------------------------------------*/
  150. friend IPair
  151.   operator +  ( const IPair &pair1,  const IPair &pair2       ),
  152.   operator *  ( const IPair &pair1,  const IPair &pair2       ),
  153.   operator *  ( const IPair &pair1,  double       multiplier  ),
  154.   operator -  ( const IPair &pair1,  const IPair &pair2       ),
  155.   operator /  ( const IPair &pair1,  const IPair &pair2       ),
  156.   operator /  ( const IPair &pair1,  double       divisor     ),
  157.   operator %  ( const IPair &pair1,  const IPair &pair2       ),
  158.   operator %  ( const IPair &aPair1, long         divisor     );
  159.  
  160. IPair
  161.  &operator += ( const IPair &aPair ),
  162.  &operator -= ( const IPair &aPair ),
  163.  &operator *= ( const IPair &aPair ),
  164.  &operator *= ( double multiplier ),
  165.  &operator /= ( const IPair &aPair ),
  166.  &operator /= ( double divisor ),
  167.  &operator %= ( const IPair &aPair ),
  168.  &operator %= ( long  divisor );
  169.  
  170. /*-------------------------- Manipulation Functions ----------------------------
  171. | These functions can be used to scale a pair by different floating point      |
  172. | values in the X- and Y-dimensions:                                           |
  173. |   scaleBy  - Scales the X-coordinate by one amount, the Y-coordinate by      |
  174. |              another.                                                        |
  175. |   scaledBy - Same as scaleBy, but returns a new IPair, leaving the original  |
  176. |              unmodified.                                                     |
  177. ------------------------------------------------------------------------------*/
  178. IPair
  179.  &scaleBy  ( double xFactor, double yFactor ),
  180.   scaledBy ( double xFactor, double yFactor ) const;
  181.  
  182. /*------------------------------ Miscellaneous ---------------------------------
  183. | distanceFrom - Returns the distance from some other ordered pair.            |
  184. | dotProduct   - Returns the dot product with some other ordered pair.         |
  185. | transpose    - Swaps the coordinates of the ordered pair; the friend         |
  186. |                function returns a new pair with transposed coordinates.      |
  187. ------------------------------------------------------------------------------*/
  188. double
  189.   distanceFrom ( const IPair &aPair ) const;
  190.  
  191. long
  192.   dotProduct ( const IPair &aPair ) const;
  193.  
  194. IPair
  195.  &transpose ( );
  196.  
  197. friend IPair
  198.   transpose ( const IPair &aPair );
  199.  
  200. /*-------------------------------- Conversion ----------------------------------
  201. | asString    - Converts the ordered pair (a,b) to an IString( "(a,b)" ).      |
  202. | asDebugInfo - Converts the ordered pair to an IString that contains a        |
  203. |               diagnostic representation of the object.                       |
  204. ------------------------------------------------------------------------------*/
  205. IString
  206.   asString ( ) const,
  207.   asDebugInfo ( ) const;
  208.  
  209. /*-------------------------------- Displaying ----------------------------------
  210. | operator << - This operator is provided to enable ordered pairs to be        |
  211. |               written to ostreams in the conventional manner.                |
  212. ------------------------------------------------------------------------------*/
  213. friend ostream
  214.  &operator << ( ostream          &aStream,
  215.                 const IPair      &aRectangle );
  216.  
  217. private:
  218. /*--------------------------------- PRIVATE ----------------------------------*/
  219. Coord
  220.   coordCl1,
  221.   coordCl2;
  222. }; // class IPair
  223.  
  224. class IPoint : public IPair {
  225. typedef IPair
  226.   Inherited;
  227. /*******************************************************************************
  228. * Objects of the IPoint class represent points in two dimensional space.  In   *
  229. * addition to all the functions inherited from its base class, IPair, the      *
  230. * IPoint class provides a few other functions.                                 *
  231. *******************************************************************************/
  232. public:
  233. /*------------------------------- Constructors ---------------------------------
  234. | You can construct instances of this class in the following ways:             |
  235. |    - From the default constructor.  The resulting point has X- and           |
  236. |      Y-coordinates equal to 0.                                               |
  237. |    - From any other ordered pair.                                            |
  238. |    - From arbitrary X- and Y-coordinates.                                    |
  239. |    - From a Presentation Manager Toolkit-defined POINTL structure.           |
  240. ------------------------------------------------------------------------------*/
  241.   IPoint ( );
  242.   IPoint ( const IPair &pair );
  243.   IPoint ( Coord x, Coord y );
  244.   IPoint ( const struct _POINTL &ptl );
  245.  
  246. /*-------------------------------- Accessors -----------------------------------
  247. | x    - Returns the point's X-coordinate.                                     |
  248. | y    - Returns the point's Y-coordinate.                                     |
  249. | setX - Sets the point's X-coordinate.                                        |
  250. | setY - Sets the point's Y-coordinate.                                        |
  251. ------------------------------------------------------------------------------*/
  252. Coord
  253.   x      ( ) const,
  254.   y      ( ) const;
  255.  
  256. IPoint
  257.  &setX ( Coord X ),
  258.  &setY ( Coord Y );
  259.  
  260. /*-------------------------------- Conversion ----------------------------------
  261. | asPOINTL - Renders the point as a Presentation Manager Toolkit POINTL        |
  262. |            structure.                                                        |
  263. ------------------------------------------------------------------------------*/
  264. struct _POINTL
  265.   asPOINTL ( ) const;
  266. }; // class IPoint
  267.  
  268. class ISize  : public IPair {
  269. typedef IPair
  270.   Inherited;
  271. /*******************************************************************************
  272. * Objects of the ISize class use their coordinates to represent a rectangular  *
  273. * size (in horizontal and vertical dimensions).                                *
  274. *******************************************************************************/
  275. public:
  276. /*------------------------------- Constructors ---------------------------------
  277. | Objects of this class can be constructed from:                               |
  278. |    - The default constructor.  The resulting size has a width and height     |
  279. |      of 0.                                                                   |
  280. |    - Another ordered pair of coordinates.                                    |
  281. |    - A given width and height.                                               |
  282. |    - A Presentation Manager Toolkit SIZEL structure.                         |
  283. |    - A Presentation Manager Toolkit RECTL structure; in this case, the       |
  284. |      resulting ISize object represents the size of the RECTL.                |
  285. ------------------------------------------------------------------------------*/
  286.   ISize ( );
  287.   ISize ( const IPair &pair );
  288.   ISize ( Coord width, Coord height );
  289.   ISize ( const struct _SIZEL &sizl );
  290.   ISize ( const struct _RECTL &rcl );
  291.  
  292. /*-------------------------------- Accessors -----------------------------------
  293. | width     - Returns the width represented by the ISize object.               |
  294. | height    - Returns the height represented by the ISize object.              |
  295. | setWidth  - Sets the size's width.                                           |
  296. | setHeight - Sets the size's height.                                          |
  297. ------------------------------------------------------------------------------*/
  298. Coord
  299.   width  ( ) const,
  300.   height ( ) const;
  301.  
  302. ISize
  303.  &setWidth  ( Coord cx ),
  304.  &setHeight ( Coord cy );
  305.  
  306. /*-------------------------------- Conversion ----------------------------------
  307. | asSIZEL - Returns the ISize as a Presentation Manager Toolkit SIZEL          |
  308. |           structure.                                                         |
  309. ------------------------------------------------------------------------------*/
  310. struct _SIZEL
  311.   asSIZEL ( ) const;
  312. }; // class ISize
  313.  
  314. class IRange : public IPair {
  315. typedef IPair
  316.   Inherited;
  317. /*******************************************************************************
  318.   Objects of the IRange class represent a range of IPair::Coord values
  319.   between a given lower and upper bound (inclusive).
  320. *******************************************************************************/
  321. public:
  322. /*------------------------------- Constructors ---------------------------------
  323. | You can construct instances of this class in the following ways:             |
  324. |    - By using the default constructor.  The resulting range is [0, 0].       |
  325. |    - From any ordered pair (a,b).  The resulting range is [a, b].            |
  326. |    - From any given lower and upper bounds.                                  |
  327. ------------------------------------------------------------------------------*/
  328.   IRange ( );
  329.   IRange ( const IPair &aPair );
  330.   IRange ( Coord lower, Coord upper );
  331.  
  332. /*-------------------------------- Accessors -----------------------------------
  333. | This class provides accessing functions that manage the elements of the      |
  334. | ordered pair as "lower" and "upper" values:                                  |
  335. |   lowerBound    - Returns the lower bound of the range.                      |
  336. |   upperBound    - Returns the upper bound of the range.                      |
  337. |   setLowerBound - Sets the lower bound of the range.                         |
  338. |   setUpperBound - Sets the upper bound of the range.                         |
  339. ------------------------------------------------------------------------------*/
  340. Coord
  341.   lowerBound ( ) const,
  342.   upperBound ( ) const;
  343.  
  344. IRange
  345.  &setUpperBound ( Coord upper ),
  346.  &setLowerBound ( Coord lower );
  347.  
  348. /*--------------------------------- Testing ------------------------------------
  349. | The following function is used to test argument coordinate values:           |
  350. |   includes - Returns true if and only if the argument coordinate value is    |
  351. |              contained in the range.                                         |
  352. ------------------------------------------------------------------------------*/
  353. Boolean
  354.   includes ( Coord aValue ) const;
  355. }; // class IRange
  356.  
  357. /*----------------------------------------------------------------------------*/
  358. /* Resume compiler default packing.                                           */
  359. /*----------------------------------------------------------------------------*/
  360. #pragma pack()
  361.  
  362. /*----------------------------- Inline Functions -----------------------------*/
  363. #ifndef I_NO_INLINES
  364.   #include <ipoint.inl>
  365. #endif
  366.  
  367. #endif /* _IPOINT_ */
  368.