Recti 1.2.4
Loading...
Searching...
No Matches
vector2.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <tuple> // import std::tie()
8#include <utility> // import std::move
9
10#if __cpp_constexpr >= 201304
11# define CONSTEXPR14 constexpr
12#else
13# define CONSTEXPR14 inline
14#endif
15
16namespace recti {
17
28 template <typename T1, typename T2 = T1> class Vector2 {
29 private:
30 T1 _x;
31 T2 _y;
32
33 public:
46 constexpr Vector2(T1 vec_x, T2 vec_y) noexcept
47 : _x{std::move(vec_x)}, _y{std::move(vec_y)} {}
48
61 template <typename U1, typename U2>
62 constexpr explicit Vector2(const Vector2<U1, U2>& other_vector)
63 : _x(other_vector.x()), _y(other_vector.y()) {}
64
72 constexpr auto x() const noexcept -> const T1& { return this->_x; }
73
81 constexpr auto y() const noexcept -> const T2& { return this->_y; }
82
95 template <typename U1, typename U2> //
96 constexpr auto cross(const Vector2<U1, U2>& other_vector) const {
97 return this->_x * other_vector._y - other_vector._x * this->_y;
98 }
99
104
117 template <typename U1, typename U2> //
118 constexpr auto operator==(const Vector2<U1, U2>& other_vector) const -> bool {
119 return std::tie(this->x(), this->y()) == std::tie(other_vector.x(), other_vector.y());
120 }
121
134 template <typename U1, typename U2> //
135 constexpr auto operator!=(const Vector2<U1, U2>& other_vector) const -> bool {
136 return !(*this == other_vector);
137 }
138
140
145
151 constexpr auto operator-() const -> Vector2 { return Vector2(-this->_x, -this->_y); }
152
161 template <typename U1, typename U2>
162 CONSTEXPR14 auto operator+=(const Vector2<U1, U2>& other_vector) -> Vector2& {
163 this->_x += other_vector.x();
164 this->_y += other_vector.y();
165 return *this;
166 }
167
176 template <typename U1, typename U2> //
177 CONSTEXPR14 auto operator-=(const Vector2<U1, U2>& other_vector) -> Vector2& {
178 this->_x -= other_vector.x();
179 this->_y -= other_vector.y();
180 return *this;
181 }
182
190 template <typename R> CONSTEXPR14 auto operator*=(const R& scalar) -> Vector2& {
191 this->_x *= scalar;
192 this->_y *= scalar;
193 return *this;
194 }
195
203 template <typename R> CONSTEXPR14 auto operator/=(const R& scalar) -> Vector2& {
204 this->_x /= scalar;
205 this->_y /= scalar;
206 return *this;
207 }
208
218 template <typename U1, typename U2> //
219 friend constexpr auto operator+(Vector2 right_vector, const Vector2<U1, U2>& left_vector)
220 -> Vector2 {
221 return right_vector += left_vector;
222 }
223
233 template <typename U1, typename U2> //
234 friend constexpr auto operator-(Vector2 right_vector, const Vector2<U1, U2>& left_vector)
235 -> Vector2 {
236 return right_vector -= left_vector;
237 }
238
247 template <typename R> friend constexpr auto operator*(Vector2 right_vector, const R& scalar)
248 -> Vector2 {
249 return right_vector *= scalar;
250 }
251
260 template <typename R> friend constexpr auto operator*(const R& scalar, Vector2 left_vector)
261 -> Vector2 {
262 return left_vector *= scalar;
263 }
264
273 template <typename R> friend constexpr auto operator/(Vector2 right_vector, const R& scalar)
274 -> Vector2 {
275 return right_vector /= scalar;
276 }
277
279
288 template <class Stream> friend auto operator<<(Stream& output_stream, const Vector2& vector)
289 -> Stream& {
290 output_stream << "{" << vector.x() << ", " << vector.y() << "}";
291 return output_stream;
292 }
293 };
294} // namespace recti
2D vector template class.
Definition vector2.hpp:28
friend constexpr auto operator*(const R &scalar, Vector2 left_vector) -> Vector2
Multiply the components of the given vector by the scalar.
Definition vector2.hpp:260
CONSTEXPR14 auto operator*=(const R &scalar) -> Vector2 &
Multiply the components of this vector by the given scalar.
Definition vector2.hpp:190
friend constexpr auto operator+(Vector2 right_vector, const Vector2< U1, U2 > &left_vector) -> Vector2
Add the components of the given vector to this vector.
Definition vector2.hpp:219
constexpr auto x() const noexcept -> const T1 &
Returns a const reference to the x-coordinate of the vector.
Definition vector2.hpp:72
constexpr auto y() const noexcept -> const T2 &
Returns a const reference to the y-coordinate of the vector.
Definition vector2.hpp:81
constexpr auto operator==(const Vector2< U1, U2 > &other_vector) const -> bool
Compares two Vector2 objects for equality.
Definition vector2.hpp:118
constexpr auto cross(const Vector2< U1, U2 > &other_vector) const
Calculates the cross product of two Vector2 objects.
Definition vector2.hpp:96
friend constexpr auto operator-(Vector2 right_vector, const Vector2< U1, U2 > &left_vector) -> Vector2
Subtract the components of the given vector from this vector.
Definition vector2.hpp:234
CONSTEXPR14 auto operator+=(const Vector2< U1, U2 > &other_vector) -> Vector2 &
Add the components of the given vector to this vector.
Definition vector2.hpp:162
friend constexpr auto operator*(Vector2 right_vector, const R &scalar) -> Vector2
Multiply the components of this vector by the given scalar.
Definition vector2.hpp:247
constexpr Vector2(T1 vec_x, T2 vec_y) noexcept
Construct a new Vector2 object.
Definition vector2.hpp:46
CONSTEXPR14 auto operator/=(const R &scalar) -> Vector2 &
Divide the components of this vector by the given scalar.
Definition vector2.hpp:203
friend constexpr auto operator/(Vector2 right_vector, const R &scalar) -> Vector2
Divide the components of this vector by the given scalar.
Definition vector2.hpp:273
constexpr auto operator!=(const Vector2< U1, U2 > &other_vector) const -> bool
Compares two Vector2 objects for inequality.
Definition vector2.hpp:135
CONSTEXPR14 auto operator-=(const Vector2< U1, U2 > &other_vector) -> Vector2 &
Subtract the components of the given vector from this vector.
Definition vector2.hpp:177
constexpr Vector2(const Vector2< U1, U2 > &other_vector)
Construct a new Vector2 object.
Definition vector2.hpp:62
constexpr auto operator-() const -> Vector2
Negate the vector, returning a new vector with the negated components.
Definition vector2.hpp:151
friend auto operator<<(Stream &output_stream, const Vector2 &vector) -> Stream &
Overload the stream insertion operator to print a Vector2 object.
Definition vector2.hpp:288
Definition svg_utils.hpp:12
#define CONSTEXPR14
Definition vector2.hpp:13