home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dokpr1.zip / rect.h < prev    next >
C/C++ Source or Header  |  1995-10-11  |  5KB  |  169 lines

  1. /**************************************************************************
  2.  *                                                                        *
  3.  *                                                                        *
  4.  *          This code is copyright (c) 1994                               *
  5.  *                     Athena Design, Inc.                                *
  6.  *                                                                        *
  7.  *                                                                        *
  8.  *                ALL RIGHTS RESERVED                                     *
  9.  *                                                                        *
  10.  *                                                                        *
  11.  *                                                                        *
  12.  *                                                                        *
  13.  *                                                                        *
  14.  **************************************************************************/
  15.  
  16.  
  17. /*
  18.     this header file contains the code for the MRect rectangle object
  19.     2-20-94 dpp
  20.  
  21.     94-08-30 dpp added support for offseting the X and Y values
  22.     94-08-31 dpp allow a rectangle to archive itself out
  23.     94-09-03 dpp added operator==() and setNull() and isNull()
  24.     94-09-04 dpp added doesIntersect()
  25.     94-09-17 dpp added the MPoint class
  26. */
  27.  
  28. #ifndef _MH_mrect
  29.  
  30. #define _MH_mrect
  31.  
  32. class MStream; // we have to know what a stream is to archive ourselves
  33.  
  34. // the MPoint class is very simply a single point in an xy coordinate space
  35. // 94-09-17 dpp
  36. class MPoint
  37. {
  38.     public:
  39.     void init() {x = 0; y = 0;};
  40.     void init(const MPoint *mp) {x = mp -> x; y = mp -> y;};
  41.     void init(int x1,int y1) {x = (float) x1; y = (float) y1;};
  42.     void free() {};
  43.     void write(MStream *) const;
  44.     void init(MStream *);
  45.     void scaleUp(float);
  46.     void scaleDown(float);
  47.     
  48.     void set(float x1,float y1) {x = x1; y = y1;};
  49.  
  50.     void copyFromPOINTL(const void *);
  51.     void copyToPOINTL(void *) const;
  52.  
  53.     float getX() const {return x;};
  54.     float getY() const {return y;};
  55.     void setX(float i) {x = i;};
  56.     void setY(float i) {y = i;};
  57.     void offsetX(float i) {x += i;};
  58.     void offsetY(float i) {y += i;};
  59.  
  60.     // int operator==(const MPoint &mp) const {return (x == mp.x && y == mp.y);};
  61.     // MPoint &operator=(const MPoint &mp) {x = mp.x; y = mp.y; return *this;};
  62.     void translate(float xt,float yt) {x -= xt; y -= yt;};
  63.     void rotate( float );
  64.     
  65.     private:
  66.     float x,y;  // the x and y coordinates
  67. };
  68.  
  69. class MRect {
  70.     public:
  71.     MRect() {x = y = wid = hi = 0.0;};
  72.     void init() {x = y = wid = hi = 0.0;};
  73.     void init(float tx,float ty,float twid,float thi) {x = tx;
  74.         y = ty; wid = twid; hi = thi;};
  75.  
  76.     void init(const MRect *re) {*this = *re;};
  77.  
  78.     // because this class takes up no allocated storage, do nothing on a free
  79.     // 94-08-31 dpp
  80.     void free() {};
  81.     
  82.     void scaleUp(float);
  83.     void scaleDown(float);
  84.  
  85.     void set(float tx,float ty,float twid,float thi) {x = tx;
  86.         y = ty; wid = twid; hi = thi;};
  87.     void copyFromRECTLNorm(const void *);
  88.     void copyToRECTLNorm(void *) const;
  89.  
  90.     void copyFromRECTLInc(const void *);
  91.     void copyToRECTLInc(void *) const;
  92.  
  93.     float getX() const {return x;};
  94.     float getY() const {return y;};
  95.     float getWid() const {return wid;};
  96.     float getHi() const {return hi;};
  97.     void setX(float i) {x = i;};
  98.     void setY(float i) {y = i;};
  99.     void setWid(float i) {wid = i;};
  100.     void setHi(float i) {hi = i;};
  101.     float getCX() const {return x + wid;};
  102.     float getCY() const {return y + hi;};
  103.     void setCX(float tx) {wid = tx - x;};
  104.     void setCY(float ty) {hi = ty - y;};
  105.  
  106.     // offset the values
  107.     void offset(float v1, float v2) { x+=v1 ; y += v2;};
  108.     void offsetX(float v) {x += v;};
  109.     void offsetY(float v) {y += v;};
  110.     void offsetWid(float v) {wid += v;};
  111.     void offsetHi(float v) {hi += v;};
  112.  
  113.     MRect *insetRect(float,float);
  114.     MRect *inset(float x,float y) {return insetRect(x,y);};
  115.     int intersectRect(const MRect *);
  116.     int pointIn(const void *) const;
  117.     int pointIn(const MPoint *) const;
  118.     int inSide(const MRect *) const;
  119.     int clipRect( const MRect * );
  120.     int sloppyClipRect( const MRect * );    // FIXME
  121.  
  122.     void unionRect( const MRect *);
  123.     void clipTo(const MRect *);    
  124.  
  125.  
  126.     // Write or initialize yourself from a stream
  127.     // 94-08-31 dpp
  128.     void write(MStream *) const;
  129.     void init(MStream *);
  130.  
  131.     // compare two rectangles.  Return 1 if they are the same
  132.     // 94-09-03 dpp
  133.     int operator==(const MRect &) const;
  134.  
  135.     // this method makes a rectangle NULL
  136.     // 94-09-03 dpp
  137.     void setNull() {x = y = wid = hi = -1.0;};
  138.  
  139.     // returns 1 if the rectangle is NULL
  140.     // 94-09-03 dpp
  141.     int isNull() const {if (x == -1 && y == -1 && wid == -1 && hi == -1) return 1; return 0;};
  142.  
  143.     // returns 1 if the two rectangles intersect
  144.     // 94-09-04 dpp
  145.     int doesIntersect(const MRect *) const;
  146.  
  147.     void sizeByCorner(int corner,float dx,float dy);
  148.     
  149.     // round the rectangle to integer coordinates
  150.     // 95-04-18 dpp
  151.     void round();
  152.  
  153.     private:
  154.     float x,y,wid,hi;
  155. };
  156.  
  157. const int MUpperLeftCorner = 1;
  158. const int MUpperRightCorner = 2;
  159. const int MUpperMidCorner = 3;
  160. const int MLowerLeftCorner = 4;
  161. const int MLowerRightCorner = 5;
  162. const int MLowerMidCorner = 6;
  163. const int MMidLeftCorner = 7;
  164. const int MMidRightCorner = 8;
  165.  
  166.  
  167.  
  168. #endif
  169.