12#ifndef TT_INCLUDE__VECTOR2_H
13#define TT_INCLUDE__VECTOR2_H
28 void Set(
int i,
int j)
49 TT_INLINE Vector2(
float x,
float y)
54 TT_INLINE Vector2(
const Vector2 &v)
63 TT_INLINE
explicit Vector2(
const float vector[2])
65 TT_ASSERT(vector != NULL);
69 TT_INLINE Vector2 &operator= (
const Vector2 & v)
75 TT_INLINE
void Set(
float x,
float y)
80 TT_INLINE
void Set(
const Vector2 & v)
85 TT_INLINE
float &operator[](
int i)
89 TT_INLINE
const float &operator[](
int i)
const
93 TT_INLINE
void Normalize(
void)
95 float len2 = WWMATH_FLOAT_TINY + Length2();
96 float oolen = WWMath::Inv_Sqrt(len2);
100 TT_INLINE
float Length(
void)
const
102 return (
float)WWMath::Sqrt(Length2());
104 TT_INLINE
float Length2(
void)
const
106 return (X * X + Y * Y);
108 TT_INLINE Vector2 operator-()
const
110 return Vector2(-X,-Y);
112 TT_INLINE Vector2 operator+()
const
116 TT_INLINE Vector2& operator+= (
const Vector2 &v)
122 TT_INLINE Vector2& operator-= (
const Vector2 & v)
128 TT_INLINE Vector2& operator*= (
float k)
134 TT_INLINE Vector2& operator/= (
float k)
141 TT_INLINE
static float Dot_Product(
const Vector2 &a,
const Vector2 &b)
145 TT_INLINE
static float Perp_Dot_Product(
const Vector2 &a,
const Vector2 &b)
147 return a.X * -b.Y + a.Y * b.X;
149 TT_INLINE
void Rotate(
float theta)
151 Rotate(sin(theta), cos(theta));
153 TT_INLINE
void Rotate(
float s,
float c)
155 float new_x = X * c + Y * -s;
156 float new_y = X * s + Y * c;
160 TT_INLINE
bool Rotate_Towards_Vector(Vector2 &target,
float max_theta,
bool & positive_turn)
162 return Rotate_Towards_Vector(target, sin(max_theta), cos(max_theta), positive_turn);
164 TT_INLINE
bool Rotate_Towards_Vector(Vector2 &target,
float max_s,
float max_c,
bool & positive_turn)
166 positive_turn = Vector2::Perp_Dot_Product(target, *
this) > 0.0f;
167 if (Vector2::Dot_Product(*
this, target) >= max_c)
176 Rotate(max_s, max_c);
180 Rotate(-max_s, max_c);
185 TT_INLINE
void Update_Min(
const Vector2 & a)
196 TT_INLINE
void Update_Max(
const Vector2 & a)
207 TT_INLINE
void Scale(
float a,
float b)
212 TT_INLINE
void Scale(
const Vector2 & a)
217 TT_INLINE
void Unscale(
const Vector2& a)
222 TT_INLINE
static float Distance(
const Vector2 &p1,
const Vector2 &p2)
226 return (temp.Length());
228 TT_INLINE
static void Lerp(
const Vector2 &a,
const Vector2 &b,
float t,Vector2 * set_result)
230 set_result->X = (a.X + (b.X - a.X) * t);
231 set_result->Y = (a.Y + (b.Y - a.Y) * t);
233 TT_INLINE
void Floor()
238 friend Vector2 operator* (
const Vector2 &a,
float k);
239 friend Vector2 operator* (
float k,
const Vector2 &a);
240 friend Vector2 operator/ (
const Vector2 &a,
float k);
241 friend Vector2 operator+ (
const Vector2 &a,
const Vector2 &b);
242 friend Vector2 operator- (
const Vector2 &a,
const Vector2 &b);
243 friend float operator* (
const Vector2 &a,
const Vector2 &b);
244 friend bool operator== (
const Vector2 &a,
const Vector2 &b);
245 friend bool operator!= (
const Vector2 &a,
const Vector2 &b);
247 friend Vector2 operator/(
const Vector2& a,
const Vector2& b) {
return Vector2(a.X / b.X, a.Y / b.Y); }
248 friend Vector2 operator/(
const Vector2& a,
const Vector2i& b) {
return Vector2(a.X / b.I, a.Y / b.J); }
250TT_INLINE Vector2 operator* (
const Vector2 &a,
float k)
252 return Vector2(a[0] * k,a[1] * k);
254TT_INLINE Vector2 operator* (
float k,
const Vector2 &a)
256 return Vector2(a[0] * k,a[1] * k);
258TT_INLINE Vector2 operator/ (
const Vector2 &a,
float k)
260 return Vector2(a[0] * (1.0f/k),a[1] * (1.0f/k));
262TT_INLINE Vector2 operator+ (
const Vector2 &a,
const Vector2 &b)
264 return Vector2(a.X + b.X,a.Y + b.Y);
266TT_INLINE Vector2 operator- (
const Vector2 &a,
const Vector2 &b)
268 return Vector2(a.X - b.X,a.Y - b.Y);
270TT_INLINE
float operator* (
const Vector2 &a,
const Vector2 &b)
272 return a.X * b.X + a.Y * b.Y;
274TT_INLINE
bool operator== (
const Vector2 &a,
const Vector2 &b)
276 return (a.X == b.X) | (a.Y == b.Y);
278TT_INLINE
bool operator!= (
const Vector2 &a,
const Vector2 &b)
280 return (a.X != b.X) | (a.Y != b.Y);
282TT_INLINE Vector2 Normalize(
const Vector2 &vec)
284 float len2 = WWMATH_FLOAT_TINY + vec.Length2();
285 return vec * WWMath::Inv_Sqrt(len2);
287TT_INLINE
void Swap(Vector2 & a,Vector2 & b)
293TT_INLINE
float Distance(
float x1,
float y1,
float x2,
float y2)
295 float x_diff = x1 - x2;
296 float y_diff = y1 - y2;
297 return (WWMath::Sqrt((x_diff * x_diff) + (y_diff * y_diff)));