SA_Maths
Sapphire Suite's C++ Maths Library
Loading...
Searching...
No Matches
Vector2.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 Sapphire Development Team. All Rights Reserved.
2
3#pragma once
4
5#ifndef SAPPHIRE_MATHS_VECTOR2_GUARD
6#define SAPPHIRE_MATHS_VECTOR2_GUARD
7
8#include <cstdint>
9
10#include <SA/Maths/Debug.hpp>
11
18
29namespace SA
30{
31 template <typename T>
32 struct Vec3;
33
39 template <typename T>
40 struct Vec2
41 {
43 using Type = T;
44
45
47 T x = T();
48
50 T y = T();
51
52//{ Constants
53
55 static const Vec2 Zero;
56
58 static const Vec2 One;
59
61 static const Vec2 Right;
62
64 static const Vec2 Left;
65
67 static const Vec2 Up;
68
70 static const Vec2 Down;
71
72//}
73
74//{ Constructors
75
77 Vec2() = default;
78
79
86 constexpr Vec2(T _x, T _y) noexcept;
87
93 constexpr Vec2(T _scale) noexcept;
94
95
97 Vec2(Vec2&&) = default;
98
100 Vec2(const Vec2&) = default;
101
109 template <typename TIn>
110 constexpr Vec2(const Vec2<TIn>& _other) noexcept;
111
119 template <typename TIn>
120 constexpr Vec2(const Vec3<TIn>& _other) noexcept;
121
122//}
123
124//{ Equals
125
131 constexpr bool IsZero() const noexcept;
132
141 constexpr bool Equals(const Vec2& _other, T _epsilon = std::numeric_limits<T>::epsilon()) const noexcept;
142
143
151 constexpr bool operator==(const Vec2 & _rhs) const noexcept;
152
160 constexpr bool operator!=(const Vec2 & _rhs) const noexcept;
161
162//}
163
164//{ Accessors
165
171 T* Data() noexcept;
172
178 const T* Data() const noexcept;
179
180
188 T& operator[](uint32_t _index);
189
197 const T& operator[](uint32_t _index) const;
198
199//}
200
201//{ Length
202
208 constexpr T Length() const;
209
215 constexpr T SqrLength() const noexcept;
216
217
224
231
237 constexpr bool IsNormalized() const noexcept;
238
239//}
240
241//{ Project
242
251 Vec2 Reflect(const Vec2& _normal, float _elasticity = 1.0f) const noexcept;
252
262 Vec2 ProjectOnTo(const Vec2& _other) const noexcept;
263
275 Vec2 ProjectOnToNormal(const Vec2& _normal) const noexcept;
276
284 Vec2 Perpendicular() const noexcept;
285
286//}
287
288//{ Dot/Cross
289
298 static constexpr T Dot(const Vec2& _lhs, const Vec2& _rhs) noexcept;
299
308 static constexpr T Cross(const Vec2& _lhs, const Vec2& _rhs) noexcept;
309
310//}
311
312//{ Angle
313
322 static Deg<T> Angle(const Vec2& _start, const Vec2& _end) noexcept;
323
332 static Deg<T> AngleUnsigned(const Vec2& _start, const Vec2& _end) noexcept;
333
334//}
335
336//{ Dist/Dir
337
346 static constexpr T Dist(const Vec2& _start, const Vec2& _end);
347
356 static constexpr T SqrDist(const Vec2& _start, const Vec2& _end) noexcept;
357
368 static constexpr Vec2 Dir(const Vec2& _start, const Vec2& _end) noexcept;
369
378 static constexpr Vec2 DirN(const Vec2& _start, const Vec2& _end);
379
380//}
381
382//{ Lerp
394 static Vec2 Lerp(const Vec2& _start, const Vec2& _end, float _alpha) noexcept;
395
407 static Vec2 LerpUnclamped(const Vec2& _start, const Vec2& _end, float _alpha) noexcept;
408
420 static Vec2 SLerp(const Vec2& _start, const Vec2& _end, float _alpha) noexcept;
421
433 static Vec2 SLerpUnclamped(const Vec2& _start, const Vec2& _end, float _alpha) noexcept;
434
435//}
436
437//{ Operators
438
444 Vec2& operator=(Vec2&&) = default;
445
451 Vec2& operator=(const Vec2&) = default;
452
453
459 constexpr Vec2 operator-() const noexcept;
460
468 constexpr Vec2 operator*(T _scale) const noexcept;
469
477 Vec2 operator/(T _scale) const;
478
486 constexpr Vec2 operator+(const Vec2& _rhs) const noexcept;
487
495 constexpr Vec2 operator-(const Vec2& _rhs) const noexcept;
496
504 constexpr Vec2 operator*(const Vec2& _rhs) const noexcept;
505
513 Vec2 operator/(const Vec2& _rhs) const;
514
522 constexpr T operator|(const Vec2& _rhs) const noexcept;
523
531 constexpr T operator^(const Vec2& _rhs) const noexcept;
532
533
541 Vec2& operator*=(T _scale) noexcept;
542
550 Vec2& operator/=(T _scale);
551
559 Vec2& operator+=(const Vec2& _rhs) noexcept;
560
568 Vec2& operator-=(const Vec2& _rhs) noexcept;
569
577 Vec2 operator*=(const Vec2& _rhs) noexcept;
578
586 Vec2 operator/=(const Vec2& _rhs);
587
588//}
589 };
590
591
600 template <typename T>
601 constexpr Vec2<T> operator*(typename std::remove_cv<T>::type _lhs, const Vec2<T>& _rhs) noexcept;
602
611 template <typename T>
612 Vec2<T> operator/(typename std::remove_cv<T>::type _lhs, const Vec2<T>& _rhs);
613
614
615#if SA_LOGGER_IMPL
616
628 template <typename T>
629 std::string ToString(const Vec2<T>& _v);
630
631#endif
632
633
634//{ Aliases
635
638
641
644
647
648
650 template <typename T>
652
655
658
661
664
665//}
666}
667
676#include <SA/Maths/Space/Vector2.inl>
677
678#endif // GUARD
Cosinus method implementation.
Maths module Debug compatibility definition.
Degree type implementation.
Equals method implementation.
Lerp algorithms implementation.
Square Root algorithm implementation.
Tangent method implementation.
Maths Degree type.
Definition Degree.hpp:37
Vector 2 Sapphire's class.
Definition Vector2.hpp:41
Vec2 Reflect(const Vec2 &_normal, float _elasticity=1.0f) const noexcept
Reflect this vector over the normal.
static constexpr T Dist(const Vec2 &_start, const Vec2 &_end)
Compute the Distance between _start and _end.
static Deg< T > AngleUnsigned(const Vec2 &_start, const Vec2 &_end) noexcept
Compute the Unsigned Angle between _start and _end.
Vec2(Vec2 &&)=default
Default move constructor.
constexpr Vec2(const Vec2< TIn > &_other) noexcept
Value constructor from another Vec2 type.
static const Vec2 Left
Left vector constant { -1, 0 }.
Definition Vector2.hpp:64
Vec2 ProjectOnToNormal(const Vec2 &_normal) const noexcept
Project this vector onto s normal.
static const Vec2 Up
Up vector constant { 0, 1 }.
Definition Vector2.hpp:67
T Type
Scalar type of the Vector.
Definition Vector2.hpp:43
Vec2 & Normalize()
Normalize this vector.
constexpr Vec2(const Vec3< TIn > &_other) noexcept
Value constructor from Vec3.
static constexpr T Cross(const Vec2 &_lhs, const Vec2 &_rhs) noexcept
Compute the Cross product between _lhs and _rhs.
static Vec2 Lerp(const Vec2 &_start, const Vec2 &_end, float _alpha) noexcept
Clamped Lerp from _start to _end at _alpha.
static const Vec2 Right
Right vector constant { 1, 0 }.
Definition Vector2.hpp:61
T * Data() noexcept
Getter of vector data
constexpr bool IsZero() const noexcept
Whether this vector is a zero vector.
constexpr bool Equals(const Vec2 &_other, T _epsilon=std::numeric_limits< T >::epsilon()) const noexcept
Compare 2 vector.
constexpr T SqrLength() const noexcept
Getter of the Squared Length of this vector.
Vec2()=default
Default constructor.
static const Vec2 Down
Down vector constant { 0, -1 }.
Definition Vector2.hpp:70
constexpr Vec2(T _scale) noexcept
Scale Value constructor.
Vec2 ProjectOnTo(const Vec2 &_other) const noexcept
Project this vector onto an other vector.
constexpr Vec2(T _x, T _y) noexcept
Value constructor.
Vec2 GetNormalized() const
Normalize this vector.
Vec2 Perpendicular() const noexcept
Compute the perpendicular vector to this vector.
static Deg< T > Angle(const Vec2 &_start, const Vec2 &_end) noexcept
Compute the Signed Angle between _start and _end.
static constexpr Vec2 Dir(const Vec2 &_start, const Vec2 &_end) noexcept
Compute the Non-Normalized Direction from _start to _end.
static Vec2 SLerpUnclamped(const Vec2 &_start, const Vec2 &_end, float _alpha) noexcept
Unclamped SLerp from _start to _end at _alpha.
static const Vec2 One
One vector constant { 1, 1 }.
Definition Vector2.hpp:58
static const Vec2 Zero
Zero vector constant { 0, 0 }.
Definition Vector2.hpp:55
static Vec2 SLerp(const Vec2 &_start, const Vec2 &_end, float _alpha) noexcept
Clamped SLerp from _start to _end at _alpha.
static Vec2 LerpUnclamped(const Vec2 &_start, const Vec2 &_end, float _alpha) noexcept
Unclamped Lerp from _start to _end at _alpha.
static constexpr T SqrDist(const Vec2 &_start, const Vec2 &_end) noexcept
Compute the Squared Distance between _start and _end.
Vec2(const Vec2 &)=default
Default copy constructor.
static constexpr Vec2 DirN(const Vec2 &_start, const Vec2 &_end)
Compute the Normalized Direction from _start to _end.
constexpr T Length() const
Getter of the /b length of this vector.
constexpr bool IsNormalized() const noexcept
Whether this vector is normalized.
static constexpr T Dot(const Vec2 &_lhs, const Vec2 &_rhs) noexcept
Compute the Dot product between _lhs and _rhs.
T y
Vector's Y component (axis value).
Definition Vector2.hpp:50
T x
Vector's X component (axis value).
Definition Vector2.hpp:47
Vector 3 Sapphire-Maths class.
Definition Vector3.hpp:43