00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _VECTOR2_
00019 #define _VECTOR2_
00020
00021 #include <cmath>
00022 #include <iostream>
00023
00024 class Vector2
00025 {
00026 public:
00027 float _x, _y, _unused;
00028
00029 Vector2()
00030 :_x(0), _y(0), _unused(0)
00031 {
00032 }
00033
00034 Vector2(float x, float y)
00035 :_x(x), _y(y), _unused(0)
00036 {
00037 }
00038
00039 Vector2(const Vector2& v)
00040 :_x(v._x), _y(v._y), _unused(0)
00041 {
00042 }
00043
00044 Vector2 operator+(const Vector2& v) const
00045 {
00046 Vector2 temp;
00047
00048 temp._x = _x + v._x;
00049 temp._y = _y + v._y;
00050
00051 return temp;
00052 }
00053
00054 Vector2 operator+(const float fTranslation) const
00055 {
00056 Vector2 temp;
00057
00058 temp._x += fTranslation;
00059 temp._y += fTranslation;
00060
00061 return temp;
00062 }
00063
00064 Vector2 operator-(const Vector2& v) const
00065 {
00066 Vector2 temp;
00067
00068 temp._x = _x - v._x;
00069 temp._y = _y - v._y;
00070
00071 return temp;
00072 }
00073
00074 Vector2 operator-(const float fTranslation) const
00075 {
00076 Vector2 temp;
00077
00078 temp._x -= fTranslation;
00079 temp._y -= fTranslation;
00080
00081 return temp;
00082 }
00083
00084 Vector2 operator*(const float fScale) const
00085 {
00086 Vector2 temp;
00087
00088 temp._x *= fScale;
00089 temp._y *= fScale;
00090
00091 return temp;
00092 }
00093
00094 Vector2 operator-() const
00095 {
00096 Vector2 temp;
00097
00098 temp._x = -_x;
00099 temp._y = -_y;
00100
00101 return temp;
00102 }
00103
00104 Vector2 Unit() const
00105 {
00106 Vector2 temp(*this);
00107
00108 temp.Normalize();
00109
00110 return temp;
00111 }
00112
00113 Vector2 & operator+=(const Vector2& v)
00114 {
00115 _x += v._x;
00116 _y += v._y;
00117
00118 return *this;
00119 }
00120
00121 Vector2 & operator+=(const float fTranslation)
00122 {
00123 _x += fTranslation;
00124 _y += fTranslation;
00125
00126 return *this;
00127 }
00128
00129 Vector2 & operator-=(const Vector2& v)
00130 {
00131 _x -= v._x;
00132 _y -= v._y;
00133
00134 return *this;
00135 }
00136
00137 Vector2 & operator-=(const float fTranslation)
00138 {
00139 _x -= fTranslation;
00140 _y -= fTranslation;
00141
00142 return *this;
00143 }
00144
00145 Vector2 & operator*=(const float fScale)
00146 {
00147 _x *= fScale;
00148 _y *= fScale;
00149
00150 return *this;
00151 }
00152
00153 Vector2 & operator=(const Vector2& v)
00154 {
00155 _x = v._x;
00156 _y = v._y;
00157
00158 return *this;
00159 }
00160
00161 Vector2 & SetX(const float x)
00162 {
00163 _x = x;
00164
00165 return *this;
00166 }
00167
00168 Vector2 & SetY(const float y)
00169 {
00170 _y = y;
00171
00172 return *this;
00173 }
00174
00175 Vector2 & Set(const float x, const float y)
00176 {
00177 _x = x;
00178 _y = y;
00179
00180 return *this;
00181 }
00182
00183 Vector2 & Normalize()
00184 {
00185 float fLength = Length();
00186
00187 _x /= fLength;
00188 _y /= fLength;
00189
00190 return *this;
00191 }
00192
00193 Vector2 & Reverse()
00194 {
00195 _x = -_x;
00196 _y = -_y;
00197
00198 return *this;
00199 }
00200
00201 float CrossZ(const Vector2& other) const
00202 {
00203 return _x * other._y + _y * other._x;
00204 }
00205
00206 float Length() const
00207 {
00208 return std::sqrt(_x * _x + _y * _y);
00209 }
00210
00211 float Dot(const Vector2& other) const
00212 {
00213 return _x * other._x + _y * other._y;
00214 }
00215 };
00216
00217 static inline std::ostream& operator<<(std::ostream& os, const Vector2& v)
00218 {
00219 os << v._x << " " << v._y;
00220
00221 return os;
00222 }
00223
00224 static inline std::istream& operator>>(std::istream& is, Vector2& v)
00225 {
00226 is >> v._x >> v._y;
00227
00228 return is;
00229 }
00230
00231 #endif