00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _LIB_NUMERICS_2D_VECTOR2_H_
00018 #define _LIB_NUMERICS_2D_VECTOR2_H_ 1
00019
00029 #include <lib/numerics/num_math.h>
00030
00031 namespace NUM
00032 {
00033
00034
00035 template<typename T>class OMatrix;
00036
00037
00049 template<typename T=dbl>class Vector2
00050 {
00051 public:
00054 enum { X=0, Y=1 };
00055
00057 static const Vector2<T> UX,UY,Null;
00058
00059 private:
00061 T v[2];
00062
00063 public:
00065 inline Vector2() {}
00067 inline Vector2(T x,T y)
00068 { v[0]=x; v[1]=y; }
00070 inline Vector2(const Vector2 &a)
00071 { v[0]=a.v[0]; v[1]=a.v[1]; }
00073 inline Vector2(const T *a)
00074 { v[0]=a[0]; v[1]=a[1]; }
00075 inline ~Vector2() {}
00076
00079 inline T &operator[](int i) { return(v[i]); }
00080 inline const T &operator[](int i) const { return(v[i]); }
00081
00083 inline operator T *() { return(v); }
00084 inline operator const T *() const { return(v); }
00085
00087 inline int dim() const
00088 { return(2); }
00089
00091 inline Vector2 &operator=(const Vector2 &a)
00092 { v[0]=a.v[0]; v[1]=a.v[1]; return(*this); }
00093
00095 inline Vector2 &promote(T a)
00096 { v[0]=a; v[1]=a; return(*this); }
00097
00099 inline T len2() const
00100 { return(v[0]*v[0] + v[1]*v[1]); }
00102 inline T len() const
00103 { return(hypot(v[0],v[1])); }
00104
00106 inline T norm1() const
00107 { return(fabs(v[0])+fabs(v[1])); }
00108
00110 inline T max() const
00111 { return(v[0]>v[1] ? v[0] : v[1]); }
00113 inline T min() const
00114 { return(v[0]<v[1] ? v[0] : v[1]); }
00115
00117 inline Vector2 &operator+=(const Vector2 &a)
00118 { v[0]+=a.v[0]; v[1]+=a.v[1]; return(*this); }
00120 inline Vector2 &operator-=(const Vector2 &a)
00121 { v[0]-=a.v[0]; v[1]-=a.v[1]; return(*this); }
00122
00124 inline Vector2 &AddScaled(const Vector2 &a,T f)
00125 { v[0]+=a.v[0]*f; v[1]+=a.v[1]*f; return(*this); }
00126
00128 inline Vector2 &operator*=(T a)
00129 { v[0]*=a; v[1]*=a; return(*this); }
00132 inline Vector2 &operator/=(T a)
00133 { v[0]/=a; v[1]/=a; return(*this); }
00134
00136 inline T normalize()
00137 { T l=len(),f=1.0/l; v[0]*=f; v[1]*=f; return(l); }
00139 inline T normalize(T wl)
00140 { T l=len(),f=wl/l; v[0]*=f; v[1]*=f; return(l); }
00141
00143 inline Vector2 &neg()
00144 { v[0]=-v[0]; v[1]=-v[1]; return(*this); }
00145
00148 inline bool IsNull(T epsilon) const
00149 { return(fabs(v[0])<epsilon && fabs(v[1])<epsilon); }
00150 };
00151
00153 template<typename T>inline Vector2<T> operator+(
00154 const Vector2<T> &a,const Vector2<T> &b)
00155 { return(Vector2<T>(a[0]+b[0],a[1]+b[1])); }
00157 template<typename T>inline Vector2<T> operator-(
00158 const Vector2<T> &a,const Vector2<T> &b)
00159 { return(Vector2<T>(a[0]-b[0],a[1]-b[1])); }
00160
00162 template<typename T>inline Vector2<T> operator*(const Vector2<T> &a,T b)
00163 { return(Vector2<T>(a[0]*b,a[1]*b)); }
00164 template<typename T>inline Vector2<T> operator*(T b,const Vector2<T> &a)
00165 { return(Vector2<T>(b*a[0],b*a[1])); }
00166
00169 template<typename T>inline Vector2<T> operator/(const Vector2<T> &a,T b)
00170 { return(Vector2<T>(a[0]/b,a[1]/b)); }
00171
00173 template<typename T>inline T len2(const Vector2<T> &v)
00174 { return(v[0]*v[0] + v[1]*v[1]); }
00176 template<typename T>inline T len(const Vector2<T> &v)
00177 { return(hypot(v[0],v[1])); }
00178
00180 template<typename T>inline T dist2(const Vector2<T> &a,const Vector2<T> &b)
00181 { return(sqr(a[0]-b[0])+sqr(a[1]-b[1])); }
00183 template<typename T>inline T dist(const Vector2<T> &a,const Vector2<T> &b)
00184 { return(hypot(a[0]-b[0],a[1]-b[1])); }
00185
00187 template<typename T>inline Vector2<T> normalize(const Vector2<T> &v)
00188 { T f=1.0/v.len(); return(Vector2<T>(v[0]*f,v[1]*f)); }
00190 template<typename T>inline Vector2<T> normalize(const Vector2<T> &v,T l)
00191 { T f=l/v.len(); return(Vector2<T>(v[0]*f,v[1]*f)); }
00192
00194 template<typename T>inline T dot(const Vector2<T> &a,const Vector2<T> &b)
00195 { return(a[0]*b[0] + a[1]*b[1]); }
00196
00198 template<typename T>inline T angle(const Vector2<T> &a,const Vector2<T> &b)
00199 { return(acos(dot(a,b)/sqrt(a.len2()*b.len2()))); }
00200
00203 template<typename T>inline bool equal(const Vector2<T> &a,const Vector2<T> &b,
00204 T epsilon)
00205 { return(fabs(a[0]-b[0])<epsilon && fabs(a[1]-b[1])<epsilon); }
00206
00208 template<typename T>inline Vector2<T> LinComb(
00209 const Vector2<T> &va,T ca,const Vector2<T> &vb)
00210 { return(Vector2<T>( ca*va[0] + vb[0], ca*va[1] + vb[1] )); }
00211
00213 template<typename T>inline Vector2<T> LinComb(
00214 const Vector2<T> &va,T ca,const Vector2<T> &vb,T cb)
00215 { return(Vector2<T>( ca*va[0] + cb*vb[0], ca*va[1] + cb*vb[1] )); }
00216
00218 template<typename T>inline Vector2<T> LinComb(
00219 const Vector2<T> &va,T ca,const Vector2<T> &vb,T cb,const Vector2<T> &vc)
00220 { return(Vector2<T>(
00221 ca*va[0] + cb*vb[0] + vc[0],
00222 ca*va[1] + cb*vb[1] + vc[1] )); }
00223
00225 template<typename T>inline Vector2<T> LinComb(
00226 const Vector2<T> &va,T ca,const Vector2<T> &vb,T cb,
00227 const Vector2<T> &vc,T cc)
00228 { return(Vector2<T>(
00229 ca*va[0] + cb*vb[0] + cc*vc[0],
00230 ca*va[1] + cb*vb[1] + cc*vc[1] )); }
00231
00233 template<typename T>inline Vector2<T> average(
00234 const Vector2<T> &a,const Vector2<T> &b)
00235 { return(Vector2<T>(0.5*(a[0]+b[0]),0.5*(a[1]+b[1]))); }
00236
00237 }
00238
00239 #endif