Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
Vector2.h
Go to the documentation of this file.
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2011-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #pragma once
22 
23 /** @file
24  * @brief A two-dimensional floating point vector and associated math
25  * functions.
26  */
27 
28 #include "debug/CrtDbgInc.h"
29 
30 #include "HFDataTypes.h"
31 #include <math.h>
32 
33 namespace HFCore
34 {
35  /**
36  * @brief A two-dimensional floating point vector and associated math
37  * functions.
38  *
39  * Can be used either as a traditional vector (indicating a direction) or
40  * a position (treating x and y as coordinates).
41  */
42  class Vector2
43  {
44  public:
45  /**
46  * @brief Constructor.
47  *
48  * @param x X value.
49  * @param y Y value.
50  */
51  Vector2(fp32_hf x = 0, fp32_hf y = 0) { this->x = x; this->y = y; }
52 
53  /**
54  * @brief Constructor.
55  *
56  * @param vec Vector2 to initialize with.
57  */
58  Vector2(const Vector2 &vec) { x = vec.x; y = vec.y; }
59 
60  /**
61  * @brief Destructor.
62  */
63  virtual ~Vector2() {}
64 
65  /**
66  * @brief Set x and y values.
67  *
68  * @param _x X value.
69  * @param _y Y value.
70  */
71  void set(fp32_hf _x, fp32_hf _y) { x = _x; y = _y; }
72 
73  /**
74  * @brief Overload of binary operator +.
75  *
76  * @param v Right operand (a Vector2).
77  *
78  * @return Memberwise addition of Vector2 v.
79  */
80  Vector2 operator + (const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
81 
82  /**
83  * @brief Overload of binary operator -.
84  *
85  * @param v Right operand (a Vector2).
86  *
87  * @return Memberwise subtraction of Vector2 v.
88  */
89  Vector2 operator - (const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
90 
91  /**
92  * @brief Overload of unary operator -.
93  *
94  * @return Memberwise opposite of the vector.
95  */
96  Vector2 operator - () const { return Vector2(-x, -y); }
97 
98  /**
99  * @brief Overload of binary operator *.
100  *
101  * @param t Right operand (a fp32_hf).
102  *
103  * @return Memberwise multiplication by fp32_hf t.
104  */
105  Vector2 operator * (fp32_hf t) const { return Vector2(t * x, t * y); }
106 
107  /**
108  * @brief Overload of binary operator /.
109  *
110  * @param t Right operand (a fp32_hf).
111  *
112  * @return Memberwise division by fp32_hf t.
113  */
114  Vector2 operator / (fp32_hf t) const { return Vector2(x / t, y / t); }
115 
116  /**
117  * @brief Overload of binary operator +=.
118  *
119  * This operator performs a memberwise addition of Vector2 v.
120  *
121  * @param v Right operand (a Vector2).
122  */
123  void operator += (const Vector2 &v) { x += v.x; y += v.y; }
124 
125  /**
126  * @brief Overload of binary operator -=.
127  *
128  * This operator performs a memberwise subtraction of Vector2 v.
129  *
130  * @param v Right operand (a Vector2).
131  */
132  void operator -= (const Vector2 &v) { x -= v.x; y -= v.y; }
133 
134  /**
135  * @brief Overload of binary operator *=.
136  *
137  * This operator performs a memberwise multiplication of
138  * fp32_hf t.
139  *
140  * @param t Right operand (a fp32_hf).
141  */
142  void operator *= (fp32_hf t) { x *= t; y *= t; }
143 
144  /**
145  * @brief Overload of binary operator /=.
146  *
147  * This operator performs a memberwise division of fp32_hf t.
148  *
149  * @param t Right operand (a fp32_hf).
150  */
151  void operator /= (fp32_hf t) { x /= t; y /= t; }
152 
153  /**
154  * @brief Overload of binary operator =.
155  *
156  * This operator assigns the value of Vector2 v.
157  *
158  * @param v Right operand (a Vector2).
159  */
160  void operator = (const Vector2 &v) { x = v.x; y = v.y; }
161 
162  /**
163  * @brief Overload of binary operator ==.
164  *
165  * This operator compares strict equality between two vectors.
166  *
167  * @param v Right operand (a Vector2).
168  *
169  * @return true if equal, else false.
170  */
171  bool operator == (const Vector2 &v) { return x == v.x && y == v.y; }
172 
173  /**
174  * @brief Overload of binary operator !=.
175  *
176  * This operator compares strict difference between two
177  * vectors.
178  *
179  * @param v Right operand (a Vector2).
180  *
181  * @return true if not equal, else false.
182  */
183  bool operator != (const Vector2 &v) { return x != v.x || y != v.y; }
184 
185  /**
186  * @brief Get the dot product of two vectors.
187  *
188  * @param v Right operand (a Vector2).
189  * @return The dot product.
190  */
191  fp32_hf dot(const Vector2 &v) const { return x * v.x + y * v.y; }
192 
193  /**
194  * @brief Get the cross product of two vectors.
195  *
196  * Note that the \b mathematical definition of a cross product
197  * results in another vector perpendicular to the two inputs, but
198  * since both of our vectors are 2D, the returned vector will
199  * always have X and Y components of 0. Thus this function returns
200  * what would be the Z component of that vector.
201  *
202  * @param v Right operand (a Vector2).
203  * @return The Z component of the cross product.
204  */
205  fp32_hf cross(const Vector2 &v) const { return x * v.y - y * v.x; }
206 
207  /**
208  * @brief Get the absolute magnitude of the vector.
209  *
210  * Uses a square-root, so be careful calling this too much within
211  * a loop.
212  *
213  * @return The length (magnitude) of the vector.
214  */
215  fp32_hf magnitude() const { return sqrtf(x * x + y * y); }
216 
217  /**
218  * @brief Get the squared magnitude of the vector.
219  *
220  * It's a lot faster to consider the squared lengths.
221  *
222  * @return The length (squared magnitude) of the vector.
223  */
224  fp32_hf magnitudeSquared() const { return x * x + y * y; }
225 
226  /**
227  * @brief Get the absolute distance between two points.
228  *
229  * (most useful if the Vector2 represents a position). Uses a
230  * square-root, so be careful calling this too much within a
231  * loop.
232  *
233  * @param v Right operand (a Vector2).
234  * @return The distance between the two points.
235  */
236  fp32_hf distance(const Vector2 &v) const { return Vector2(*this - v).magnitude(); }
237 
238  /**
239  * @brief Get the absolute squared distance between two points.
240  *
241  * It's a lot faster to consider the squared distances.
242  *
243  * @param v Right operand (a Vector2).
244  * @return The distance between the two points squared.
245  */
246  fp32_hf distanceSquared(const Vector2 &v) const { return Vector2(*this - v).magnitudeSquared(); }
247 
248  /**
249  * @brief reflects a point t off a line l that passes through the
250  * two points l0 and l1.
251  *
252  * @param l0 Point 0.
253  * @param l1 Point 1.
254  * @return The new vector.
255  */
256  Vector2 reflect(const Vector2 &l0, const Vector2 &l1) const
257  {
258  // calculate the normal along the line:
259  Vector2 n = (l1 - l0);
260  n /= n.magnitude();
261 
262  return l0 * 2 - *this + n * 2 * (*this - l0).dot(n);
263  }
264 
265  /**
266  * @brief Normalizes a vector and ensures that its magnitude is
267  * equal to 1.0.
268  *
269  * @return The normalized vector.
270  */
271  Vector2 normalize() const { fp32_hf mag = magnitude(); return mag != 0 ? (*this) / mag : *this; }
272 
273  /**
274  * @brief Retrieve perpendicular vector.
275  *
276  * @return Perpendicular Vector2.
277  */
278  Vector2 perp() const { return Vector2(-y, x); }
279 
280  public:
281  /**
282  * @brief X coordinate of the vector.
283  */
285 
286  /**
287  * @brief Y coordinate of the vector.
288  */
290  };
291 }
fp32_hf x
X coordinate of the vector.
Definition: Vector2.h:284
void operator*=(fp32_hf t)
Overload of binary operator *=.
Definition: Vector2.h:142
fp32_hf dot(const Vector2 &v) const
Get the dot product of two vectors.
Definition: Vector2.h:191
Vector2 perp() const
Retrieve perpendicular vector.
Definition: Vector2.h:278
Vector2 operator/(fp32_hf t) const
Overload of binary operator /.
Definition: Vector2.h:114
fp32_hf cross(const Vector2 &v) const
Get the cross product of two vectors.
Definition: Vector2.h:205
void operator=(const Vector2 &v)
Overload of binary operator =.
Definition: Vector2.h:160
Vector2 operator+(const Vector2 &v) const
Overload of binary operator +.
Definition: Vector2.h:80
bool operator==(const Vector2 &v)
Overload of binary operator ==.
Definition: Vector2.h:171
Vector2(const Vector2 &vec)
Constructor.
Definition: Vector2.h:58
Framework data types, defines, typedef&#39;s, enum and structs.
fp32_hf magnitudeSquared() const
Get the squared magnitude of the vector.
Definition: Vector2.h:224
void operator+=(const Vector2 &v)
Overload of binary operator +=.
Definition: Vector2.h:123
fp32_hf distanceSquared(const Vector2 &v) const
Get the absolute squared distance between two points.
Definition: Vector2.h:246
void operator-=(const Vector2 &v)
Overload of binary operator -=.
Definition: Vector2.h:132
Vector2 normalize() const
Normalizes a vector and ensures that its magnitude is equal to 1.0.
Definition: Vector2.h:271
Vector2 operator*(fp32_hf t) const
Overload of binary operator *.
Definition: Vector2.h:105
A two-dimensional floating point vector and associated math functions.
Definition: Vector2.h:42
virtual ~Vector2()
Destructor.
Definition: Vector2.h:63
bool operator!=(const Vector2 &v)
Overload of binary operator !=.
Definition: Vector2.h:183
Vector2(fp32_hf x=0, fp32_hf y=0)
Constructor.
Definition: Vector2.h:51
void operator/=(fp32_hf t)
Overload of binary operator /=.
Definition: Vector2.h:151
fp32_hf y
Y coordinate of the vector.
Definition: Vector2.h:289
fp32_hf distance(const Vector2 &v) const
Get the absolute distance between two points.
Definition: Vector2.h:236
Vector2 reflect(const Vector2 &l0, const Vector2 &l1) const
reflects a point t off a line l that passes through the two points l0 and l1.
Definition: Vector2.h:256
Definition: Actor.h:34
fp32_hf magnitude() const
Get the absolute magnitude of the vector.
Definition: Vector2.h:215
Vector2 operator-() const
Overload of unary operator -.
Definition: Vector2.h:96
float fp32_hf
A type definition for float.
Definition: HFDataTypes.h:359