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

Vect2D.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998 Marcel Bosc
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
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  */
00021 #ifndef Vect2D_H
00022 #define Vect2D_H
00023 
00024 #include<stdio.h>
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027 #include <math.h>
00028 #include<string.h>
00029 #include<ImLib3D/CppTools.hpp>
00030 
00031 
00032 // ********************** Vect2D ********************
00034 template<class Value,class Real=Value>
00035 class Vect2D
00036 {
00037 public:
00038     Value x,y;
00039 
00040     ~Vect2D(){;}
00041     Vect2D(Value xi,Value yi){x=xi;y=yi;}
00042     Vect2D(){;}
00043 
00044     Real Norme2()const {return(x*x+y*y);}
00045     Real Norm2() const {return(x*x+y*y);}
00046     Real Norme() const {return(sqrt(Norme2()));}
00047     Real Norm()  const {return(sqrt(Norme2()));}
00048     Real Unit();
00049 
00050     Real Dist2(const Vect2D &p2) const {return((p2.x-x)*(p2.x-x)+(p2.y-y)*(p2.y-y));}
00051     Real Dist(const Vect2D &p2) const {return(sqrt(Dist2(p2)));}
00052     Real Distance2(const Vect2D &p2) const {return((p2.x-x)*(p2.x-x)+(p2.y-y)*(p2.y-y));}
00053     Real Distance(const Vect2D &p2) const {return(sqrt(Dist2(p2)));}
00054 
00055     void Show() const {Show("","");}
00056     void Show(char *p) const {Show(p,"");}
00057     void Show(char *p,char *s) const 
00058     {
00059         if(IsSameType<Value,int>())
00060         {mprintf("%s%d %d%s",p,(int)x,(int)y,s);}
00061         else{mprintf("%s%f %f%s",p,(float)x,(float)y,s);}
00062     }
00063 
00064     Value & operator()(int i)
00065     {
00066         switch(i)
00067         {case 0:return x;case 1:return y;}
00068         return x;
00069     }
00070 
00071     Value  operator()(int i) const
00072     {
00073         switch(i)
00074         {case 0:return x;case 1:return y;}
00075         return x;
00076     }
00077     Vect2D<Value,Real>& operator=(Value val)
00078     {
00079         x=val;
00080         y=val;
00081         return *this;
00082     }
00083     
00084 
00085     bool operator==(const Vect2D &other) const {return other.x==x && other.y==y;}
00086     bool operator!=(const Vect2D &other) const {return !((*this)==other);}
00087     Vect2D &operator+=(const Vect2D &v){x+=v.x;y+=v.y;return(*this);}
00088     Vect2D &operator-=(const Vect2D &v){x-=v.x;y-=v.y;return(*this);}
00089     Vect2D &operator/=(const Value &d) {x/=d;  y/=d;  return(*this);}
00090     Vect2D &operator*=(const Value &d) {x*=d;  y*=d;  return(*this);}
00091     Vect2D &operator-(){x=-x;y=-y;return(*this);}
00092 
00093 
00094 };
00095 
00096 
00097 
00098 // Repere2D
00099 //  template<class Value,class Real>
00100 //  struct Repere2D
00101 //  {
00102 //      Vect2D<Value,Real> i,j;
00103 //      Repere2D(Value ix,Value iy,
00104 //               Value jx,Value jy );
00105 //      Repere2D(){;}
00106 //  };
00107 
00108 
00109 //  extern Repere2D BONDd0;
00110 //  extern Vect2D Vect2D_NUL;
00111 
00112 
00113 //  template<class Value,class Real>
00114 //  inline
00115 //  Repere2D<Value,Real>::Repere2D(Value ix,Value iy,
00116 //                                 Value jx,Value jy )
00117 //  {
00118 //      i.x=ix;i.y=iy;
00119 //      j.x=jx;j.y=jy;
00120 //  }
00121 
00122 template<class Value,class Real>
00123 inline
00124 Real
00125 Vect2D<Value,Real>::Unit()
00126 {
00127     Value n;
00128     n=Norme();
00129     if (n==0) { ThrowError("Vect2D::Unit: UNIT FAILED");}
00130     x/=n;
00131     y/=n;
00132     return(n);
00133 }
00134 
00135 
00136 template<class Value,class Real>
00137 inline Vect2D<Value,Real>
00138 operator+ (const Vect2D<Value,Real>& v1,const Vect2D<Value,Real>& v2)
00139 {
00140     Vect2D<Value,Real> opres;
00141     opres.x=v1.x+v2.x;
00142     opres.y=v1.y+v2.y;
00143     return opres;
00144 }
00145 
00146 template<class Value,class Real>
00147 inline Vect2D<Value,Real>
00148 operator- (const Vect2D<Value,Real>& v1,const Vect2D<Value,Real>& v2)
00149 {
00150     Vect2D<Value,Real> opres;
00151     opres.x=v1.x-v2.x;
00152     opres.y=v1.y-v2.y;
00153     return opres;
00154 }
00155 
00156 template<class Value,class Real>
00157 inline Value 
00158 operator* (const Vect2D<Value,Real>& v1,const Vect2D<Value,Real>& v2)
00159 {
00160     return(v1.x*v2.x+v1.y*v2.y);
00161 }
00162 
00163 template<class Value,class Real>
00164 inline Vect2D<Value,Real>
00165 operator/(const Vect2D<Value,Real>& v1,Value l)
00166 {
00167     Vect2D<Value,Real> opres;
00168     opres.x=v1.x/l;
00169     opres.y=v1.y/l;
00170     return opres;
00171 }
00172 
00173 
00174 template<class Value,class Real>
00175 inline Vect2D<Value,Real>
00176 operator* (float l,const Vect2D<Value,Real>& v1)
00177 {
00178     Vect2D<Value,Real> opres;
00179     opres.x=v1.x*l;
00180     opres.y=v1.y*l;
00181     return opres;
00182 }
00183 template<class Value,class Real>
00184 inline Vect2D<Value,Real>
00185 operator* (double l,const Vect2D<Value,Real>& v1)
00186 {
00187     Vect2D<Value,Real> opres;
00188     opres.x=v1.x*l;
00189     opres.y=v1.y*l;
00190     return opres;
00191 }
00192 
00193 template<class Value,class Real>
00194 inline Vect2D<Value,Real>
00195 operator* (int l,const Vect2D<Value,Real>& v1)
00196 {
00197     Vect2D<Value,Real> opres;
00198     opres.x=v1.x*l;
00199     opres.y=v1.y*l;
00200     return opres;
00201 }
00202 
00203 
00204 typedef Vect2D<float,float>   Vect2Df;
00205 typedef Vect2D<double,double> Vect2Dd;
00206 typedef Vect2D<int,float>     Vect2Di;
00207 
00209 template<class T> inline bool IsVect2Df()          {return false;}
00210 template<>        inline bool IsVect2Df<Vect2Df>() {return true ;}
00212 template<>  inline string TypeName<Vect2Df>(){return "Vect2Df";}
00213 
00214 #endif // Vect2D_H
00215 

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