ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Vector2D.cpp
Go to the documentation of this file.
1#include "Vector2D.h"
2
3Vector2D::Vector2D() : X(0.0f), Y(0.0f) {}
4
6
7Vector2D::Vector2D(const float& X, const float& Y) : X(X), Y(Y) {}
8
10
12 return Vector2D{
13 fabsf(X),
14 fabsf(Y)
15 };
16}
17
19 float magn = Magnitude();
20
22 return (*this);
23 }
25 return Multiply(3.40282e+038f);
26 }
27 else{
28 return Multiply(1.0f / magn);
29 }
30}
31
33 return Vector2D{
34 X + vector.X,
35 Y + vector.Y
36 };
37}
38
40 return Vector2D{
41 X - vector.X,
42 Y - vector.Y
43 };
44}
45
47 return Vector2D{
48 X * vector.X,
49 Y * vector.Y
50 };
51}
52
54 return Vector2D{
55 X / vector.X,
56 Y / vector.Y
57 };
58}
59
60Vector2D Vector2D::Multiply(const float& scalar) const {
61 if (Mathematics::IsClose(scalar, 1.0f, Mathematics::EPSILON)) return (*this);
63
64 return Vector2D{
65 X * scalar,
66 Y * scalar
67 };
68}
69
70Vector2D Vector2D::Divide(const float& scalar) const {
71 if (Mathematics::IsClose(scalar, 1.0f, Mathematics::EPSILON)) return (*this);
73
74 return Vector2D{
75 X / scalar,
76 Y / scalar
77 };
78}
79
81 return (X * vector.Y) - (Y * vector.X);
82}
83
85 float length = Magnitude();
86
87 if (Mathematics::IsClose(length, 1.0f, Mathematics::EPSILON)) return Vector2D(this->X, this->Y);
88 if (length == 0) return Vector2D(0, 1);
89
90 return Vector2D{
91 X / length,
92 Y / length
93 };
94}
95
96Vector2D Vector2D::Constrain(const float& minimum, const float& maximum) const {
97 return Vector2D{
98 Mathematics::Constrain(X, minimum, maximum),
99 Mathematics::Constrain(Y, minimum, maximum)
100 };
101}
102
103Vector2D Vector2D::Constrain(const Vector2D& minimum, const Vector2D& maximum) const {
104 return Vector2D{
105 Mathematics::Constrain(X, minimum.X, maximum.X),
106 Mathematics::Constrain(Y, minimum.Y, maximum.Y)
107 };
108}
109
111 return Vector2D{
112 X < v.X ? X : v.X,
113 Y < v.Y ? Y : v.Y
114 };
115}
116
118 return Vector2D{
119 X > v.X ? X : v.X,
120 Y > v.Y ? Y : v.Y
121 };
122}
123
124Vector2D Vector2D::Rotate(const float& angle, const Vector2D& offset) const {
125 Vector2D v = Vector2D(X, Y).Subtract(offset);
126
127 float angleMPD18 = angle * Mathematics::MPID180;
128
129 float cs = cosf(angleMPD18);
130 float sn = sinf(angleMPD18);
131
132 return Vector2D{
133 v.X * cs - v.Y * sn + offset.X,
134 v.X * sn + v.Y * cs + offset.Y
135 };
136}
137
138bool Vector2D::CheckBounds(const Vector2D& minimum, const Vector2D& maximum) const {
139 return X > minimum.X && X < maximum.X && Y > minimum.Y && Y < maximum.Y;
140}
141
142float Vector2D::Magnitude() const {
143 return Mathematics::Sqrt(X * X + Y * Y);
144}
145
147 return (X * vector.X) + (Y * vector.Y);
148}
149
151 Vector2D offset = Vector2D(X - vector.X, Y - vector.Y);
152
153 return offset.Magnitude();
154}
155
157 return (X == vector.X) && (Y == vector.Y);
158}
159
163
164 return "[" + x + ", " + y + "]";
165}
166
167
168// Implementations of static member functions
170 return Vector2D{
171 v1.X < v2.X ? v1.X : v2.X,
172 v1.Y < v2.Y ? v1.Y : v2.Y
173 };
174}
175
177 return Vector2D{
178 v1.X > v2.X ? v1.X : v2.X,
179 v1.Y > v2.Y ? v1.Y : v2.Y
180 };
181}
182
183Vector2D Vector2D::LERP(const Vector2D& start, const Vector2D& finish, const float& ratio) {
185
186 return finishL * ratio + startL * (1.0f - ratio);
187}
188
192
196
198 Vector2D normal = vector;
199
200 return normal.Normal();
201}
202
204 return Vector2D(v1.X + v2.X, v1.Y + v2.Y);
205}
206
208 return Vector2D(v1.X - v2.X, v1.Y - v2.Y);
209}
210
212 return Vector2D(v1.X * v2.X, v1.Y * v2.Y);
213}
214
216 return Vector2D(v1.X / v2.X, v1.Y / v2.Y);
217}
218
220 return Vector2D(vector.X * scalar, vector.Y * scalar);
221}
222
224 return Vector2D(vector.X * scalar, vector.Y * scalar);
225}
226
228 return Vector2D(vector.X / scalar, vector.Y / scalar);
229}
230
232 return (v1.X * v2.Y) - (v1.Y * v2.X);
233}
234
236 return (v1.X * v2.X) + (v1.Y * v2.Y);
237}
238
240 Vector2D offset = Vector2D(v1.X - v2.X, v1.Y - v2.Y);
241
242 return offset.Magnitude();
243}
244
246 return (v1.X == v2.X) && (v1.Y == v2.Y);
247}
248
249bool Vector2D::LineSegmentsIntersect(const Vector2D& p1, const Vector2D& p2, const Vector2D& q1, const Vector2D& q2) {
250 Vector2D dirP = p2 - p1;
251 Vector2D dirQ = q2 - q1;
252 float crossPQ = dirP.CrossProduct(dirQ);
253
254 if (fabsf(crossPQ) < Mathematics::EPSILON) return false;
255
256 Vector2D diffPQ1 = q1 - p1;
257 Vector2D diffPQ2 = q2 - p1;
258 float crossPDiff1 = dirP.CrossProduct(diffPQ1);
259 float crossPDiff2 = dirP.CrossProduct(diffPQ2);
260
261 if ((crossPDiff1 * crossPDiff2) <= 0) {
262 return true;
263 }
264
265 return false;
266}
267
268// Operator overloads
270 return this->IsEqual(vector);
271}
272
274 return !(this->IsEqual(vector));
275}
276
278 this->X = vector.X;
279 this->Y = vector.Y;
280
281 return *this;
282}
283
285 this->X = vector.X;
286 this->Y = vector.Y;
287
288 return *this;
289}
290
292 return Add(vector);
293}
294
296 return Subtract(vector);
297}
298
300 return Multiply(vector);
301}
302
304 return Divide(vector);
305}
306
308 return Multiply(value);
309}
310
312 return Divide(value);
313}
Defines a 2D vector and various related operations.
Implements a generic Kalman Filter for 1D data.
static float Sqrt(float value)
Computes the square root of a value.
static bool IsClose(float v1, float v2, float epsilon)
Checks if two values are close within a specified epsilon.
static String DoubleToCleanString(float value)
Converts a floating-point value to a String, removing trailing decimals if not needed.
static const float MPID180
The value of , useful for converting degrees to radians.
Definition Mathematics.h:47
static const float EPSILON
A small constant used for floating-point comparisons.
Definition Mathematics.h:37
static T Constrain(T value, T minimum, T maximum)
Constrains a value between minimum and maximum.
static const float M180DPI
The value of , useful for converting radians to degrees.
Definition Mathematics.h:52
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
float X
The X-component of the 3D vector.
Definition Vector3D.h:28
float Y
The Y-component of the 3D vector.
Definition Vector3D.h:29