00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef KEOS_VECTOR2_H
00023 #define KEOS_VECTOR2_H
00024
00025 #include "KeosPrerequisites.h"
00026
00027 namespace Keos
00028 {
00029
00033 template <class T>
00034 class CVector2
00035 {
00036 public :
00037
00040 CVector2(T X = 0, T Y = 0);
00041
00044 void Set(T X, T Y);
00045
00048 T Length() const;
00049
00052 T LengthSq() const;
00053
00056 void Normalize();
00057
00058
00059 CVector2<T> operator +() const;
00060 CVector2<T> operator -() const;
00061
00062 CVector2<T> operator +(const CVector2<T>& v) const;
00063 CVector2<T> operator -(const CVector2<T>& v) const;
00064
00065 const CVector2<T>& operator +=(const CVector2<T>& v);
00066 const CVector2<T>& operator -=(const CVector2<T>& v);
00067
00068 CVector2<T> operator *(T t) const;
00069 CVector2<T> operator /(T t) const;
00070
00071 const CVector2<T>& operator *=(T t);
00072 const CVector2<T>& operator /=(T t);
00073
00074 bool operator ==(const CVector2<T>& v) const;
00075 bool operator !=(const CVector2<T>& v) const;
00076
00080 operator T*();
00081
00082
00083 T x, y;
00084 };
00085
00086
00087 template <class T> CVector2<T> operator * (const CVector2<T>& v, T t);
00088 template <class T> CVector2<T> operator / (const CVector2<T>& v, T t);
00089 template <class T> CVector2<T> operator * (T t, const CVector2<T>& v);
00090 template <class T> T VectorDot (const CVector2<T>& v1, const CVector2<T>& v2);
00091 template <class T> CVector2<T> VectorCross(const CVector2<T>& v1, const CVector2<T>& v2);
00092 template <class T> std::istream& operator >>(std::istream& Stream, CVector2<T>& Vector);
00093 template <class T> std::ostream& operator <<(std::ostream& Stream, const CVector2<T>& Vector);
00094
00095
00096 typedef CVector2<int> TVector2I;
00097 typedef CVector2<float> TVector2F;
00098
00099
00100
00101
00102
00103
00104
00105 template <class T>
00106 inline CVector2<T>::CVector2(T X, T Y) :
00107 x(X),
00108 y(Y)
00109 {}
00110
00111
00112 template <class T>
00113 inline void CVector2<T>::Set(T X, T Y)
00114 {
00115 x = X;
00116 y = Y;
00117 }
00118
00119
00120 template <class T>
00121 inline T CVector2<T>::Length() const
00122 {
00123 return std::sqrt(LengthSq());
00124 }
00125
00126
00127 template <class T>
00128 inline T CVector2<T>::LengthSq() const
00129 {
00130 return x * x + y * y + z * z;
00131 }
00132
00133
00134 template <class T>
00135 inline void CVector2<T>::Normalize()
00136 {
00137 T Norm = Length();
00138
00139 if (std::abs(Norm) > std::numeric_limits<T>::epsilon())
00140 {
00141 x /= Norm;
00142 y /= Norm;
00143 }
00144 }
00145
00146
00147 template <class T>
00148 inline CVector2<T> CVector2<T>::operator +() const
00149 {
00150 return this;
00151 }
00152
00153
00154 template <class T>
00155 inline CVector2<T> CVector2<T>::operator -() const
00156 {
00157 return CVector2<T>(-x, -y);
00158 }
00159
00160
00161 template <class T>
00162 inline CVector2<T> CVector2<T>::operator +(const CVector2<T>& v) const
00163 {
00164 return CVector2<T>(x + v.x, y + v.y);
00165 }
00166
00167
00168 template <class T>
00169 inline CVector2<T> CVector2<T>::operator -(const CVector2<T>& v) const
00170 {
00171 return CVector2<T>(x - v.x, y - v.y);
00172 }
00173
00174
00175 template <class T>
00176 inline const CVector2<T>& CVector2<T>::operator +=(const CVector2<T>& v)
00177 {
00178 x += v.x;
00179 y += v.y;
00180
00181 return *this;
00182 }
00183
00184
00185 template <class T>
00186 inline const CVector2<T>& CVector2<T>::operator -=(const CVector2<T>& v)
00187 {
00188 x -= v.x;
00189 y -= v.y;
00190
00191 return *this;
00192 }
00193
00194
00195 template <class T>
00196 inline CVector2<T> CVector2<T>::operator *(T t) const
00197 {
00198 return CVector2<T>(x * t, y * t);
00199 }
00200
00201
00202 template <class T>
00203 inline CVector2<T> CVector2<T>::operator /(T t) const
00204 {
00205 return CVector2<T>(x / t, y / t);
00206 }
00207
00208
00209 template <class T>
00210 inline const CVector2<T>& CVector2<T>::operator *=(T t)
00211 {
00212 x *= t;
00213 y *= t;
00214
00215 return *this;
00216 }
00217
00218
00219 template <class T>
00220 inline const CVector2<T>& CVector2<T>::operator /=(T t)
00221 {
00222 x /= t;
00223 y /= t;
00224
00225 return *this;
00226 }
00227
00228
00229 template <class T>
00230 inline bool CVector2<T>::operator ==(const CVector2<T>& v) const
00231 {
00232 return ((std::abs(x - v.x) <= std::numeric_limits<T>::epsilon()) &&
00233 (std::abs(y - v.y) <= std::numeric_limits<T>::epsilon()));
00234 }
00235
00236
00237 template <class T>
00238 inline bool CVector2<T>::operator !=(const CVector2<T>& v) const
00239 {
00240 return !(*this == v);
00241 }
00242
00243
00244 template <class T>
00245 inline CVector2<T>::operator T*()
00246 {
00247 return &x;
00248 }
00249
00250
00251 template <class T>
00252 inline CVector2<T> operator *(const CVector2<T>& v, T t)
00253 {
00254 return CVector2<T>(v.x * t, v.y * t);
00255 }
00256
00257
00258 template <class T>
00259 inline CVector2<T> operator /(const CVector2<T>& v, T t)
00260 {
00261 return CVector2<T>(v.x / t, v.y / t);
00262 }
00263
00264
00265 template <class T>
00266 inline CVector2<T> operator *(T t, const CVector2<T>& v)
00267 {
00268 return v * t;
00269 }
00270
00271
00272 template <class T>
00273 inline T VectorDot(const CVector2<T>& v1, const CVector2<T>& v2)
00274 {
00275 return v1.x * v2.x + v1.y * v2.y;
00276 }
00277
00278
00279 template <class T>
00280 inline CVector2<T> VectorCross(const CVector2<T>& v1, const CVector2<T>& v2)
00281 {
00282 return CVector2<T>();
00283 }
00284
00285
00286 template <class T>
00287 inline std::istream& operator >>(std::istream& Stream, CVector2<T>& Vector)
00288 {
00289 return Stream >> Vector.x >> Vector.y;
00290 }
00291
00292
00293 template <class T>
00294 inline std::ostream& operator <<(std::ostream& Stream, const CVector2<T>& Vector)
00295 {
00296 return Stream << Vector.x << " " << Vector.y;
00297 }
00298
00299
00300 }
00301
00302
00303 #endif // KEOS_VECTOR2_H