ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Vector2D.h
Go to the documentation of this file.
1/**
2 * @file Vector2D.h
3 * @brief Defines a 2D vector and various related operations.
4 *
5 * The `Vector2D` class provides a simple 2D vector representation along with
6 * utilities for common vector arithmetic, constraints, and geometric operations.
7 *
8 * @date 22/12/2024
9 * @version 1.0
10 * @author Coela Can't
11 */
12
13#pragma once
14
15#include "Mathematics.h"
16#include "Vector3D.h"
17
18/**
19 * @class Vector2D
20 * @brief Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
21 *
22 * The `Vector2D` class defines basic 2D vector operations such as addition, subtraction,
23 * multiplication, division, dot product, cross product, and geometric queries. It also
24 * includes static functions to perform operations on multiple `Vector2D` objects without
25 * requiring an instance.
26 */
27class Vector2D {
28public:
29 float X; ///< The X-component of the 2D vector.
30 float Y; ///< The Y-component of the 2D vector.
31
32 /**
33 * @brief Constructs a default `Vector2D` with X = 0 and Y = 0.
34 */
35 Vector2D();
36
37 /**
38 * @brief Copy constructor. Initializes this vector with the same values as another `Vector2D`.
39 * @param vector The `Vector2D` to copy from.
40 */
41 Vector2D(const Vector2D& vector);
42
43 /**
44 * @brief Constructs a `Vector2D` using specified float components.
45 * @param X The X-component of the vector.
46 * @param Y The Y-component of the vector.
47 */
48 Vector2D(const float& X, const float& Y);
49
50 /**
51 * @brief Constructs a `Vector2D` from a `Vector3D`, discarding the Z-component.
52 * @param vector The `Vector3D` to convert (Z is ignored).
53 */
54 Vector2D(const Vector3D& vector);
55
56 /**
57 * @brief Returns a vector with the absolute value of each component.
58 * @return A `Vector2D` where each component is `abs(X)` and `abs(Y)`.
59 */
60 Vector2D Absolute() const;
61
62 /**
63 * @brief Computes the squared magnitude of the vector (X^2 + Y^2).
64 * @return A `Vector2D` whose components are squared. (Typically used as an internal helper.)
65 */
66 Vector2D Normal() const;
67
68 /**
69 * @brief Adds this vector to another `Vector2D` component-wise.
70 * @param vector The vector to add.
71 * @return A new `Vector2D` representing the sum.
72 */
73 Vector2D Add(const Vector2D& vector) const;
74
75 /**
76 * @brief Subtracts another `Vector2D` from this vector component-wise.
77 * @param vector The vector to subtract.
78 * @return A new `Vector2D` representing the difference.
79 */
80 Vector2D Subtract(const Vector2D& vector) const;
81
82 /**
83 * @brief Multiplies this vector by another `Vector2D` component-wise.
84 * @param vector The vector to multiply by.
85 * @return A new `Vector2D` representing the product.
86 */
87 Vector2D Multiply(const Vector2D& vector) const;
88
89 /**
90 * @brief Divides this vector by another `Vector2D` component-wise.
91 * @param vector The vector to divide by.
92 * @return A new `Vector2D` representing the quotient.
93 */
94 Vector2D Divide(const Vector2D& vector) const;
95
96 /**
97 * @brief Scales this vector by a float.
98 * @param scalar The scalar value.
99 * @return A new `Vector2D` scaled by `scalar`.
100 */
101 Vector2D Multiply(const float& scalar) const;
102
103 /**
104 * @brief Divides this vector by a scalar.
105 * @param scalar The scalar divisor.
106 * @return A new `Vector2D` after division by `scalar`.
107 */
108 Vector2D Divide(const float& scalar) const;
109
110 /**
111 * @brief Calculates the 2D cross product of this vector with another.
112 *
113 * For 2D vectors, the "cross product" reduces to a scalar, which is conceptually
114 * the Z-component of the 3D cross product.
115 *
116 * @param vector The other `Vector2D`.
117 * @return The scalar cross product result.
118 */
119 float CrossProduct(const Vector2D& vector) const;
120
121 /**
122 * @brief Normalizes this vector such that its magnitude is 1 (if non-zero).
123 * @return A unit circle vector pointing in the same direction.
124 */
125 Vector2D UnitCircle() const;
126
127 /**
128 * @brief Constrains each component of this vector between the specified minimum and maximum.
129 * @param minimum The lower bound.
130 * @param maximum The upper bound.
131 * @return A new `Vector2D` with each component constrained.
132 */
133 Vector2D Constrain(const float& minimum, const float& maximum) const;
134
135 /**
136 * @brief Constrains each component of this vector between the corresponding components
137 * of two other `Vector2D` objects.
138 * @param minimum The lower bound vector.
139 * @param maximum The upper bound vector.
140 * @return A new `Vector2D` with each component constrained.
141 */
142 Vector2D Constrain(const Vector2D& minimum, const Vector2D& maximum) const;
143
144 /**
145 * @brief Computes the minimum components between this vector and another `Vector2D`.
146 * @param v The other `Vector2D`.
147 * @return A new `Vector2D` taking the minimum of X and Y components.
148 */
149 Vector2D Minimum(const Vector2D& v) const;
150
151 /**
152 * @brief Computes the maximum components between this vector and another `Vector2D`.
153 * @param v The other `Vector2D`.
154 * @return A new `Vector2D` taking the maximum of X and Y components.
155 */
156 Vector2D Maximum(const Vector2D& v) const;
157
158 /**
159 * @brief Rotates this vector by a specified angle (in degrees or radians) around a given offset.
160 * @param angle The angle of rotation.
161 * @param offset The origin about which to rotate.
162 * @return A new `Vector2D` representing the rotated vector.
163 */
164 Vector2D Rotate(const float& angle, const Vector2D& offset) const;
165
166 /**
167 * @brief Checks if this vector lies within the bounds of two other `Vector2D` objects.
168 * @param minimum The lower bound `Vector2D`.
169 * @param maximum The upper bound `Vector2D`.
170 * @return `true` if within bounds, otherwise `false`.
171 */
172 bool CheckBounds(const Vector2D& minimum, const Vector2D& maximum) const;
173
174 /**
175 * @brief Computes the magnitude (length) of this vector using the formula sqrt(X^2 + Y^2).
176 * @return The length of the vector.
177 */
178 float Magnitude() const;
179
180 /**
181 * @brief Computes the dot product of this vector with another `Vector2D`.
182 * @param vector The other `Vector2D`.
183 * @return The dot product result (X1*X2 + Y1*Y2).
184 */
185 float DotProduct(const Vector2D& vector) const;
186
187 /**
188 * @brief Calculates the Euclidean distance between this vector and another `Vector2D`.
189 * @param vector The other `Vector2D`.
190 * @return The distance between the two vectors.
191 */
192 float CalculateEuclideanDistance(const Vector2D& vector) const;
193
194 /**
195 * @brief Checks if this vector is equal to another `Vector2D` component-wise.
196 * @param vector The `Vector2D` to compare.
197 * @return `true` if equal, otherwise `false`.
198 */
199 bool IsEqual(const Vector2D& vector) const;
200
201 /**
202 * @brief Converts the vector to a string representation.
203 * @return A `String` in the format "(X, Y)".
204 */
205 String ToString() const;
206
207
208 // --- Static function declarations ---
209
210 /**
211 * @brief Returns the squared magnitude of a given vector (X^2 + Y^2) as a Vector2D.
212 *
213 * Note: This function name can be misleading since "Normal" typically implies a normalized vector.
214 * Here, it is used as in "square of the magnitude" or "norm".
215 *
216 * @param vector The input `Vector2D`.
217 * @return A `Vector2D` containing squared values of each component.
218 */
219 static Vector2D Normal(const Vector2D& vector);
220
221 /**
222 * @brief Adds two vectors (component-wise).
223 * @param v1 The first `Vector2D`.
224 * @param v2 The second `Vector2D`.
225 * @return A new `Vector2D` representing the sum.
226 */
227 static Vector2D Add(const Vector2D& v1, const Vector2D& v2);
228
229 /**
230 * @brief Subtracts one vector from another (component-wise).
231 * @param v1 The first `Vector2D`.
232 * @param v2 The second `Vector2D` to subtract from `v1`.
233 * @return A new `Vector2D` representing the difference.
234 */
235 static Vector2D Subtract(const Vector2D& v1, const Vector2D& v2);
236
237 /**
238 * @brief Multiplies two vectors component-wise.
239 * @param v1 The first `Vector2D`.
240 * @param v2 The second `Vector2D`.
241 * @return A new `Vector2D` representing the product.
242 */
243 static Vector2D Multiply(const Vector2D& v1, const Vector2D& v2);
244
245 /**
246 * @brief Divides two vectors component-wise.
247 * @param v1 The first `Vector2D`.
248 * @param v2 The second `Vector2D` (divisor).
249 * @return A new `Vector2D` representing the quotient.
250 */
251 static Vector2D Divide(const Vector2D& v1, const Vector2D& v2);
252
253 /**
254 * @brief Scales a `Vector2D` by a float, component-wise.
255 * @param vector The `Vector2D`.
256 * @param scalar The scaling factor.
257 * @return A new `Vector2D` scaled by `scalar`.
258 */
259 static Vector2D Multiply(const Vector2D& vector, const float& scalar);
260
261 /**
262 * @brief Scales a `Vector2D` by a float, component-wise (scalar on the left).
263 * @param scalar The scaling factor.
264 * @param vector The `Vector2D`.
265 * @return A new `Vector2D` scaled by `scalar`.
266 */
267 static Vector2D Multiply(const float& scalar, const Vector2D& vector);
268
269 /**
270 * @brief Divides a `Vector2D` by a scalar, component-wise.
271 * @param vector The `Vector2D`.
272 * @param scalar The scalar divisor.
273 * @return A new `Vector2D` after the division.
274 */
275 static Vector2D Divide(const Vector2D& vector, const float& scalar);
276
277 /**
278 * @brief Computes the 2D cross product of two vectors, returning a scalar (Z-component in 3D).
279 * @param v1 The first `Vector2D`.
280 * @param v2 The second `Vector2D`.
281 * @return The scalar cross product (v1.x*v2.y - v1.y*v2.x).
282 */
283 static float CrossProduct(const Vector2D& v1, const Vector2D& v2);
284
285 /**
286 * @brief Computes the dot product of two `Vector2D`s.
287 * @param v1 The first `Vector2D`.
288 * @param v2 The second `Vector2D`.
289 * @return The dot product (v1.x*v2.x + v1.y*v2.y).
290 */
291 static float DotProduct(const Vector2D& v1, const Vector2D& v2);
292
293 /**
294 * @brief Calculates the Euclidean distance between two `Vector2D`s.
295 * @param v1 The first `Vector2D`.
296 * @param v2 The second `Vector2D`.
297 * @return The distance between `v1` and `v2`.
298 */
299 static float CalculateEuclideanDistance(const Vector2D& v1, const Vector2D& v2);
300
301 /**
302 * @brief Checks if two `Vector2D`s are equal component-wise.
303 * @param v1 The first `Vector2D`.
304 * @param v2 The second `Vector2D`.
305 * @return `true` if equal, otherwise `false`.
306 */
307 static bool IsEqual(const Vector2D& v1, const Vector2D& v2);
308
309 /**
310 * @brief Returns a new vector with the minimum components of two `Vector2D`s.
311 * @param v1 The first `Vector2D`.
312 * @param v2 The second `Vector2D`.
313 * @return A `Vector2D` with per-component minimums.
314 */
315 static Vector2D Minimum(const Vector2D& v1, const Vector2D& v2);
316
317 /**
318 * @brief Returns a new vector with the maximum components of two `Vector2D`s.
319 * @param v1 The first `Vector2D`.
320 * @param v2 The second `Vector2D`.
321 * @return A `Vector2D` with per-component maximums.
322 */
323 static Vector2D Maximum(const Vector2D& v1, const Vector2D& v2);
324
325 /**
326 * @brief Performs linear interpolation between two `Vector2D`s.
327 * @param start The start `Vector2D`.
328 * @param finish The end `Vector2D`.
329 * @param ratio A normalized factor (0 to 1).
330 * @return A new `Vector2D` representing the linear interpolation result.
331 */
332 static Vector2D LERP(const Vector2D& start, const Vector2D& finish, const float& ratio);
333
334 /**
335 * @brief Converts a vector of degrees to radians (component-wise).
336 * @param degrees The vector in degrees.
337 * @return A `Vector2D` in radians.
338 */
340
341 /**
342 * @brief Converts a vector of radians to degrees (component-wise).
343 * @param radians The vector in radians.
344 * @return A `Vector2D` in degrees.
345 */
347
348 /**
349 * @brief Checks if two line segments defined by (p1, p2) and (q1, q2) intersect in 2D space.
350 * @param p1 Start of the first segment.
351 * @param p2 End of the first segment.
352 * @param q1 Start of the second segment.
353 * @param q2 End of the second segment.
354 * @return `true` if the segments intersect, otherwise `false`.
355 */
356 static bool LineSegmentsIntersect(const Vector2D& p1, const Vector2D& p2,
357 const Vector2D& q1, const Vector2D& q2);
358
359
360 // --- Operator overloads ---
361
362 /**
363 * @brief Equality operator. Checks if two `Vector2D`s are equal (component-wise).
364 * @param vector The vector to compare with.
365 * @return `true` if equal, otherwise `false`.
366 */
367 bool operator ==(const Vector2D& vector) const;
368
369 /**
370 * @brief Inequality operator. Checks if two `Vector2D`s differ (component-wise).
371 * @param vector The vector to compare with.
372 * @return `true` if not equal, otherwise `false`.
373 */
374 bool operator !=(const Vector2D& vector) const;
375
376 /**
377 * @brief Assignment operator. Copies another `Vector2D` into this one.
378 * @param vector The vector to copy.
379 * @return A reference to this `Vector2D`.
380 */
382
383 /**
384 * @brief Assignment operator. Copies a `Vector3D` into this `Vector2D`, discarding Z.
385 * @param vector The `Vector3D` to copy from.
386 * @return A reference to this `Vector2D`.
387 */
389
390 /**
391 * @brief Addition operator. Adds two vectors component-wise.
392 * @param vector The right-hand side `Vector2D`.
393 * @return A new `Vector2D` representing the sum.
394 */
395 Vector2D operator +(const Vector2D& vector) const;
396
397 /**
398 * @brief Subtraction operator. Subtracts two vectors component-wise.
399 * @param vector The right-hand side `Vector2D`.
400 * @return A new `Vector2D` representing the difference.
401 */
402 Vector2D operator -(const Vector2D& vector) const;
403
404 /**
405 * @brief Multiplication operator. Multiplies two vectors component-wise.
406 * @param vector The right-hand side `Vector2D`.
407 * @return A new `Vector2D` representing the product.
408 */
409 Vector2D operator *(const Vector2D& vector) const;
410
411 /**
412 * @brief Division operator. Divides two vectors component-wise.
413 * @param vector The right-hand side `Vector2D` (divisor).
414 * @return A new `Vector2D` representing the quotient.
415 */
416 Vector2D operator /(const Vector2D& vector) const;
417
418 /**
419 * @brief Multiplication operator by a float scalar (on the right).
420 * @param value The scalar factor.
421 * @return A new `Vector2D` scaled by `value`.
422 */
423 Vector2D operator *(const float& value) const;
424
425 /**
426 * @brief Division operator by a float scalar.
427 * @param value The scalar divisor.
428 * @return A new `Vector2D` after division by `value`.
429 */
430 Vector2D operator /(const float& value) const;
431};
Provides a collection of mathematical utility functions and constants.
Defines a 3D vector and various related operations.
Implements a generic Kalman Filter for 1D data.
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27
Vector2D Multiply(const Vector2D &vector) const
Multiplies this vector by another Vector2D component-wise.
Definition Vector2D.cpp:46
Vector2D Rotate(const float &angle, const Vector2D &offset) const
Rotates this vector by a specified angle (in degrees or radians) around a given offset.
Definition Vector2D.cpp:124
Vector2D Divide(const Vector2D &vector) const
Divides this vector by another Vector2D component-wise.
Definition Vector2D.cpp:53
static Vector2D RadiansToDegrees(const Vector2D &radians)
Converts a vector of radians to degrees (component-wise).
Definition Vector2D.cpp:193
Vector2D operator+(const Vector2D &vector) const
Addition operator. Adds two vectors component-wise.
Definition Vector2D.cpp:291
Vector2D operator/(const Vector2D &vector) const
Division operator. Divides two vectors component-wise.
Definition Vector2D.cpp:303
Vector2D Maximum(const Vector2D &v) const
Computes the maximum components between this vector and another Vector2D.
Definition Vector2D.cpp:117
bool operator!=(const Vector2D &vector) const
Inequality operator. Checks if two Vector2Ds differ (component-wise).
Definition Vector2D.cpp:273
bool CheckBounds(const Vector2D &minimum, const Vector2D &maximum) const
Checks if this vector lies within the bounds of two other Vector2D objects.
Definition Vector2D.cpp:138
bool IsEqual(const Vector2D &vector) const
Checks if this vector is equal to another Vector2D component-wise.
Definition Vector2D.cpp:156
float DotProduct(const Vector2D &vector) const
Computes the dot product of this vector with another Vector2D.
Definition Vector2D.cpp:146
static Vector2D LERP(const Vector2D &start, const Vector2D &finish, const float &ratio)
Performs linear interpolation between two Vector2Ds.
Definition Vector2D.cpp:183
Vector2D()
Constructs a default Vector2D with X = 0 and Y = 0.
Definition Vector2D.cpp:3
Vector2D Minimum(const Vector2D &v) const
Computes the minimum components between this vector and another Vector2D.
Definition Vector2D.cpp:110
Vector2D Normal() const
Computes the squared magnitude of the vector (X^2 + Y^2).
Definition Vector2D.cpp:18
Vector2D Subtract(const Vector2D &vector) const
Subtracts another Vector2D from this vector component-wise.
Definition Vector2D.cpp:39
float CalculateEuclideanDistance(const Vector2D &vector) const
Calculates the Euclidean distance between this vector and another Vector2D.
Definition Vector2D.cpp:150
Vector2D UnitCircle() const
Normalizes this vector such that its magnitude is 1 (if non-zero).
Definition Vector2D.cpp:84
Vector2D operator*(const Vector2D &vector) const
Multiplication operator. Multiplies two vectors component-wise.
Definition Vector2D.cpp:299
static Vector2D DegreesToRadians(const Vector2D &degrees)
Converts a vector of degrees to radians (component-wise).
Definition Vector2D.cpp:189
String ToString() const
Converts the vector to a string representation.
Definition Vector2D.cpp:160
Vector2D operator=(const Vector2D &vector)
Assignment operator. Copies another Vector2D into this one.
Definition Vector2D.cpp:277
float CrossProduct(const Vector2D &vector) const
Calculates the 2D cross product of this vector with another.
Definition Vector2D.cpp:80
float X
The X-component of the 2D vector.
Definition Vector2D.h:29
float Y
The Y-component of the 2D vector.
Definition Vector2D.h:30
Vector2D Absolute() const
Returns a vector with the absolute value of each component.
Definition Vector2D.cpp:11
Vector2D Add(const Vector2D &vector) const
Adds this vector to another Vector2D component-wise.
Definition Vector2D.cpp:32
Vector2D Constrain(const float &minimum, const float &maximum) const
Constrains each component of this vector between the specified minimum and maximum.
Definition Vector2D.cpp:96
bool operator==(const Vector2D &vector) const
Equality operator. Checks if two Vector2Ds are equal (component-wise).
Definition Vector2D.cpp:269
float Magnitude() const
Computes the magnitude (length) of this vector using the formula sqrt(X^2 + Y^2).
Definition Vector2D.cpp:142
Vector2D operator-(const Vector2D &vector) const
Subtraction operator. Subtracts two vectors component-wise.
Definition Vector2D.cpp:295
static bool LineSegmentsIntersect(const Vector2D &p1, const Vector2D &p2, const Vector2D &q1, const Vector2D &q2)
Checks if two line segments defined by (p1, p2) and (q1, q2) intersect in 2D space.
Definition Vector2D.cpp:249
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26