Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Rect.h

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id:  $
00022 */
00023 
00024 
00025 #ifndef TEDDY__MATHS__RECT__H
00026 #define TEDDY__MATHS__RECT__H
00027 
00028 
00029 #include "Teddy/Maths/Vector2.h"
00030 #include "Teddy/SysSupport/StdMaths.h"
00031 #include "Teddy/SysSupport/StdIO.h"
00032 
00033 
00034 namespace Teddy {
00035 namespace Maths {
00036 
00037 
00038 #ifndef SWIG
00039 #define Rect    TRect<float>
00040 #define IntRect TRect<int>
00041 #endif
00042 
00043 
00044 template<typename T> class TRect {
00045 public:
00046     TVector2<T> min;
00047     TVector2<T> max;
00048 
00049     TRect(){}
00050 /*  TRect( const TVector2<T> &a, const TVector2<T> &b ){
00051         min = a;
00052         max = b;
00053     }*/
00054     TRect( const TVector2<int> &a, const TVector2<int> &b ){
00055         min = TVector2<T>(a);
00056         max = TVector2<T>(b);
00057    }
00058     TRect( const TVector2<float> &a, const TVector2<float> &b ){
00059         min = TVector2<T>(a);
00060         max = TVector2<T>(b);
00061     }
00062     TRect( const TVector2<double> &a, const TVector2<double> &b ){
00063         min = TVector2<T>(a);
00064         max = TVector2<T>(b);
00065     }
00066     TRect( const TRect<int> &r ){
00067         min = TVector2<T>(r.min);
00068         max = TVector2<T>(r.max);
00069     }
00070     TRect( const TRect<float> &r ){
00071         min = TVector2<T>(r.min);
00072         max = TVector2<T>(r.max);
00073     }
00074     TRect( const TRect<double> &r ){
00075         min = TVector2<T>(r.min);
00076         max = TVector2<T>(r.max);
00077     }
00078 
00079     TVector2<T> getSize() const {
00080         TVector2<T> ret_val = TVector2<T>(
00081             max[0] - min[0],
00082             max[1] - min[1]
00083         );
00084         return ret_val;
00085     }
00086 
00087     T getArea() const {
00088         return (max[0] - min[0] + 1) * (max[1] - min[1] + 1);
00089     }
00090 
00091 
00092     void setSize( const TVector2<T> &size ){
00093         max = min + size - TVector2<T>(1,1);
00094     }
00095 
00096     const TRect &intersect( const TRect &a ){
00097         min = TVector2<T>( MAX( min[0], a.min[0] ), MAX( min[1], a.min[1] ) );
00098         max = TVector2<T>( MIN( max[0], a.max[0] ), MIN( max[1], a.max[1] ) );
00099         return *this;
00100     }
00101 
00102     TRect grow( const TVector2<T> v ){
00103         return TRect( min - v, max + v );
00104     }
00105 
00106     TRect shrink( const TVector2<T> v ){
00107         return TRect( min + v, max - v );
00108     }
00109 
00110     bool hit( const TVector2<T> v ) const {
00111         return
00112             (v[0] >= min[0]) &&
00113             (v[0] <= max[0]) &&
00114             (v[1] >= min[1]) &&
00115             (v[1] <= max[1]);
00116     }
00117 
00118     const TRect &setPosition( const TVector2<T> &pos ){
00119         move( pos - min );
00120         return *this;
00121     }
00122 
00123     //  Translation
00124     const TRect &move( const TVector2<T> &v ){ min += v; max += v; return *this; }
00125     const TRect &operator +=( const TVector2<T> &v )       { min += v; max += v; return *this; }
00126           TRect  operator + ( const TVector2<T> &v ) const { return TRect( min+v, max+v ); }
00127     const TRect &operator -=( const TVector2<T> &v )       { min -= v; max -= v; }
00128           TRect  operator - ( const TVector2<T> &v ) const { return TRect( min-v, max-v ); }
00129 };
00130 
00131 
00132 #ifdef SWIG
00133 %template(Rect)            TRect<float>;
00134 %template(IntRect)         TRect<int>;
00135 #endif
00136 
00137 
00138 };  //  namespace Maths
00139 };  //  namespace Teddy
00140 
00141 
00142 #endif  //  TEDDY__MATHS__RECT__H
00143