00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __TVECTOR2_H__
00013 #define __TVECTOR2_H__
00014
00015
00016
00017
00018 #include <Definitions.h>
00019 #include <math.h>
00020
00021
00022
00023
00024
00025
00026
00027
00028 template<typename T>
00029 struct TVector2
00030 {
00031
00032
00033
00034 public:
00035 TVector2() {}
00036 TVector2(const T &x, const T &y) {m_X = x; m_Y = y;}
00037 TVector2(const TVector2<T> &other) {m_X = other.m_X; m_Y = other.m_Y;}
00038
00039 const TVector2<T> &operator=(const TVector2<T> &other) {m_X = other.m_X; m_Y = other.m_Y; return *this;}
00040
00041 const TVector2<T> operator+(const TVector2<T> &other) const {return TVector2<T>(m_X + other.m_X, m_Y + other.m_Y);}
00042 const TVector2<T> &operator+=(const TVector2<T> &other) {m_X += other.m_X; m_Y += other.m_Y; return *this;}
00043
00044 const TVector2<T> operator-(const TVector2<T> &other) const {return TVector2<T>(m_X - other.m_X, m_Y - other.m_Y);}
00045 const TVector2<T> &operator-=(const TVector2<T> &other) {m_X -= other.m_X; m_Y -= other.m_Y; return *this;}
00046
00047 const TVector2<T> operator*(const T &factor) const {return TVector2<T>(m_X * factor, m_Y * factor);}
00048 const TVector2<T> &operator*=(const T &factor) {m_X *= factor; m_Y *= factor; return *this;}
00049
00050 const TVector2<T> operator/(const T ") const {return TVector2<T>(m_X / quot, m_Y / quot);}
00051 const TVector2<T> &operator/=(const T ") {m_X /= quot; m_Y /= quot; return *this;}
00052
00053 bool operator==(const TVector2<T> &other) {return m_X == other.m_X && m_Y == other.m_Y;}
00054 bool operator!=(const TVector2<T> &other) {return m_X != other.m_X || m_Y != other.m_Y;}
00055
00056 T &operator[](const byte &i) {assert(i < 2 && "Error: Invalid index for TVector2::operator[]!");
00057 return *(&m_X + i);}
00058
00059 const T &operator[](const byte &i) const {assert(i < 2 && "Error: Invalid index for TVector2::operator[]!");
00060 return *(&m_X + i);}
00061
00062 void Set(const T &x, const T &y) {m_X = x; m_Y = y;}
00063 void SetRPhi(const T &r, const T &phi) {m_X = 0; m_Y = r;}
00064
00065 const double Length() const {return sqrt(m_X * m_X + m_Y * m_Y);}
00066 const T LengthSqr() const {return m_X * m_X + m_Y * m_Y;}
00067
00068 void Normalize() {assert(LengthSqr() && "Error: TVector2::Normalize called for a zero vector!");
00069 float fL = 1 / Length(); m_X *= fL; m_Y *= fL;}
00070
00071 const T Dot(const TVector2<T> &other) const {return m_X * other.m_X + m_Y * other.m_Y}
00072
00073 void Log(word wTargetLog) const {LOG(wTargetLog, "(%.2f, %.2f)", m_X, m_Y);}
00074
00075
00076
00077
00078 public:
00079 T m_X, m_Y;
00080 };
00081
00082 typedef TVector2<float> CVector2f;
00083 typedef TVector2<double> CVector2d;
00084 typedef TVector2<sword> CVector2sw;
00085 typedef TVector2<sdword> CVector2sdw;
00086
00087 #endif