00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
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