Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

xrect.h

Go to the documentation of this file.
00001 #if !defined( __ARSRECT_H__ )
00002 #define __ARSRECT_H__
00003 
00004 namespace Ars
00005 {
00006 
00007 class XPoint    {
00008 public:
00009         int     x , y;
00010 
00011 // Constructors
00012 
00013         // create from two integers
00014         XPoint( const int initX = 0, const int initY = 0 ) : x( initX ) , y( initY )    {}
00015 
00016         // create from another point
00017         XPoint( const XPoint &initPt) : x( initPt.x ) , y( initPt.y )   {};
00018 
00019 // Operations
00021         XPoint operator+( const XPoint& p) const        {       return XPoint( x + p.x , y + p.y );     }
00023         XPoint operator-(const XPoint& p) const {       return XPoint( x - p.x , y - p.y );     }
00024 
00025         XPoint &operator/=( const double v )    {       x = (int)(x / v);       y = (int)(y / v);       return *this;   }
00026         XPoint &operator*=( const double v )    {       x = (int)(x * v);       y = (int)(y * v);       return *this;   }
00027         XPoint &operator/=( const int v )       {       x /= v; y /= v; return *this;   }
00028         XPoint &operator*=( const int v )       {       x *= v; y *= v; return *this;   }
00029         XPoint &operator+=( const int v )       {       x += v; y += v; return *this;   }
00030         XPoint &operator-=( const int v )       {       x -= v; y -= v; return *this;   }
00031 
00033         XPoint &operator=(const XPoint& p)      {       x = p.x;        y = p.y;        return *this;   }
00034 
00036         const bool operator==(const XPoint& p) const { return ((x == p.x) && (y == p.y)); }
00037 
00039         const bool leftof(const XPoint& p) const { return (x < p.x); }
00040 
00042         const bool rightof(const XPoint& p) const { return (x > p.x); }
00043 
00045         const bool above(const XPoint& p) const { return (y < p.y); }
00046 
00048         const bool below(const XPoint& p) const { return (y > p.y); }
00049 
00050         const double dist( void )       
00051         {       return sqrt( (double)x * x + (double)y * y );   }
00052 };
00053 
00055 class XRect     : public SDL_Rect       {
00056 public:
00058         XRect( void )
00059         {       x = 0;  y = 0;  w = 0;  h = 0;  }  // constructor
00060 
00065         XRect(const int ax, const int ay, const int aw, const int ah) 
00066         {       x = ax; y = ay; w = aw; h = ah; }  // constructor
00067 
00071         XRect( const XPoint &p1 , const XPoint &p2 ) 
00072         {
00073                 x = min( p2.x , p1.x ); y = min( p2.y , p1.y );
00074                 w = max( p2.x , p1.x ) - x;
00075                 h = max( p2.y , p1.y ) - y;
00076         }  // constructor
00077 
00080         XRect( const XRect &r ) 
00081         {       x = r.x;        y = r.y;        w = r.w;        h = r.h;        }  // constructor
00082 
00084         virtual ~XRect( void ) { }
00085 
00087         int Width(void) const { return (short int)w; }
00088 
00090         int Height(void) const { return (short int)h; }
00091 
00093         XPoint Size() const     {       return XPoint( w , h ); }
00094 
00096         XPoint TopLeft(void) const { return XPoint( x , y ); }
00097 
00099         XPoint TopRight(void) const { return XPoint( x + w, y ); }
00100 
00102         XPoint BottomLeft(void) const { return XPoint( x , y + h ); }
00103 
00105         XPoint BottomRight(void) const { return XPoint( x + w , y + h); }
00106 
00107         const int Right( void ) const           {       return x + Width();     }
00108         const int Bottom( void ) const  {       return y + Height();    }
00109 
00110         void SetBottom( const int y2 )  {       h = abs( y2 - y );      }
00111         void SetRight( const int x2 )   {       w = abs( x2 - x );      }
00112 
00114         XPoint Center(void) const       { return XPoint( x + (Width() >> 1) , y + (Height() >> 1) );    }
00115 
00118         operator SDL_Rect( void )       {       return static_cast<SDL_Rect>( *this );  }
00119         operator const SDL_Rect( void ) const   {       return static_cast<const SDL_Rect>( *this );    }
00120 
00122         XRect &operator=( const XRect &r )      {       x = r.x;        y = r.y;        w = r.w;        h = r.h;        return *this;   }  // assignment operator
00123         XRect &operator=( const SDL_Rect &r )   {       x = r.x;        y = r.y;        w = r.w;        h = r.h;        return *this;   }  // assignment operator
00124 
00127         XRect operator+( const XPoint &p ) const        {       return XRect( x + p.x , y + p.y , w , y );      }
00128 
00131         XRect operator-(const XPoint& p) const  {       return XRect( x - p.x , y - p.y , w , y );      }
00132 
00138         XRect &Grow( const int iGrowAmount )    {       x -= iGrowAmount;       y -= iGrowAmount;       w += 2 * iGrowAmount;   h += 2 * iGrowAmount;   return *this;   }
00139 
00143         const bool Overlaps( const XRect &r )
00144         {
00145                 return (HitTest(r.TopLeft()) == RELPOS_INSIDE || HitTest(r.TopRight()) == RELPOS_INSIDE || HitTest(r.BottomRight()) == RELPOS_INSIDE
00146                          || HitTest(r.BottomLeft()) == RELPOS_INSIDE || r.HitTest(TopLeft()) == RELPOS_INSIDE || r.HitTest(TopRight()) == RELPOS_INSIDE
00147                          || r.HitTest(BottomRight()) == RELPOS_INSIDE || r.HitTest(BottomLeft()) == RELPOS_INSIDE);
00148         }
00149 
00152         void ClipTo( const XRect &r )
00153         {
00154                 if( Overlaps( r ) )
00155                 {
00156                         if( x < r.x )
00157                                 x = r.x;
00158                         if( Right() > r.Right() )
00159                                 SetRight( r.Right() );
00160         
00161                         if( y < r.y )   
00162                                 y = r.y;
00163                         if( Bottom() > r.Bottom() )
00164                                 SetBottom( r.Bottom() );
00165                 }
00166                 else
00167                         w = h = 0;
00168         }
00169 
00170         enum ERelativePosition
00171         {
00172                 RELPOS_ABOVE = 1, 
00173                 RELPOS_BELOW = 2, 
00174                 RELPOS_LEFT = 4, 
00175                 RELPOS_RIGHT = 8, 
00176                 RELPOS_INSIDE = 16 
00177         };
00178 
00182         unsigned int HitTest( const XPoint &p ) const
00183         { 
00184                 unsigned int eRelPos = 0;
00185 
00186                 eRelPos |= (p.x < x) ? RELPOS_LEFT : 0;
00187                 eRelPos |= (p.y < y) ? RELPOS_ABOVE: 0;
00188                 eRelPos |= (p.x > (x+w)) ? RELPOS_RIGHT : 0;
00189                 eRelPos |= (p.y > (y+h)) ? RELPOS_BELOW: 0;
00190                 eRelPos |= (p.x >= x && p.x <= (x+w) && p.y >= y && p.y <= (y+h)) ? RELPOS_INSIDE : 0;
00191 
00192                 return eRelPos;
00193         }
00194 };
00195 
00196 static XRect    emptyRect( 0 , 0 , 0 , 0 );
00197 
00198 }
00199 
00200 #endif
00201 

Generated on Fri Dec 5 04:06:01 2003 for Borqueror by doxygen 1.3.3