home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / Libraries / Geometry.cp < prev    next >
Encoding:
Text File  |  1991-05-01  |  11.0 KB  |  442 lines  |  [TEXT/MPS ]

  1. // Geometry.cp 
  2. // Copyright © 1985-1991 by Apple Computer, Inc.  All rights reserved.
  3.  
  4.  
  5. #ifndef __TYPES__
  6. #include <Types.h>
  7. #endif
  8.  
  9. #ifndef __STDIO__
  10. #include <StdIo.h>
  11. #endif
  12.  
  13. #ifndef __GEOMETRY__
  14. #include <Geometry.h>
  15. #endif
  16.  
  17. #pragma segment Main
  18.  
  19. //--------------------------------------------------------------------------------
  20. // This constructor for Point is being outlined by CFront. It claims there is an
  21. // expression to complex to be inline. I tried taking the space out between the
  22. // two curly braces…
  23. //--------------------------------------------------------------------------------
  24.  
  25. Point::Point() { }
  26.  
  27.  
  28. //--------------------------------------------------------------------------------
  29. // Conversion operator, for converting Point to a textual representation of a
  30. // Point. "Point(v,h)". Formats Point in a static character string and returns a
  31. // pointer to that static character string.
  32. //--------------------------------------------------------------------------------
  33.  
  34. Point::operator char*() const
  35. {
  36.     static char textPoint[40];
  37.     
  38.     sprintf (textPoint, "Point(%d, %d)", v, h);
  39.     
  40.     return textPoint;
  41. }
  42.  
  43.  
  44. //--------------------------------------------------------------------------------
  45. // Selector operators for Point. Selects one of the two coordinates depending
  46. // on the value of the sel parameter.
  47. //--------------------------------------------------------------------------------
  48.  
  49. short& Point::operator[](VHSelect sel)                // For non-const Point
  50. {
  51.     if (sel == vSel)
  52.         return v;
  53.     else
  54.         return h;
  55. }
  56.  
  57. const short& Point::operator[](VHSelect sel) const    // For const Point
  58. {
  59.     if (sel == vSel)
  60.         return v;
  61.     else
  62.         return h;
  63. }
  64.  
  65.  
  66. //--------------------------------------------------------------------------------
  67. // Arithmatic operators for Point. Addition and subtraction are all that make
  68. // any sense. Both the Add and AddTo forms are defined.
  69. //--------------------------------------------------------------------------------
  70.  
  71. Point Point::operator+(const Point& pt) const
  72. {
  73.     Point returnPt;
  74.     
  75.     returnPt.v = v + pt.v;
  76.     returnPt.h = h + pt.h;
  77.     return returnPt;
  78. }
  79.  
  80. Point Point::operator-(const Point& pt) const
  81. {
  82.     Point returnPt;
  83.     
  84.     returnPt.v = v - pt.v;
  85.     returnPt.h = h - pt.h;
  86.     return returnPt;
  87. }
  88.  
  89. Point& Point::operator+=(const Point& pt)
  90. {
  91.     v += pt.v;
  92.     h += pt.h;
  93.     return *this;
  94. }
  95.  
  96. Point& Point::operator-=(const Point& pt)
  97. {
  98.     v -= pt.v;
  99.     h -= pt.h;
  100.     return *this;
  101. }
  102.  
  103.  
  104. //--------------------------------------------------------------------------------
  105. // Relational operators for Point. These are defined by applying the operator in
  106. // question to both coordinates. The condition must hold for both to hold for the
  107. // Points the corresponding Points.
  108. //--------------------------------------------------------------------------------
  109.  
  110. Boolean Point::operator!=(const Point& pt) const
  111. {
  112.     return v != pt.v || h != pt.h;
  113. }
  114.  
  115. Boolean Point::operator==(const Point& pt) const
  116. {
  117.     return v == pt.v && h == pt.h;
  118. }
  119.  
  120. Boolean Point::operator>(const Point& pt) const
  121. {
  122.     return v > pt.v && h > pt.h;
  123. }
  124.  
  125. Boolean Point::operator<(const Point& pt) const
  126. {
  127.     return v < pt.v && h < pt.h;
  128. }
  129.  
  130. Boolean Point::operator>=(const Point& pt) const
  131. {
  132.     return v >= pt.v && h >= pt.h;
  133. }
  134.  
  135. Boolean Point::operator<=(const Point& pt) const
  136. {
  137.     return v <= pt.v && h <= pt.h;
  138. }
  139.  
  140.  
  141. //--------------------------------------------------------------------------------
  142. // Conversion operator, for converting Rect to a textual representation of a
  143. // Rect. "Rect(top,left,bottom,right)". Formats Rect in a static character string
  144. // and returns a pointer to that static character string.
  145. //--------------------------------------------------------------------------------
  146.  
  147. Rect::operator char*() const
  148. {
  149.     static char textRect[40];
  150.     
  151.     sprintf (textRect, "Rect(%d, %d, %d, %d)", top, left, bottom, right);
  152.     
  153.     return textRect;
  154. }
  155.  
  156.  
  157. //--------------------------------------------------------------------------------
  158. // Selector operators for Point. Selects one of the two coordinates depending
  159. // on the value of the sel parameter.
  160. //--------------------------------------------------------------------------------
  161.  
  162. Point& Rect::operator[](PointSelector sel)
  163. {
  164.     if (sel == topLeft)
  165.         return *((Point *) &top);
  166.     else
  167.         return *((Point *) &bottom);
  168. }
  169.  
  170. const Point& Rect::operator[](PointSelector sel) const
  171. {
  172.     if (sel == topLeft)
  173.         return *((Point *) &top);
  174.     else
  175.         return *((Point *) &bottom);
  176. }
  177.  
  178.  
  179. //--------------------------------------------------------------------------------
  180. // Operators for adding and subtracting one Rect from to/from another. Both the
  181. // Add and AddTo form of operators are defined.
  182. //--------------------------------------------------------------------------------
  183.  
  184. Rect Rect::operator+(const Rect& rt) const
  185. {
  186.     Rect returnRect;
  187.     
  188.     returnRect.top = top + rt.top;
  189.     returnRect.left = left + rt.left;
  190.     returnRect.bottom = bottom + rt.bottom;
  191.     returnRect.right = right + rt.right;
  192.     
  193.     return returnRect;
  194. }
  195.  
  196. Rect Rect::operator-(const Rect& rt) const
  197. {
  198.     Rect returnRect;
  199.     
  200.     returnRect.top = top - rt.top;
  201.     returnRect.left = left - rt.left;
  202.     returnRect.bottom = bottom - rt.bottom;
  203.     returnRect.right = right - rt.right;
  204.     
  205.     return returnRect;
  206. }
  207.  
  208. Rect& Rect::operator+=(const Rect& rt)
  209. {
  210.     top += rt.top;
  211.     left+= rt.left;
  212.     bottom += rt.bottom;
  213.     right += rt.right;
  214.     
  215.     return *this;
  216. }
  217.  
  218.  
  219. Rect& Rect::operator-=(const Rect& rt)
  220. {
  221.     top -= rt.top;
  222.     left-= rt.left;
  223.     bottom-= rt.bottom;
  224.     right-= rt.right;
  225.     
  226.     return *this;
  227. }
  228.  
  229.  
  230. //--------------------------------------------------------------------------------
  231. // Operators for adding and subtracting a Point to/from a Rect. A Point is added
  232. // to a Rect by adding the Point to both the top-left and bottom-right Points that
  233. // define the Rect. Both the Add and AddTo operators are defined. Very convenient
  234. // for translating Rects. These take a point and since Point has a constructor
  235. // that takes two shorts the Rect windowRect can be translated 100 pixels in the
  236. // positive y direction by the statement:
  237. //
  238. //    windowRect = windowRect + Point (0, 100);    or
  239. //     windowRect += Point (0, 100);
  240. //--------------------------------------------------------------------------------
  241.  
  242. Rect Rect::operator+(const Point& pt) const
  243. {
  244.     Rect returnRect;
  245.     
  246.     returnRect.top = top + pt.v;
  247.     returnRect.left = left + pt.h;
  248.     returnRect.bottom = bottom + pt.v;
  249.     returnRect.right = right + pt.h;
  250.     
  251.     return returnRect;
  252. }
  253.  
  254. Rect Rect::operator-(const Point& pt) const
  255. {
  256.     Rect returnRect;
  257.     
  258.     returnRect.top = top - pt.v;
  259.     returnRect.left = left - pt.h;
  260.     returnRect.bottom = bottom - pt.v;
  261.     returnRect.right = right - pt.h;
  262.     
  263.     return returnRect;
  264. }
  265.  
  266. Rect& Rect::operator+=(const Point& pt)
  267. {
  268.     top += pt.v;
  269.     left+= pt.h;
  270.     bottom+= pt.v;
  271.     right+= pt.h;
  272.     
  273.     return *this;
  274. }
  275.  
  276. Rect& Rect::operator-=(const Point& pt)
  277. {
  278.     top -= pt.v;
  279.     left -= pt.h;
  280.     bottom -= pt.v;
  281.     right -= pt.h;
  282.     
  283.     return *this;
  284. }
  285.  
  286.  
  287. //--------------------------------------------------------------------------------
  288. // Inset a Rect using the coordinates in Point for the inset delta
  289. //--------------------------------------------------------------------------------
  290.  
  291. Rect& Rect::Inset(const Point& delta)
  292. {
  293.     top += delta.v;
  294.     left += delta.h;
  295.     bottom -= delta.v;
  296.     right -= delta.h;
  297.     
  298.     return *this;
  299. }
  300.  
  301.  
  302. //--------------------------------------------------------------------------------
  303. // Equality operators, other relational operator could be defined such as <. But
  304. // their meaning is ambiguous and probably better implemented as methods. For
  305. // example, aRect < bRect could return true if aRect was inside of bRect, or could
  306. // return true if the area of aRect was less than the area of bRect.
  307. //--------------------------------------------------------------------------------
  308.  
  309. Boolean Rect::operator==(const Rect& rt) const
  310. {
  311.     return
  312.         top == rt.top && left == rt.left &&
  313.         bottom == rt.bottom && right == rt.right;
  314. }
  315.  
  316. Boolean Rect::operator!=(const Rect& rt) const
  317. {
  318.     return
  319.         top != rt.top || left != rt.left ||
  320.         bottom != rt.bottom || right != rt.right;
  321. }
  322.  
  323.  
  324. //--------------------------------------------------------------------------------
  325. // Two simple area operators, the intersection && (logical and in C++) and the
  326. // union || (logical or in C++). The definition of union here is to return a Rect
  327. // that exactly encloses its operands.
  328. //--------------------------------------------------------------------------------
  329.  
  330. Rect Rect::operator&&(const Rect&) const
  331. {
  332.     // Obviously unimplemented yet.
  333.     
  334.     Rect returnRect;
  335.     
  336.     returnRect.top = 0;
  337.     returnRect.left = 0;
  338.     returnRect.bottom = 0;
  339.     returnRect.right = 0;
  340.     
  341.     return returnRect;
  342. }
  343.  
  344. Rect Rect::operator||(const Rect& rt) const
  345. {
  346.     Rect returnRect;
  347.     
  348.     returnRect.top = Min(top, rt.top);
  349.     returnRect.left = Min(left, rt.left);
  350.     returnRect.bottom = Max(bottom, rt.bottom);
  351.     returnRect.right = Max(right, rt.right);
  352.     
  353.     return returnRect;
  354. }
  355.  
  356.  
  357. //--------------------------------------------------------------------------------
  358. // Returns true if a valid rectangle (left < right and top < bottom). If not
  359. // a valid rectangle then return false and set all coordinates to 0.
  360. //--------------------------------------------------------------------------------
  361.  
  362. Boolean Rect::ValidRect()
  363. {
  364.     if (left < right && top < bottom)
  365.         return true;
  366.     else
  367.     {
  368.         top = left = 0;
  369.         bottom = right = 0;
  370.         return false;
  371.     }
  372. }
  373.  
  374.  
  375. //--------------------------------------------------------------------------------
  376. // Empty returns true if the rectangle is empty.
  377. //--------------------------------------------------------------------------------
  378.  
  379. Boolean Rect::Empty() const
  380. {
  381.     return left - right <= 0 && bottom - top <= 0;
  382. }
  383.  
  384.  
  385. //--------------------------------------------------------------------------------
  386. // Length returns the length of a Rect in a given dimension.
  387. //--------------------------------------------------------------------------------
  388.  
  389. short Rect::Length(VHSelect sel) const
  390. {
  391.     if (sel == vSel)
  392.         return bottom - top;
  393.     else
  394.         return right - left;
  395. }
  396.  
  397.  
  398. //--------------------------------------------------------------------------------
  399. // Size returns the size of a Rect as a Point.
  400. //--------------------------------------------------------------------------------
  401.  
  402. Point Rect::Size() const
  403. {
  404.     return Point(bottom - top, right - left);
  405. }
  406.  
  407.  
  408. //--------------------------------------------------------------------------------
  409. // The Contains method takes either a Point or a Rect and returns true if the
  410. // operand is inside of the Rect the method is applied to.
  411. //--------------------------------------------------------------------------------
  412.  
  413. Boolean Rect::Contains (const Point& pt) const
  414. {
  415.     // Does the point 'pt' lie within the rectangle of 'this'?
  416.     
  417.     return pt.v >= top && pt.v <= bottom && pt.h >= left && pt.h <= right;
  418. }
  419.  
  420. Boolean Rect::Contains (const Rect& rt) const
  421. {
  422.     // Does the rectangle 'rt' lie withing the rectagle of 'this'?
  423.     
  424.     return Contains (rt[topLeft]) && Contains (rt[botRight]);
  425. }
  426.  
  427.  
  428. //--------------------------------------------------------------------------------
  429. // Max and Min are two private methods for comparing individual coordinates
  430. // of Rects.
  431. //--------------------------------------------------------------------------------
  432.  
  433. short Rect::Min (const short a, const short b) const
  434. {
  435.     return a < b ? a : b;
  436. }
  437.  
  438. short Rect::Max (const short a, const short b) const
  439. {
  440.     return a > b ? a : b;
  441. }
  442.