src/tool/rectangle.h

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Rectangle.h: Standard C++ Rectangle template
00020  ******************************************************************************
00021  * 2005/09/21:  Jean-Christophe Duberga (jcduberga@gmx.de) 
00022  *              Initial version
00023  *****************************************************************************/
00024 
00025 #ifndef _RECTANGLE_H
00026 #define _RECTANGLE_H
00027 
00028 #include <cmath>
00029 #include "vector2.h"
00030 
00036 template<class T> class rectangle
00037 {
00038         protected:
00040                 Vector2<T> position;
00042                 Vector2<T> size; 
00043 
00044         public:
00048                 inline rectangle(){
00049                 }
00050                 
00059                 inline rectangle(T x, T y, T width, T height){
00060                         position.SetValues( x, y );
00061                         size.SetValues( width, height );
00062                 }
00063 
00070                 inline rectangle(Vector2<T> thePosition, Vector2<T> theSize){
00071                         position = thePosition;
00072                         size = theSize;
00073                 }
00074 
00081                 inline void SetPosition(T x, T y){
00082                         position.SetValues(x, y);
00083                 }
00084 
00090                 inline void SetPosition(const Vector2<T> &newPos){
00091                         position = newPos;
00092                 }
00093 
00099                 inline void SetPositionX(T x){
00100                         position.x = x;
00101                 }
00102 
00108                 inline void SetPositionY(T y){
00109                         position.y = y;
00110                 }
00111 
00118                 inline void SetSize(T sizeX, T sizeY){
00119                         size.SetValues(sizeX, sizeY);
00120                 }
00121 
00127                 inline void SetSizeX(T sizeX){
00128                         size.x = sizeX;
00129                 }
00130 
00136                 inline void SetSizeY(T sizeY){
00137                         size.y = sizeY;
00138                 }
00139 
00140                 inline void SetSize(Vector2<T> newSize){
00141                         size = newSize;
00142                 }
00143 
00144                 inline rectangle<T> GetRectangle() const{
00145                         return *this;
00146                 }
00147 
00151                 inline Vector2<T> GetPosition() const{
00152                         return position;
00153                 }
00154                 
00155                 inline T GetPositionX() const{
00156                         return position.x;                      
00157                 }
00158 
00159                 inline T GetPositionY() const{
00160                         return position.y;
00161                 }
00162 
00166                 inline Vector2<T> GetSize() const{
00167                         return size;
00168                 }
00169                 
00170                 inline T GetSizeX() const{
00171                         return size.x;
00172                 }
00173 
00174                 inline T GetSizeY() const{
00175                         return size.y;
00176                 }
00177 
00183                 void Clip( const rectangle &cr){
00184                         if( !Intersect(cr) ){
00185                                 size.x = 0;
00186                                 size.y = 0;
00187 
00188                                 return;
00189                         }
00190                         
00191                         Vector2<T> newPositionBR = GetBottomRightPoint();
00192 
00193                         if( position.x < cr.position.x )
00194                                 position.x = cr.position.x;
00195                         
00196                         if( position.x > cr.GetBottomRightPoint().x )
00197                                 position.x = cr.GetBottomRightPoint().x;
00198 
00199                         if( position.y < cr.position.y )
00200                                 position.y = cr.position.y;
00201                         
00202                         if( position.y > cr.GetBottomRightPoint().y )
00203                                 position.y = cr.GetBottomRightPoint().y;
00204 
00205                         if( newPositionBR.x < cr.position.x )
00206                                 newPositionBR.x = cr.position.x;
00207                         
00208                         if( newPositionBR.x > cr.GetBottomRightPoint().x )
00209                                 newPositionBR.x = cr.GetBottomRightPoint().x;
00210 
00211                         if( newPositionBR.y < cr.position.y )
00212                                 newPositionBR.y = cr.position.y;
00213                         
00214                         if( newPositionBR.y > cr.GetBottomRightPoint().y )
00215                                 newPositionBR.y = cr.GetBottomRightPoint().y;
00216 
00217                         size = newPositionBR - position + 1 ;
00218                         assert( cr.Contains( *this ) );
00219                 }
00220 
00226                 inline bool Contains( const Vector2<T> p ) const{
00227                         if( IsSizeZero() )
00228                                 return false;
00229 
00230                         return p >= GetTopLeftPoint() &&
00231                                 p <= GetBottomRightPoint();
00232                 }
00233 
00239                 inline bool Contains( const rectangle<T> &r2 ) const{
00240                         if( r2.IsSizeZero() )
00241                                 return false;
00242                         
00243                         return Contains( r2.GetTopLeftPoint() ) &&
00244                                 Contains( r2.GetBottomRightPoint() );
00245                 }
00246 
00253                 inline bool Intersect( const rectangle<T> &r2 ) const{
00254                         if( IsSizeZero() || r2.IsSizeZero() )
00255                                 return false;
00256                         
00257                         Vector2<T> r1BR = GetBottomRightPoint();
00258                         Vector2<T> r2BR = r2.GetBottomRightPoint();
00259                         Vector2<T> r1TL = GetTopLeftPoint();
00260                         Vector2<T> r2TL = r2.GetTopLeftPoint();
00261 
00262                         if( r1BR.x < r2TL.x || r1BR.y < r2TL.y ||
00263                                    r2BR.x < r1TL.x || r2BR.y < r1TL.y ) 
00264                                 return false;
00265 
00266                         return true;
00267                 }
00268 
00275                 inline Vector2<T> GetTopLeftPoint() const{
00276                         assert( !IsSizeZero() );
00277                         return position;
00278                 }
00279 
00286                 inline Vector2<T> GetTopRightPoint() const{
00287                         assert( !IsSizeZero() );
00288                         Vector2<T> r = position;
00289 
00290                         r.x += size.x - 1;
00291 
00292                         return r;
00293                 }
00294 
00301                 inline Vector2<T> GetBottomLeftPoint() const{
00302                         assert( !IsSizeZero() );
00303                         Vector2<T> r = position;
00304 
00305                         r.y += size.y - 1;
00306 
00307                         return r;
00308                 }
00309 
00315                 inline Vector2<T> GetBottomRightPoint() const{
00316                         assert( !IsSizeZero() );
00317                         return position + size - 1;
00318                 }
00319 
00323                 inline bool IsSizeZero() const{
00324                         return size.IsXNull() || size.IsYNull();
00325                 }
00326 };
00327 
00328 typedef rectangle<int>    Rectanglei;
00329 typedef rectangle<float>  Rectanglef;
00330 typedef rectangle<double> Rectangled;
00331 #endif // _RECTANGLE_H

Generated on Mon Jan 1 13:10:59 2007 for Wormux by  doxygen 1.4.7