Ginger 1.1.5; VERSION ${PROJECT_VERSION}
Loading...
Searching...
No Matches
vector2.hpp
Go to the documentation of this file.
1#pragma once
2
3#if __cpp_constexpr >= 201304
4# define CONSTEXPR14 constexpr
5#else
6# define CONSTEXPR14 inline
7#endif
8
9namespace ginger {
10
19 template <typename T1, typename T2 = T1> class Vector2 {
20 public:
21 T1 _x;
22 T2 _y;
23
30 constexpr Vector2() : _x{0}, _y{0} {}
31
44 constexpr Vector2(T1 x, T2 y) noexcept : _x{x}, _y{y} {}
45
58 // constexpr Vector2(const T1 &x, const T2 &y) : _x{x}, _y{y} {}
59
66 template <typename U1, typename U2> constexpr explicit Vector2(const Vector2<U1, U2>& other)
67 : _x(other.x()), _y(other.y()) {}
68
74 constexpr auto x() const noexcept -> const T1& { return this->_x; }
75
81 constexpr auto y() const noexcept -> const T2& { return this->_y; }
82
83 // /**
84 // * @brief
85 // *
86 // * @return double
87 // */
88 // constexpr auto norm_inf() const -> double {
89 // return std::max(std::abs(this->_x), std::abs(this->_y));
90 // }
91
106 template <typename U1, typename U2> //
107 constexpr auto dot(const Vector2<U1, U2>& other) const -> double {
108 return this->_x * other._x + this->_y * other._y;
109 }
110
125 template <typename U1, typename U2> //
126 constexpr auto cross(const Vector2<U1, U2>& other) const -> double {
127 return this->_x * other._y - other._x * this->_y;
128 }
129
134
141 constexpr auto operator-() const -> Vector2<T1, T2> { return {-this->_x, -this->_y}; }
142
154 template <typename U1, typename U2> inline auto operator+=(const Vector2<U1, U2>& other)
155 -> Vector2<T1, T2>& {
156 this->_x += other.x();
157 this->_y += other.y();
158 return *this;
159 }
160
172 template <typename U1, typename U2> //
173 inline auto operator-=(const Vector2<U1, U2>& other) -> Vector2<T1, T2>& {
174 this->_x -= other.x();
175 this->_y -= other.y();
176 return *this;
177 }
178
187 template <typename R> inline auto operator*=(const R& alpha) -> Vector2<T1, T2>& {
188 this->_x *= alpha;
189 this->_y *= alpha;
190 return *this;
191 }
192
202 template <typename R> inline auto operator/=(const R& alpha) -> Vector2<T1, T2>& {
203 this->_x /= alpha;
204 this->_y /= alpha;
205 return *this;
206 }
207
220 template <typename U1, typename U2> //
221 friend constexpr auto operator+(Vector2<T1, T2> x, const Vector2<U1, U2>& y)
222 -> Vector2<T1, T2> {
223 return x += y;
224 }
225
238 template <typename U1, typename U2> //
239 friend constexpr auto operator-(Vector2<T1, T2> x, const Vector2<U1, U2>& y)
240 -> Vector2<T1, T2> {
241 return x -= y;
242 }
243
254 template <typename R> friend constexpr auto operator*(Vector2<T1, T2> x, const R& alpha)
255 -> Vector2<T1, T2> {
256 return x *= alpha;
257 }
258
269 template <typename R> friend constexpr auto operator*(const R& alpha, Vector2<T1, T2> x)
270 -> Vector2<T1, T2> {
271 return x *= alpha;
272 }
273
284 template <typename R> friend constexpr auto operator/(Vector2<T1, T2> x, const R& alpha)
285 -> Vector2<T1, T2> {
286 return x /= alpha;
287 }
288
290
303 template <class Stream> friend auto operator<<(Stream& out, const Vector2<T1, T2>& vec)
304 -> Stream& {
305 out << "{" << vec.x() << ", " << vec.y() << "}";
306 return out;
307 }
308 };
309} // namespace ginger
Vector2.
Definition vector2.hpp:19
auto operator/=(const R &alpha) -> Vector2< T1, T2 > &
Definition vector2.hpp:202
friend auto operator<<(Stream &out, const Vector2< T1, T2 > &vec) -> Stream &
Definition vector2.hpp:303
constexpr auto operator-() const -> Vector2< T1, T2 >
Definition vector2.hpp:141
friend constexpr auto operator*(const R &alpha, Vector2< T1, T2 > x) -> Vector2< T1, T2 >
Definition vector2.hpp:269
constexpr auto cross(const Vector2< U1, U2 > &other) const -> double
Definition vector2.hpp:126
auto operator+=(const Vector2< U1, U2 > &other) -> Vector2< T1, T2 > &
Definition vector2.hpp:154
friend constexpr auto operator/(Vector2< T1, T2 > x, const R &alpha) -> Vector2< T1, T2 >
Definition vector2.hpp:284
auto operator-=(const Vector2< U1, U2 > &other) -> Vector2< T1, T2 > &
Definition vector2.hpp:173
constexpr Vector2(T1 x, T2 y) noexcept
Construct a new Vector2 object.
Definition vector2.hpp:44
constexpr auto y() const noexcept -> const T2 &
Definition vector2.hpp:81
friend constexpr auto operator*(Vector2< T1, T2 > x, const R &alpha) -> Vector2< T1, T2 >
Definition vector2.hpp:254
friend constexpr auto operator+(Vector2< T1, T2 > x, const Vector2< U1, U2 > &y) -> Vector2< T1, T2 >
Definition vector2.hpp:221
constexpr auto dot(const Vector2< U1, U2 > &other) const -> double
Definition vector2.hpp:107
T2 _y
Definition vector2.hpp:22
T1 _x
Definition vector2.hpp:21
friend constexpr auto operator-(Vector2< T1, T2 > x, const Vector2< U1, U2 > &y) -> Vector2< T1, T2 >
Definition vector2.hpp:239
constexpr Vector2(const Vector2< U1, U2 > &other)
Construct a new Vector2 object.
Definition vector2.hpp:66
constexpr auto x() const noexcept -> const T1 &
Definition vector2.hpp:74
constexpr Vector2()
Construct a new Vector2 object.
Definition vector2.hpp:30
auto operator*=(const R &alpha) -> Vector2< T1, T2 > &
Definition vector2.hpp:187
Definition logger.hpp:10