Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Vector2.hpp
1// INFINITY_API_PUBLIC
2
3#pragma once
4
5#include <Infinity/Types/Core/Value.hpp>
6#include <Infinity/Types/Containers/Array.hpp>
7
8#include <cstdint>
9#include <cmath>
10
12{
73 template<typename T>
74 struct INFINITY_API_TEMPLATE t_Vector2
75 {
85 union
86 {
87 T value[2];
88 struct
89 {
90 T x, y;
91 };
92 struct
93 {
94 T r, g;
95 };
96 struct
97 {
98 T s, t;
99 };
100 };
101
107 constexpr t_Vector2() : value{} {}
108
114 constexpr t_Vector2(const t_Vector2& v) : value{v.x, v.y} {}
115
119 constexpr t_Vector2& operator=(const t_Vector2&) = default;
120
132 constexpr t_Vector2(T x, T y) : value{x, y} {}
133
148 template<typename R>
149 constexpr explicit t_Vector2(const t_Vector2<R>& v) :
150 value{static_cast<T>(v.x), static_cast<T>(v.y)} {}
151
158 T& operator[](size_t i) { return value[i]; }
159
166 T operator[](size_t i) const { return value[i]; }
167
174 void set(T in_x, T in_y)
175 {
176 x = in_x;
177 y = in_y;
178 }
179
191 T* data() { return value; }
192
198 const T* data() const { return value; }
199
210 template<typename R>
212 {
213 value[0] = static_cast<T>(rhs[0]);
214 value[1] = static_cast<T>(rhs[1]);
215 return *this;
216 }
217
224 inline t_Vector2& operator+=(const t_Vector2& rhs)
225 {
226 value[0] += rhs.value[0];
227 value[1] += rhs.value[1];
228 return *this;
229 }
230
237 inline t_Vector2& operator-=(const t_Vector2& rhs)
238 {
239 value[0] -= rhs.value[0];
240 value[1] -= rhs.value[1];
241 return *this;
242 }
243
250 inline t_Vector2& operator*=(T rhs)
251 {
252 value[0] *= rhs;
253 value[1] *= rhs;
254 return *this;
255 }
256
263 inline t_Vector2& operator*=(const t_Vector2& rhs)
264 {
265 value[0] *= rhs.value[0];
266 value[1] *= rhs.value[1];
267 return *this;
268 }
269
281 inline t_Vector2& operator/=(T rhs)
282 {
283 if constexpr (std::is_floating_point_v<T>)
284 {
285 T inv = static_cast<T>(1.0) / rhs;
286 value[0] *= inv;
287 value[1] *= inv;
288 }
289 else
290 {
291 value[0] /= rhs;
292 value[1] /= rhs;
293 }
294 return *this;
295 }
296
311 explicit operator bool() const noexcept { return value[0] != 0.0 || value[1] != 0.0; }
312
327 {
328 float len = length();
329 if (len == 0.0f) return t_Vector2<T>(0, 0);
330 return t_Vector2<T>(x / len, y / len);
331 }
332
344 t_Vector2<T> cross(const t_Vector2<T>& other) const
345 {
346 return t_Vector2<T>(y * other.x - x * other.y, x * other.y - y * other.x);
347 }
348
359 float length() const
360 {
361 return std::sqrt(squaredMagnitude());
362 }
363
371 float magnitude() const
372 {
373 return std::sqrt(squaredMagnitude());
374 }
375
391 float squaredMagnitude() const
392 {
393 return x * x + y * y;
394 }
395
408 float distance(const t_Vector2<T>& other) const
409 {
410 return (*this - other).magnitude();
411 }
412
429 float dot(const t_Vector2<T>& other) const
430 {
431 return x * other.x + y * other.y;
432 }
433
450 float angle(const t_Vector2<T>& other) const
451 {
452 float dotProduct = dot(other);
453 float lengths = length() * other.length();
454 if (lengths == 0.0f) return 0.0f; // Avoid division by zero
455 return std::acos(dotProduct / lengths);
456 }
457
474 {
475 if constexpr (std::is_unsigned<T>::value) {
476 return *this; // No change for unsigned types
477 } else {
478 return t_Vector2<T>(std::abs(x), std::abs(y));
479 }
480 }
481 };
482
488
494
500
506
512
518
524
530
536
542
548
557 template<typename T>
558 constexpr bool operator==(const t_Vector2<T>& lhs, const t_Vector2<T>& rhs)
559 {
560 return lhs[0] == rhs[0] && lhs[1] == rhs[1];
561 }
562
571 template<typename T>
572 constexpr bool operator!=(const t_Vector2<T>& lhs, const t_Vector2<T>& rhs)
573 {
574 return lhs[0] != rhs[0] || lhs[1] != rhs[1];
575 }
576
588 template<typename T>
589 constexpr bool operator<(const t_Vector2<T>& lhs, const t_Vector2<T>& rhs)
590 {
591 if (lhs[0] < rhs[0]) return true;
592 if (lhs[0] > rhs[0]) return false;
593 return lhs[1] < rhs[1];
594 }
595
604 template<typename T>
605 constexpr t_Vector2<T> operator-(const t_Vector2<T>& lhs, const t_Vector2<T>& rhs)
606 {
607 return t_Vector2<T>(lhs[0] - rhs[0], lhs[1] - rhs[1]);
608 }
609
617 template<typename T>
619 {
620 return t_Vector2<T>(-v[0], -v[1]);
621 }
622
631 template<typename T>
632 constexpr t_Vector2<T> operator+(const t_Vector2<T>& lhs, const t_Vector2<T>& rhs)
633 {
634 return t_Vector2<T>(lhs[0] + rhs[0], lhs[1] + rhs[1]);
635 }
636
645 template<typename T>
646 constexpr t_Vector2<T> operator*(const t_Vector2<T>& lhs, T rhs)
647 {
648 return t_Vector2<T>(lhs[0] * rhs, lhs[1] * rhs);
649 }
650
659 template<typename T>
660 constexpr t_Vector2<T> operator*(const t_Vector2<T>& lhs, const t_Vector2<T>& rhs)
661 {
662 return t_Vector2<T>(lhs[0] * rhs[0], lhs[1] * rhs[1]);
663 }
664
677 template<typename T>
678 constexpr t_Vector2<T> operator/(const t_Vector2<T>& lhs, T rhs)
679 {
680 if constexpr (std::is_floating_point_v<T>)
681 {
682 T inv = static_cast<T>(1.0) / rhs;
683 return t_Vector2<T>(lhs[0] * inv, lhs[1] * inv);
684 }
685 else
686 {
687 return t_Vector2<T>(lhs[0] / rhs, lhs[1] / rhs);
688 }
689 }
690
701 template<typename T>
702 inline std::ostream& operator<<(std::ostream& out, const t_Vector2<T>& v)
703 {
704 out << v.x << " " << v.y;
705 return out;
706 }
707
718 template<typename T>
719 inline std::istream& operator>>(std::istream& in, t_Vector2<T>& v)
720 {
721 in >> v.x >> v.y;
722 return in;
723 }
724
735 template<>
736 inline std::istream& operator>>(std::istream& in, t_Vector2<uint8_t>& v) {
737 int temp_x, temp_y;
738 in >> temp_x >> temp_y;
739
740 v.x = static_cast<uint8_t>(temp_x);
741 v.y = static_cast<uint8_t>(temp_y);
742
743 return in;
744 }
745
746}
747
748// Register Value<Vector2> specializations in the type system
759
760// Register Array<Vector2> specializations in the type system
Dynamic contiguous container for homogeneous elements in the Infinity type system.
Definition Array.hpp:77
Template wrapper for primitive types to integrate with the Infinity type system.
Definition Value.hpp:89
Definition Math.hpp:9
std::istream & operator>>(std::istream &in, t_Matrix3< T > &m)
Stream extraction operator for single-line text input.
Definition Matrix3.hpp:726
constexpr Rect operator-(const Rect &lhs, const Rect &rhs)
Rectangle subtraction operator.
Definition Rect.hpp:371
constexpr bool operator<(const t_Vector2< T > &lhs, const t_Vector2< T > &rhs)
Less-than comparison operator for ordering.
Definition Vector2.hpp:589
constexpr t_Vector2< T > operator/(const t_Vector2< T > &lhs, T rhs)
Scalar division.
Definition Vector2.hpp:678
constexpr t_Vector2< T > operator*(const t_Vector2< T > &lhs, T rhs)
Scalar multiplication.
Definition Vector2.hpp:646
constexpr Rect operator+(const Rect &lhs, const Rect &rhs)
Rectangle addition operator.
Definition Rect.hpp:398
std::ostream & operator<<(std::ostream &out, const t_Matrix3< T > &m)
Stream insertion operator for single-line text output.
Definition Matrix3.hpp:709
constexpr bool operator!=(const Rect &lhs, const Rect &rhs)
Inequality comparison operator.
Definition Rect.hpp:357
constexpr bool operator==(const Rect &lhs, const Rect &rhs)
Equality comparison operator.
Definition Rect.hpp:345
Template structure representing a 2-component vector.
Definition Vector2.hpp:75
constexpr t_Vector2()
Default constructor.
Definition Vector2.hpp:107
void set(T in_x, T in_y)
Sets both components simultaneously.
Definition Vector2.hpp:174
t_Vector2< T > normalized() const
Computes the normalized version of this vector.
Definition Vector2.hpp:326
constexpr t_Vector2(const t_Vector2 &v)
Copy constructor.
Definition Vector2.hpp:114
t_Vector2 & operator+=(const t_Vector2 &rhs)
Component-wise addition assignment.
Definition Vector2.hpp:224
float length() const
Computes the length (magnitude) of this vector.
Definition Vector2.hpp:359
float angle(const t_Vector2< T > &other) const
Computes the angle between this vector and another.
Definition Vector2.hpp:450
t_Vector2< T > abs() const
Computes the component-wise absolute value.
Definition Vector2.hpp:473
constexpr t_Vector2(const t_Vector2< R > &v)
Explicit type-converting constructor.
Definition Vector2.hpp:149
t_Vector2< T > cross(const t_Vector2< T > &other) const
Computes the 2D cross product with another vector.
Definition Vector2.hpp:344
t_Vector2 & operator-=(const t_Vector2 &rhs)
Component-wise subtraction assignment.
Definition Vector2.hpp:237
T value[2]
Array access to components.
Definition Vector2.hpp:87
float distance(const t_Vector2< T > &other) const
Computes the Euclidean distance to another vector.
Definition Vector2.hpp:408
t_Vector2 & operator=(const t_Vector2< R > &rhs)
Type-converting assignment operator.
Definition Vector2.hpp:211
float dot(const t_Vector2< T > &other) const
Computes the dot product with another vector.
Definition Vector2.hpp:429
const T * data() const
Gets a const pointer to the underlying data.
Definition Vector2.hpp:198
t_Vector2 & operator/=(T rhs)
Scalar division assignment.
Definition Vector2.hpp:281
constexpr t_Vector2(T x, T y)
Constructs a vector from component values.
Definition Vector2.hpp:132
T operator[](size_t i) const
Array subscript operator (const).
Definition Vector2.hpp:166
T * data()
Gets a mutable pointer to the underlying data.
Definition Vector2.hpp:191
T g
Color component interpretation.
Definition Vector2.hpp:94
t_Vector2 & operator*=(const t_Vector2 &rhs)
Component-wise multiplication assignment.
Definition Vector2.hpp:263
T s
Definition Vector2.hpp:98
float magnitude() const
Computes the magnitude of this vector.
Definition Vector2.hpp:371
T y
Cartesian coordinate interpretation.
Definition Vector2.hpp:90
float squaredMagnitude() const
Computes the squared magnitude of this vector.
Definition Vector2.hpp:391
T x
Definition Vector2.hpp:90
T & operator[](size_t i)
Array subscript operator (mutable).
Definition Vector2.hpp:158
constexpr t_Vector2 & operator=(const t_Vector2 &)=default
Copy assignment operator.
t_Vector2 & operator*=(T rhs)
Scalar multiplication assignment.
Definition Vector2.hpp:250