-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVector2.h
More file actions
215 lines (188 loc) · 7.34 KB
/
Vector2.h
File metadata and controls
215 lines (188 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#pragma once
///A two-dimensional floating point vector and associated math functions
/**
* A floating point two-dimensional vector. Can be used either as a traditional
* vector (indicating a direction) or a position (treating X and Y as
* coordinates).
*/
struct Vector2
{
/**
* The X dimension, publicly available because it's so often gotten or
* set and there's no good reason to encapsulate it.
*/
float X;
/**
* The Y dimension, also publicly available.
*/
float Y;
/**
* A reference to a zero-length vector (0, 0)
*
* NB: We can't make a static member variable constant, so it is
* possible to change this value. That is a horrible idea and will make
* the engine act "odd" at best.
*/
static Vector2 Zero;
/**
* A reference to a (1, 1) vector
*
* NB: We can't make a static member variable constant, so it is
* possible to change this value. That is a horrible idea and will make
* the engine act "odd" at best.
*/
static Vector2 One;
/**
* A reference to a (1, 0) vector
*
* NB: We can't make a static member variable constant, so it is
* possible to change this value. That is a horrible idea and will make
* the engine act "odd" at best.
*/
static Vector2 UnitX;
/**
* A reference to a (0, 1) vector
*
* NB: We can't make a static member variable constant, so it is
* possible to change this value. That is a horrible idea and will make
* the engine act "odd" at best.
*/
static Vector2 UnitY;
/**
* Constructor to initialize the vector to set dimensions
*
* @param x The X dimension
* @param y The Y dimension
*/
Vector2(float x, float y);
/**
* Constructor to initialize the vector to uniform dimensions
*
* @param value The value to use for both the X and Y dimension
*/
Vector2(float value);
/**
* Constructor to initialize a zero-length vector (0, 0)
*/
Vector2();
/**
* Get the absolute magnitude of the vector. Uses a square-root, so be
* careful calling this too much within a loop.
*
* @return The length (magnitude) of the vector
*/
float Length();
/**
* Get the squared magnitude of the vector -- if all you care about is
* comparison, it's a lot faster to consider the squared lengths of
* the vectors.
*
* @return The length (magnitude) of the vector squared
*/
float LengthSquared();
/**
* Get the absolute distance between two points (most useful if the
* Vector2 represents a position). Uses a square-root, so be careful
* calling this too much within a loop.
*
* @param value1 The first point
* @param value2 The second point
* @return The distance between the two points
*/
static float Distance(const Vector2& value1, const Vector2& value2);
/**
* Get the absolute distance between two points -- if all you care about
* is comparison, it's a lot faster to consider the squared lengths of
* the vectors.
*
* @param value1 The first point
* @param value2 The second point
* @return The distance between the two points squared
*/
static float DistanceSquared(const Vector2& value1, const Vector2& value2);
/**
* Get the dot product of two vectors.
*
* @param value1 The first vector
* @param value2 The second vector
* @return The dot product
*/
static float Dot(const Vector2& value1, const Vector2& value2);
/**
* Normalizes a vector in place -- retains its direction, but ensures
* that its magnitude is equal to 1.0.
*/
void Normalize();
/**
* Get the normalized value for a Vector2 without affecting the original.
*
* @param value The Vector2 to normalize
* @return A normalized version of the passed-in Vector2
*/
static Vector2 Normalize(const Vector2& value);
/**
* Reflect one Vector around another
*
* @param vector The vector to reflect
* @param normal The normal to reflect it around
* @return The new vector resulting from the reflection
*/
static Vector2 Reflect(const Vector2& vector, const Vector2& normal);
/**
* Get a new vector from the minimum X and minimum Y of the two
*
* @return The vector composed from minimums on both axes
*/
static Vector2 Min(const Vector2& value1, const Vector2& value2);
/**
* Get a new vector from the maximum X and maximum Y of the two
*
* @return The vector composed from the maximums on both axes
*/
static Vector2 Max(const Vector2& value1, const Vector2& value2);
/**
* Clamp a vector to a given minimum and maximum
*
* @param value The vector to be clamped
* @param min The vector representing the X and Y minimums for the clamping
* @param max The vector representing the X and Y maximums for the clamping
* @return The clamped vector
*/
static Vector2 Clamp(const Vector2& value, const Vector2& min, const Vector2& max);
/**
* Perform a linear interpolation between two vectors
*
* @param value1 The starting point vector
* @param value2 The ending point vector
* @param amount The amount (from 0.0 to 1.0) to interpolate between them
* @return The interpolated vector
*/
static Vector2 Lerp(const Vector2& value1, const Vector2& value2, float amount);
/**
* Get a negated vector -- if you add the result and the original, you
* should get a zero-length vector (0, 0)
*
* @param value The vector to negate
* @return The negated vector
*/
static Vector2 Negate(const Vector2& value);
/**
* Rotate a vector
*
* @param value The original vector to rotate
* @param radians The rotation angle (in radians)
* @return The rotated vector
*/
static Vector2 Rotate(const Vector2& value, const float radians);
bool operator==(const Vector2 &v) const;
bool operator!=(const Vector2 &v) const;
Vector2 operator-() const;
Vector2 operator-(const Vector2 &v) const;
Vector2 operator+(const Vector2 &v) const;
Vector2 operator/(float divider) const;
Vector2 operator*(float scaleFactor) const;
Vector2& operator+=(const Vector2 &v);
Vector2& operator-=(const Vector2 &v);
Vector2& operator*=(float f);
Vector2& operator/=(float f);
};