lxgui
Loading...
Searching...
No Matches
gui_vector2.hpp
1#ifndef LXGUI_GUI_VECTOR2_HPP
2#define LXGUI_GUI_VECTOR2_HPP
3
4#include "lxgui/lxgui.hpp"
5#include "lxgui/utils.hpp"
6
7#include <cmath>
8#include <type_traits>
9
10namespace lxgui::gui {
11
13template<typename T>
14struct vector2 {
15 using float_type = std::conditional_t<std::is_floating_point_v<T>, T, double>;
16
17 constexpr vector2() = default;
18
19 constexpr vector2(T nx, T ny) noexcept : x(nx), y(ny) {}
20
21 template<typename U>
22 explicit constexpr vector2(const vector2<U>& v) noexcept :
23 x(static_cast<T>(v.x)), y(static_cast<T>(v.y)) {}
24
25 void set(T nx, T ny) noexcept {
26 x = nx;
27 y = ny;
28 }
29
30 T get_norm_squared() const noexcept {
31 return x * x + y * y;
32 }
33
34 float_type get_norm() const noexcept {
35 return std::sqrt(static_cast<float_type>(get_norm_squared()));
36 }
37
38 vector2<float_type> get_unit() const noexcept {
39 vector2<float_type> vec(static_cast<float_type>(x), static_cast<float_type>(y));
40 const typename vector2<float_type>::float_type norm = get_norm();
41 vec.x /= norm;
42 vec.y /= norm;
43 return vec;
44 }
45
47 vector2<float_type> vec(static_cast<float_type>(-y), static_cast<float_type>(x));
48 const typename vector2<float_type>::float_type norm = get_norm();
49 vec.x /= norm;
50 vec.y /= norm;
51 return vec;
52 }
53
54 vector2 get_rotated(float_type angle) const noexcept {
55 vector2<float_type> vec(static_cast<float_type>(x), static_cast<float_type>(y));
56 vector2<float_type> orig = vec;
57
58 const float_type ca = std::cos(angle);
59 const float_type sa = std::sin(angle);
60
61 vec.x = static_cast<T>(orig.x * ca - orig.y * sa);
62 vec.y = static_cast<T>(orig.x * sa + orig.y * ca);
63
64 return vec;
65 }
66
67 vector2 get_scaled(const vector2& v) const noexcept {
68 vector2 vec = *this;
69 vec.x *= v.x;
70 vec.y *= v.y;
71 return vec;
72 }
73
74 vector2 operator+(const vector2& v) const noexcept {
75 return vector2(x + v.x, y + v.y);
76 }
77
78 vector2& operator+=(const vector2& v) noexcept {
79 x += v.x;
80 y += v.y;
81 return *this;
82 }
83
84 vector2 operator-() const noexcept {
85 return vector2(-x, -y);
86 }
87
88 vector2 operator-(const vector2& v) const noexcept {
89 return vector2(x - v.x, y - v.y);
90 }
91
92 vector2& operator-=(const vector2& v) noexcept {
93 x -= v.x;
94 y -= v.y;
95 return *this;
96 }
97
98 bool operator==(const vector2& v) const noexcept {
99 return (x == v.x) && (y == v.y);
100 }
101
102 bool operator!=(const vector2& v) const noexcept {
103 return (x != v.x) || (y != v.y);
104 }
105
106 vector2 operator*(T value) const noexcept {
107 return vector2(x * value, y * value);
108 }
109
110 vector2 operator*(const vector2& value) const noexcept {
111 return vector2(x * value.x, y * value.y);
112 }
113
114 vector2& operator*=(T value) noexcept {
115 x *= value;
116 y *= value;
117 return *this;
118 }
119
120 vector2& operator*=(const vector2& value) noexcept {
121 x *= value.x;
122 y *= value.y;
123 return *this;
124 }
125
126 vector2 operator/(T value) const noexcept {
127 return vector2(x / value, y / value);
128 }
129
130 vector2 operator/(const vector2& value) const noexcept {
131 return vector2(x / value.x, y / value.y);
132 }
133
134 vector2& operator/=(T value) noexcept {
135 x /= value;
136 y /= value;
137 return *this;
138 }
139
140 vector2& operator/=(const vector2& value) noexcept {
141 x /= value.x;
142 y /= value.y;
143 return *this;
144 }
145
146 T dot(const vector2& v) const noexcept {
147 return x * v.x + y * v.y;
148 }
149
150 static const vector2 zero;
151 static const vector2 unit;
152 static const vector2 unit_x;
153 static const vector2 unit_y;
154
155 T x = 0, y = 0;
156};
157
158template<typename T>
159constexpr vector2<T> vector2<T>::zero(0, 0);
160
161template<typename T>
162constexpr vector2<T> vector2<T>::unit(1, 1);
163
164template<typename T>
165constexpr vector2<T> vector2<T>::unit_x(1, 0);
166
167template<typename T>
168constexpr vector2<T> vector2<T>::unit_y(0, 1);
169
178
179template<typename T>
180vector2<T> operator*(T value, const vector2<T>& v) noexcept {
181 return vector2<T>(v.x * value, v.y * value);
182}
183
184template<typename O, typename T>
185O& operator<<(O& stream, const vector2<T>& v) {
186 return stream << v.x << ", " << v.y;
187}
188
189} // namespace lxgui::gui
190
191#endif
std::ostream & operator<<(std::ostream &stream, const color &c)
color operator*(const color &c1, const color &c2) noexcept
Holds 2D coordinates.
T get_norm_squared() const noexcept
vector2 operator*(T value) const noexcept
void set(T nx, T ny) noexcept
vector2 operator*(const vector2 &value) const noexcept
float_type get_norm() const noexcept
vector2< float_type > get_normal() const noexcept
static const vector2 unit_y
vector2 operator/(const vector2 &value) const noexcept
static const vector2 unit_x
constexpr vector2(T nx, T ny) noexcept
static const vector2 zero
static const vector2 unit
T dot(const vector2 &v) const noexcept
vector2< float_type > get_unit() const noexcept
vector2 & operator*=(const vector2 &value) noexcept
vector2 get_rotated(float_type angle) const noexcept
vector2 & operator/=(const vector2 &value) noexcept
vector2 & operator/=(T value) noexcept
vector2 & operator*=(T value) noexcept
constexpr vector2()=default
vector2 operator-(const vector2 &v) const noexcept
vector2 & operator+=(const vector2 &v) noexcept
vector2 & operator-=(const vector2 &v) noexcept
std::conditional_t< std::is_floating_point_v< T >, T, double > float_type
vector2 operator/(T value) const noexcept
vector2 operator-() const noexcept
constexpr vector2(const vector2< U > &v) noexcept
vector2 operator+(const vector2 &v) const noexcept
bool operator!=(const vector2 &v) const noexcept
vector2 get_scaled(const vector2 &v) const noexcept
bool operator==(const vector2 &v) const noexcept