18 constexpr
Vector2D(T xValue, T yValue) noexcept :
x(xValue),
y(yValue) {}
20 constexpr void set(
const T& newX,
const T& newY)
noexcept {
x = newX;
y = newY; }
21 [[nodiscard]]
constexpr T
getX() const noexcept {
return x; }
22 [[nodiscard]]
constexpr T
getY() const noexcept {
return y; }
24 [[nodiscard]]
constexpr T&
operator[](std::size_t n)
noexcept {
25 assert(n < 2 &&
"Vector2D index out of range");
26 return n == 0 ?
x :
y;
28 [[nodiscard]]
constexpr const T&
operator[](std::size_t n)
const noexcept {
29 assert(n < 2 &&
"Vector2D index out of range");
30 return n == 0 ?
x :
y;
32 [[nodiscard]]
constexpr T*
data() noexcept {
return &
x; }
33 [[nodiscard]]
constexpr const T*
data() const noexcept {
return &
x; }
42 if constexpr (std::is_integral_v<T>) {
43 assert(v.x != 0 && v.y != 0 &&
"Vector2D integer division by zero");
51 if constexpr (std::is_integral_v<T>) {
52 assert(s != 0 &&
"Vector2D integer division by zero");
62 [[nodiscard]]
constexpr T
dot(
const Vector2D& v)
const noexcept {
return x * v.x +
y * v.y; }
64 [[nodiscard]]
constexpr T
cross(
const Vector2D& v)
const noexcept {
return x * v.y -
y * v.x; }
76 if (len > std::numeric_limits<T>::epsilon())
77 *
this /=
static_cast<T
>(len);
123 return os <<
"(" << vec.
getX() <<
", " << vec.
getY() <<
")";
139 constexpr
Vector3D(T xValue, T yValue, T zValue) noexcept :
x(xValue),
y(yValue),
z(zValue) {}
141 constexpr void set(
const T& newX,
const T& newY,
const T& newZ)
noexcept {
x = newX;
y = newY;
z = newZ; }
142 [[nodiscard]]
constexpr T
getX() const noexcept {
return x; }
143 [[nodiscard]]
constexpr T
getY() const noexcept {
return y; }
144 [[nodiscard]]
constexpr T
getZ() const noexcept {
return z; }
146 [[nodiscard]]
constexpr T&
operator[](std::size_t n)
noexcept {
147 assert(n < 3 &&
"Vector3D index out of range");
148 return n == 0 ?
x : (n == 1 ?
y :
z);
150 [[nodiscard]]
constexpr const T&
operator[](std::size_t n)
const noexcept {
151 assert(n < 3 &&
"Vector3D index out of range");
152 return n == 0 ?
x : (n == 1 ?
y :
z);
154 [[nodiscard]]
constexpr T*
data() noexcept {
return &
x; }
155 [[nodiscard]]
constexpr const T*
data() const noexcept {
return &
x; }
164 if constexpr (std::is_integral_v<T>) {
165 assert(v.x != 0 && v.y != 0 && v.z != 0 &&
"Vector3D integer division by zero");
167 x /= v.x;
y /= v.y;
z /= v.z;
173 if constexpr (std::is_integral_v<T>) {
174 assert(s != 0 &&
"Vector3D integer division by zero");
176 x /= s;
y /= s;
z /= s;
184 [[nodiscard]]
constexpr T
dot(
const Vector3D& v)
const noexcept {
return x * v.x +
y * v.y +
z * v.z; }
200 const auto len =
length();
201 if (len > std::numeric_limits<T>::epsilon())
202 *
this /=
static_cast<T
>(len);
210 return Vector3D<U>(
static_cast<U
>(
x),
static_cast<U
>(
y),
static_cast<U
>(
z));
215 return Vector3D(v.x <
x ? v.x :
x, v.y <
y ? v.y :
y, v.z <
z ? v.z :
z);
220 return Vector3D(
x < v.x ? v.x :
x,
y < v.y ? v.y :
y,
z < v.z ? v.z :
z);
248 return os <<
"(" << vec.
getX() <<
", " << vec.
getY() <<
", " << vec.
getZ() <<
")";
Math helpers: constants, interpolation, random numbers, 2D and 3D vectors and matrices.
Definition Constants.hpp:5
constexpr Vector2D< T > operator-(Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
Definition Vector.hpp:104
constexpr Vector2D< T > operator*(Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
Definition Vector.hpp:106
constexpr Vector2D< T > operator/(Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
Definition Vector.hpp:108
constexpr Vector2D< T > operator+(Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
Definition Vector.hpp:102
std::ostream & operator<<(std::ostream &os, const Matrix< T, Rows, Cols > &mat)
Definition Matrix.hpp:380
2D vector; arithmetic operators work component-wise, with a vector or a scalar.
Definition Vector.hpp:14
constexpr bool operator==(const Vector2D &) const noexcept=default
constexpr Vector2D & operator-=(T s) noexcept
Definition Vector.hpp:58
constexpr Vector2D & operator+=(T s) noexcept
Definition Vector.hpp:57
constexpr Vector2D operator+() const noexcept
Definition Vector.hpp:35
constexpr Vector2D min(const Vector2D &v) const noexcept
Component-wise minimum.
Definition Vector.hpp:89
constexpr T & operator[](std::size_t n) noexcept
Definition Vector.hpp:24
constexpr T cross(const Vector2D &v) const noexcept
Z component of the 3D cross product, positive when v is counterclockwise from this vector.
Definition Vector.hpp:64
constexpr auto distanceSquared(const Vector2D &v) const noexcept
Definition Vector.hpp:70
T x
Definition Vector.hpp:15
constexpr Vector2D max(const Vector2D &v) const noexcept
Component-wise maximum.
Definition Vector.hpp:94
constexpr auto lengthSquared() const noexcept
Definition Vector.hpp:68
constexpr Vector2D & normalize() noexcept
Scales this vector to length 1; a zero vector is left unchanged.
Definition Vector.hpp:74
constexpr const T * data() const noexcept
Definition Vector.hpp:33
constexpr T * data() noexcept
Definition Vector.hpp:32
constexpr T dot(const Vector2D &v) const noexcept
Definition Vector.hpp:62
constexpr void set(const T &newX, const T &newY) noexcept
Definition Vector.hpp:20
constexpr Vector2D & operator/=(T s) noexcept
Definition Vector.hpp:50
constexpr Vector2D & operator+=(const Vector2D &v) noexcept
Definition Vector.hpp:38
constexpr Vector2D normalized() const noexcept
Definition Vector.hpp:80
constexpr Vector2D< U > to() const noexcept
Converts the components, such as v.to<int>().
Definition Vector.hpp:84
constexpr T getY() const noexcept
Definition Vector.hpp:22
constexpr Vector2D & operator-=(const Vector2D &v) noexcept
Definition Vector.hpp:39
constexpr Vector2D & operator*=(const Vector2D &v) noexcept
Definition Vector.hpp:40
constexpr Vector2D perpendicular() const noexcept
This vector rotated by 90 degrees counterclockwise.
Definition Vector.hpp:66
constexpr Vector2D() noexcept=default
constexpr auto distance(const Vector2D &v) const noexcept
Definition Vector.hpp:71
constexpr Vector2D operator-() const noexcept
Definition Vector.hpp:36
constexpr const T & operator[](std::size_t n) const noexcept
Definition Vector.hpp:28
constexpr T getX() const noexcept
Definition Vector.hpp:21
T y
Definition Vector.hpp:15
constexpr Vector2D & operator/=(const Vector2D &v) noexcept
Definition Vector.hpp:41
constexpr Vector2D & operator*=(T s) noexcept
Definition Vector.hpp:49
constexpr auto length() const noexcept
Definition Vector.hpp:69
3D vector; arithmetic operators work component-wise, with a vector or a scalar.
Definition Vector.hpp:135
constexpr auto distance(const Vector3D &v) const noexcept
Definition Vector.hpp:196
constexpr T getZ() const noexcept
Definition Vector.hpp:144
constexpr T dot(const Vector3D &v) const noexcept
Definition Vector.hpp:184
constexpr Vector3D & operator*=(const Vector3D &v) noexcept
Definition Vector.hpp:162
constexpr Vector3D & operator-=(const Vector3D &v) noexcept
Definition Vector.hpp:161
constexpr Vector3D< U > to() const noexcept
Converts the components, such as v.to<int>().
Definition Vector.hpp:209
T x
Definition Vector.hpp:136
constexpr void set(const T &newX, const T &newY, const T &newZ) noexcept
Definition Vector.hpp:141
constexpr T getX() const noexcept
Definition Vector.hpp:142
constexpr Vector3D operator+() const noexcept
Definition Vector.hpp:157
constexpr const T & operator[](std::size_t n) const noexcept
Definition Vector.hpp:150
constexpr Vector3D & operator-=(T s) noexcept
Definition Vector.hpp:180
constexpr auto distanceSquared(const Vector3D &v) const noexcept
Definition Vector.hpp:195
constexpr Vector3D & operator*=(T s) noexcept
Definition Vector.hpp:171
constexpr bool operator==(const Vector3D &) const noexcept=default
constexpr Vector3D operator-() const noexcept
Definition Vector.hpp:158
constexpr Vector3D normalized() const noexcept
Definition Vector.hpp:205
constexpr Vector3D & normalize() noexcept
Scales this vector to length 1; a zero vector is left unchanged.
Definition Vector.hpp:199
constexpr T getY() const noexcept
Definition Vector.hpp:143
constexpr Vector3D() noexcept=default
constexpr Vector3D & operator+=(const Vector3D &v) noexcept
Definition Vector.hpp:160
constexpr auto lengthSquared() const noexcept
Definition Vector.hpp:193
constexpr Vector3D cross(const Vector3D &v) const noexcept
Definition Vector.hpp:185
constexpr auto length() const noexcept
Definition Vector.hpp:194
constexpr Vector3D & operator/=(T s) noexcept
Definition Vector.hpp:172
constexpr Vector3D & operator/=(const Vector3D &v) noexcept
Definition Vector.hpp:163
constexpr Vector3D & operator+=(T s) noexcept
Definition Vector.hpp:179
constexpr const T * data() const noexcept
Definition Vector.hpp:155
T z
Definition Vector.hpp:136
constexpr Vector3D min(const Vector3D &v) const noexcept
Component-wise minimum.
Definition Vector.hpp:214
constexpr T & operator[](std::size_t n) noexcept
Definition Vector.hpp:146
constexpr Vector3D max(const Vector3D &v) const noexcept
Component-wise maximum.
Definition Vector.hpp:219
constexpr T * data() noexcept
Definition Vector.hpp:154
T y
Definition Vector.hpp:136