home *** CD-ROM | disk | FTP | other *** search
/ Computer Panoráma / computer_panorama_1997-12-hibas.iso / SHARE / GRAPH / PTC051.ZIP / SRC / RECT.H < prev    next >
C/C++ Source or Header  |  1997-09-20  |  2KB  |  154 lines

  1. /////////////////////
  2. // rectangle class //
  3. /////////////////////
  4.  
  5. #ifndef __RECTANGLE_H
  6. #define __RECTANGLE_H
  7.  
  8. #include "misc.h"
  9. #include "globals.h"
  10. #include <math.h>
  11. #include <stdlib.h>
  12.  
  13.  
  14.  
  15.  
  16.                               
  17.  
  18.  
  19. class RECTANGLE
  20. {
  21.     public:
  22.  
  23.         // construct
  24.         inline RECTANGLE();
  25.         inline RECTANGLE(float x1,float y1,float x2,float y2,int mode=ABSOLUTE);
  26.         
  27.         // fix rectangle
  28.         inline void fix();
  29.  
  30.         // cool stuff
  31.         inline float width() const;
  32.         inline float height() const;
  33.         inline float area() const;
  34.  
  35.         // operators
  36.         inline int operator ==(RECTANGLE const &other) const;
  37.         inline int operator !=(RECTANGLE const &other) const;
  38.  
  39.         // operation mode
  40.         char mode;
  41.  
  42.         // data
  43.         float x1,y1,x2,y2;
  44. };
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. inline RECTANGLE::RECTANGLE()
  54. {
  55.     // defaults
  56.     x1=(float)0;
  57.     y1=(float)0;
  58.     x2=(float)0;
  59.     y2=(float)0;
  60.     mode=0;
  61. }
  62.  
  63.  
  64. inline RECTANGLE::RECTANGLE(float ix1,float iy1,float ix2,float iy2,int imode)
  65. {
  66.     // init
  67.     x1=ix1;
  68.     y1=iy1;
  69.     x2=ix2;
  70.     y2=iy2;
  71.     mode=(char)imode;
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. inline void RECTANGLE::fix()
  81. {
  82.     // fix x coords so that x1 is leftmost
  83.     float temp;
  84.     if (x1>x2)
  85.     {
  86.         temp=x1;
  87.         x1=x2;
  88.         x2=temp;
  89.     }
  90.  
  91.     // fix y coords so that y1 is topmost
  92.     if (y1>y2)
  93.     {
  94.         temp=y1;
  95.         y1=y2;
  96.         y2=temp;
  97.     }
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. inline float RECTANGLE::width() const
  107. {
  108.     // rect width
  109.     return x2-x1;
  110. }
  111.  
  112.  
  113. inline float RECTANGLE::height() const
  114. {
  115.     // rect height
  116.     return y2-y1;
  117. }
  118.  
  119.  
  120. inline float RECTANGLE::area() const
  121. {
  122.     // rect area
  123.     return (float)fabs((x2-x1)*(y2-y1));
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. inline int RECTANGLE::operator ==(RECTANGLE const &other) const
  133. {
  134.     if (mode!=other.mode || x1!=other.x1 || y1!=other.y1 || x2!=other.x2 || y2!=other.y2) return 0;
  135.     else return 1;
  136. }
  137.  
  138.  
  139. inline int RECTANGLE::operator !=(RECTANGLE const &other) const
  140. {
  141.     return !(*this==other);
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. #endif
  154.