Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

RectZone2D.hpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00020 #ifndef _RectZone2D_hpp
00021 #define _RectZone2D_hpp
00022 
00023 #include<math.h>
00024 #include<ImLib3D/Vect2D.hpp>
00025 
00027 template<class RectType,class Real=RectType>
00028 class RectZone2D
00029 {
00030 protected:
00031 public:
00032     typedef Vect2D<RectType,Real> Vect2DType;
00033 
00034     RectType x0,y0;
00035     RectType x1,y1;
00036 
00037     RectType Width()  const {return x1-x0+1;}
00038     RectType Height() const {return y1-y0+1;}
00039     Vect2DType GetP0(){return Vect2DType(x0,y0);}
00040     Vect2DType GetP1(){return Vect2DType(x1,y1);}
00041 
00042     Vect2Df SelfCoords(const Vect2DType &P)
00043     {
00044         return Vect2Df((P.x-x0)/(float)(x1-x0),
00045                        (P.y-y0)/(float)(y1-y0) );
00046     }
00047 
00048     void Translate(const float x,const float y)
00049     {
00050         x0+=x;
00051         y0+=y;
00052         x1+=x;
00053         y1+=y;
00054     }
00055     inline void ToStart(RectType &x,RectType &y) const {x=x0;y=y0;}
00056     bool IsInside(const Vect2DType & vect) const {return IsInside(vect.x, vect.y);}
00057     bool IsInside(RectType x,RectType y) const 
00058     {
00059         if( x<x0 ) {return false;}
00060         if( y<y0 ) {return false;}
00061         if( x>x1 ) {return false;}
00062         if( y>y1 ) {return false;}
00063         return true;
00064     }
00065     bool operator==(const RectZone2D<RectType> &other)
00066     {
00067         return 
00068             x0==other.x0 &&
00069             y0==other.y0 &&
00070             x1==other.x1 &&
00071             y1==other.y1    ;
00072     }
00073     RectZone2D( const Vect2DType &p0,const Vect2DType p1):
00074         x0(p0.x),y0(p0.y),
00075         x1(p1.x),y1(p1.y)
00076     {
00077 
00078     }
00079     RectZone2D( RectType _x0, RectType _y0,
00080                 RectType _x1, RectType _y1) :
00081         x0(_x0),y0(_y0),
00082         x1(_x1),y1(_y1)
00083     {
00084         ;
00085     }
00086     RectZone2D() :
00087         x0(0),y0(0),
00088         x1(-1),y1(-1)
00089     {;}
00090 };
00091 
00092 typedef RectZone2D<float> RectZone2Df;
00093 
00095 class RectZone2Di : public RectZone2D<int,float>
00096 {
00097 public:
00098     inline int  ToNext(int &x,int &y) const
00099     {
00100         x++;
00101         if(x>x1)
00102         {
00103             x=x0;
00104             y++;
00105             if(y>y1){return 2;}
00106             return 1;
00107         }
00108         else { return 0;}
00109     }
00110     inline int  ToPrev(int &x,int &y) const
00111     {
00112         x--;
00113         if(x<x0)
00114         {
00115             x=x1;
00116             y--;
00117             if(y<y0){return 2;}
00118             return 1;
00119         }
00120         else { return 0;}
00121     }
00122 
00123     RectZone2Di ( const RectZone2Df & , int );
00124     RectZone2Di (const Vect2Di &p0,const Vect2Di &p1):
00125         RectZone2D<int,float>(p0,p1)
00126     {
00127         ;
00128     }
00129     RectZone2Di( int _x0, int _y0,
00130                  int _x1, int _y1 ) :
00131         RectZone2D<int,float>(_x0,_y0,_x1,_y1)
00132     {
00133         ;
00134     }
00135     RectZone2Di()
00136     {
00137         ;
00138     }
00139 
00140 };
00141 
00142 
00143 inline RectZone2Di::RectZone2Di( const RectZone2Df &s0, int type)
00144 {
00145     if(type==0)
00146     {
00147         x0= (int)ceil (s0.x0);
00148         y0= (int)ceil (s0.y0);
00149         x1= (int)floor(s0.x1);
00150         y1= (int)floor(s0.y1);
00151     }
00152     else
00153     {
00154         x0= (int)floor(s0.x0);
00155         y0= (int)floor(s0.y0);
00156         x1= (int)ceil (s0.x1);
00157         y1= (int)ceil (s0.y1);
00158     }
00159 }
00160 
00161 #endif // _RectZone2D_hpp

Generated on Fri Jun 17 13:36:06 2005 for ImLib3D by  doxygen 1.4.2