Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
vector2.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#ifndef TT_INCLUDE__VECTOR2_H
13#define TT_INCLUDE__VECTOR2_H
14#include "wwmath.h"
15
16
17class Vector2i
18{
19
20public:
21 Vector2i(void)
22 {
23 }
24 Vector2i(int i,int j)
25 {
26 I = i; J = j;
27 }
28 void Set(int i, int j)
29 {
30 I = i;
31 J = j;
32 }
33 int I;
34 int J;
35};
36
37
38
39class Vector2 {
40public:
41 union {
42 float X;
43 float U;
44 };
45 union {
46 float Y;
47 float V;
48 };
49 TT_INLINE Vector2(float x, float y)
50 {
51 X = x;
52 Y = y;
53 }
54 TT_INLINE Vector2(const Vector2 &v)
55 {
56 X = v.X;
57 Y = v.Y;
58 }
59 TT_INLINE Vector2()
60 {
61 X = Y = 0.0f;
62 }
63 TT_INLINE explicit Vector2(const float vector[2])
64 {
65 TT_ASSERT(vector != NULL);
66 X = vector[0];
67 Y = vector[1];
68 }
69 TT_INLINE Vector2 &operator= (const Vector2 & v)
70 {
71 X = v[0];
72 Y = v[1];
73 return *this;
74 }
75 TT_INLINE void Set(float x, float y)
76 {
77 X = x;
78 Y = y;
79 }
80 TT_INLINE void Set(const Vector2 & v)
81 {
82 X = v.X;
83 Y = v.Y;
84 }
85 TT_INLINE float &operator[](int i)
86 {
87 return (&X)[i];
88 }
89 TT_INLINE const float &operator[](int i) const
90 {
91 return (&X)[i];
92 }
93 TT_INLINE void Normalize(void)
94 {
95 float len2 = WWMATH_FLOAT_TINY + Length2();
96 float oolen = WWMath::Inv_Sqrt(len2);
97 X *= oolen;
98 Y *= oolen;
99 }
100 TT_INLINE float Length(void) const
101 {
102 return (float)WWMath::Sqrt(Length2());
103 }
104 TT_INLINE float Length2(void) const
105 {
106 return (X * X + Y * Y);
107 }
108 TT_INLINE Vector2 operator-() const
109 {
110 return Vector2(-X,-Y);
111 }
112 TT_INLINE Vector2 operator+() const
113 {
114 return *this;
115 }
116 TT_INLINE Vector2& operator+= (const Vector2 &v)
117 {
118 X += v.X;
119 Y += v.Y;
120 return *this;
121 }
122 TT_INLINE Vector2& operator-= (const Vector2 & v)
123 {
124 X -= v.X;
125 Y -= v.Y;
126 return *this;
127 }
128 TT_INLINE Vector2& operator*= (float k)
129 {
130 X = (float)(X*k);
131 Y = (float)(Y*k);
132 return *this;
133 }
134 TT_INLINE Vector2& operator/= (float k)
135 {
136 k=1.0f/k;
137 X*=k;
138 Y*=k;
139 return *this;
140 }
141 TT_INLINE static float Dot_Product(const Vector2 &a,const Vector2 &b)
142 {
143 return a * b;
144 }
145 TT_INLINE static float Perp_Dot_Product(const Vector2 &a,const Vector2 &b)
146 {
147 return a.X * -b.Y + a.Y * b.X;
148 }
149 TT_INLINE void Rotate(float theta)
150 {
151 Rotate(sin(theta), cos(theta));
152 }
153 TT_INLINE void Rotate(float s, float c)
154 {
155 float new_x = X * c + Y * -s;
156 float new_y = X * s + Y * c;
157 X = new_x;
158 Y = new_y;
159 }
160 TT_INLINE bool Rotate_Towards_Vector(Vector2 &target, float max_theta, bool & positive_turn)
161 {
162 return Rotate_Towards_Vector(target, sin(max_theta), cos(max_theta), positive_turn);
163 }
164 TT_INLINE bool Rotate_Towards_Vector(Vector2 &target, float max_s, float max_c, bool & positive_turn)
165 {
166 positive_turn = Vector2::Perp_Dot_Product(target, *this) > 0.0f;
167 if (Vector2::Dot_Product(*this, target) >= max_c)
168 {
169 Set(target);
170 return true;
171 }
172 else
173 {
174 if (positive_turn)
175 {
176 Rotate(max_s, max_c);
177 }
178 else
179 {
180 Rotate(-max_s, max_c);
181 }
182 }
183 return false;
184 }
185 TT_INLINE void Update_Min(const Vector2 & a)
186 {
187 if (a.X < X)
188 {
189 X = a.X;
190 }
191 if (a.Y < Y)
192 {
193 Y = a.Y;
194 }
195 }
196 TT_INLINE void Update_Max(const Vector2 & a)
197 {
198 if (a.X > X)
199 {
200 X = a.X;
201 }
202 if (a.Y > Y)
203 {
204 Y = a.Y;
205 }
206 }
207 TT_INLINE void Scale(float a, float b)
208 {
209 X *= a;
210 Y *= b;
211 }
212 TT_INLINE void Scale(const Vector2 & a)
213 {
214 X *= a.X;
215 Y *= a.Y;
216 }
217 TT_INLINE void Unscale(const Vector2& a)
218 {
219 X /= a.X;
220 Y /= a.Y;
221 }
222 TT_INLINE static float Distance(const Vector2 &p1, const Vector2 &p2)
223 {
224 Vector2 temp;
225 temp = p1 - p2;
226 return (temp.Length());
227 }
228 TT_INLINE static void Lerp(const Vector2 &a,const Vector2 &b,float t,Vector2 * set_result)
229 {
230 set_result->X = (a.X + (b.X - a.X) * t);
231 set_result->Y = (a.Y + (b.Y - a.Y) * t);
232 }
233 TT_INLINE void Floor()
234 {
235 X = floor(X);
236 Y = floor(Y);
237 };
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);
246
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); }
249};
250TT_INLINE Vector2 operator* (const Vector2 &a,float k)
251{
252 return Vector2(a[0] * k,a[1] * k);
253}
254TT_INLINE Vector2 operator* (float k, const Vector2 &a)
255{
256 return Vector2(a[0] * k,a[1] * k);
257}
258TT_INLINE Vector2 operator/ (const Vector2 &a,float k)
259{
260 return Vector2(a[0] * (1.0f/k),a[1] * (1.0f/k));
261}
262TT_INLINE Vector2 operator+ (const Vector2 &a,const Vector2 &b)
263{
264 return Vector2(a.X + b.X,a.Y + b.Y);
265}
266TT_INLINE Vector2 operator- (const Vector2 &a,const Vector2 &b)
267{
268 return Vector2(a.X - b.X,a.Y - b.Y);
269}
270TT_INLINE float operator* (const Vector2 &a,const Vector2 &b)
271{
272 return a.X * b.X + a.Y * b.Y;
273}
274TT_INLINE bool operator== (const Vector2 &a,const Vector2 &b)
275{
276 return (a.X == b.X) | (a.Y == b.Y);
277}
278TT_INLINE bool operator!= (const Vector2 &a,const Vector2 &b)
279{
280 return (a.X != b.X) | (a.Y != b.Y);
281}
282TT_INLINE Vector2 Normalize(const Vector2 &vec)
283{
284 float len2 = WWMATH_FLOAT_TINY + vec.Length2();
285 return vec * WWMath::Inv_Sqrt(len2);
286}
287TT_INLINE void Swap(Vector2 & a,Vector2 & b)
288{
289 Vector2 tmp(a);
290 a = b;
291 b = tmp;
292}
293TT_INLINE float Distance(float x1, float y1, float x2, float y2)
294{
295 float x_diff = x1 - x2;
296 float y_diff = y1 - y2;
297 return (WWMath::Sqrt((x_diff * x_diff) + (y_diff * y_diff)));
298}
299#endif