home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / df3os2.zip / RECTANGL.CPP < prev    next >
C/C++ Source or Header  |  1992-11-21  |  1KB  |  56 lines

  1. // ------------- rectangl.cpp
  2.  
  3. #include "rectangl.h"
  4.  
  5. // ---- is a point within a vector?
  6. static inline Bool within(int p, int v1, int v2)
  7. {
  8.     return (Bool) (p >= v1 && p <= v2);
  9. }
  10.  
  11. // --- Produce vector end points of the overlap of two other vectors
  12. static void subVector(int &v1, int &v2,
  13.                         int t1, int t2, int o1, int o2)
  14. {
  15.     v1 = v2 = -1;
  16.     if (within(o1, t1, t2))    {
  17.         v1 = o1;
  18.         if (within(o2, t1, t2))
  19.             v2 = o2;
  20.         else
  21.             v2 = t2;
  22.     }
  23.     else if (within(o2, t1, t2))    {
  24.         v2 = o2;
  25.         if (within(o1, t1, t2))
  26.             v1 = o1;
  27.         else
  28.             v1 = t1;
  29.     }
  30.     else if (within(t1, o1, o2))    {
  31.         v1 = t1;
  32.         if (within(t2, o1, o2))
  33.             v2 = t2;
  34.         else
  35.             v2 = o2;
  36.     }
  37.     else if (within(t2, o1, o2))    {
  38.         v2 = t2;
  39.         if (within(t1, o1, o2))
  40.             v1 = t1;
  41.         else
  42.             v1 = o1;
  43.     }
  44. }
  45.  
  46. // --- Return rectangle produced by the overlap of two other rectangles
  47. Rect Rect::subRectangle(Rect &rc)
  48. {
  49.     Rect r(0,0,0,0);
  50.     subVector(r.left, r.right, left, right, rc.left, rc.right);
  51.     subVector(r.top, r.bottom, top, bottom, rc.top, rc.bottom);
  52.     return r;
  53. }
  54.  
  55.  
  56.