![]() |
Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
|
General purpose math vector. More...
#include <gf/Vector.h>
Public Member Functions | |
Vector ()=default | |
Default constructor. More... | |
constexpr | Vector (ZeroType) noexcept |
Constructor that zero the vector out. More... | |
Vector (T val) noexcept | |
Constructor that fills the vector with a value. More... | |
Vector (const T *array) | |
Constructor that takes an array. More... | |
Vector (std::initializer_list< T > list) noexcept | |
Constructor that takes an initializer list. More... | |
Vector (const Vector &other)=default | |
Default copy constructor. More... | |
template<typename U > | |
Vector (const Vector< U, N > &other) noexcept | |
Converting copy constructor. More... | |
Vector & | operator= (const Vector &other)=default |
Default copy assignment. More... | |
constexpr T | operator[] (std::size_t i) const |
Access to the \( i \)-th coordinate. More... | |
constexpr T & | operator[] (std::size_t i) |
Access to the \( i \)-th coordinate. More... | |
T * | begin () |
Iterator.to the first element. More... | |
T * | end () |
Iterator to the element after the last one. More... | |
const T * | begin () const |
Iterator.to the first element (const version). More... | |
const T * | end () const |
Iterator on the element after the last one (const version). More... | |
const T * | cbegin () const |
Iterator.on the first element (const version). More... | |
const T * | cend () const |
Iterator on the element after the last one (const version). More... | |
constexpr void | zero () noexcept |
Zero out the vector. More... | |
Public Attributes | |
T | data [N] |
The internal representation of the vector. More... | |
Related Functions | |
(Note that these are not member functions.) | |
template<typename T , std::size_t N> | |
void | swap (Vector< T, N > &lhs, Vector< T, N > &rhs) noexcept |
Swap two vectors. More... | |
template<typename ... Types> | |
constexpr auto | vec (Types... values) -> Vector< std::common_type_t< Types... >, sizeof...(Types)> |
Universal vector factory. More... | |
template<typename T , std::size_t N> | |
constexpr bool | operator== (Vector< T, N > lhs, Vector< T, N > rhs) |
Equality operator between two vectors. More... | |
template<typename T , std::size_t N> | |
constexpr bool | operator!= (Vector< T, N > lhs, Vector< T, N > rhs) |
Inequality operator between two vectors. More... | |
template<typename T , std::size_t N> | |
constexpr Vector< T, N > | operator- (Vector< T, N > val) |
Component-wise unary minus. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator+ (Vector< T, N > lhs, Vector< U, N > rhs) |
Component-wise addition. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< T, N > & | operator+= (Vector< T, N > &lhs, Vector< U, N > rhs) |
Component-wise addition and assignment. More... | |
template<typename T , typename U , std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<U>::value, U>::type> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator+ (Vector< T, N > lhs, U rhs) |
Right scalar addition. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< T, N > & | operator+= (Vector< T, N > &lhs, U rhs) |
Right scalar addition and assignment. More... | |
template<typename T , typename U , std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator+ (T lhs, Vector< U, N > rhs) |
Left scalar addition. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator- (Vector< T, N > lhs, Vector< U, N > rhs) |
Component-wise substraction. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< T, N > & | operator-= (Vector< T, N > &lhs, Vector< U, N > rhs) |
Component-wise substraction and assignment. More... | |
template<typename T , typename U , std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<U>::value, U>::type> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator- (Vector< T, N > lhs, U rhs) |
Right scalar substraction. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< T, N > & | operator-= (Vector< T, N > &lhs, U rhs) |
Right scalar substraction and assignment. More... | |
template<typename T , typename U , std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator- (T lhs, Vector< U, N > rhs) |
Left scalar substraction. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator* (Vector< T, N > lhs, Vector< U, N > rhs) |
Component-wise multiplication. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< T, N > & | operator*= (Vector< T, N > &lhs, Vector< U, N > rhs) |
Component-wise multiplication and assignment. More... | |
template<typename T , typename U , std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<U>::value, U>::type> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator* (Vector< T, N > lhs, U rhs) |
Right scalar multiplication. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< T, N > & | operator*= (Vector< T, N > &lhs, U rhs) |
Right scalar multiplication and assignment. More... | |
template<typename T , typename U , std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator* (T lhs, Vector< U, N > rhs) |
Left scalar multiplication. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator/ (Vector< T, N > lhs, Vector< U, N > rhs) |
Component-wise division. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< T, N > & | operator/= (Vector< T, N > &lhs, Vector< U, N > rhs) |
Component-wise division and assignment. More... | |
template<typename T , typename U , std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<U>::value, U>::type> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator/ (Vector< T, N > lhs, U rhs) |
Right scalar division. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< T, N > & | operator/= (Vector< T, N > &lhs, U rhs) |
Right scalar division and assignment. More... | |
template<typename T , typename U , std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> | |
constexpr Vector< std::common_type_t< T, U >, N > | operator/ (T lhs, Vector< U, N > rhs) |
Left scalar division. More... | |
template<std::size_t N> | |
constexpr Vector< bool, N > | operator|| (Vector< bool, N > lhs, Vector< bool, N > rhs) |
Component-wise logical or operator. More... | |
template<std::size_t N> | |
constexpr Vector< bool, N > | operator&& (Vector< bool, N > lhs, Vector< bool, N > rhs) |
Component-wise logical and operator. More... | |
template<typename T , std::size_t N> | |
constexpr T | dot (Vector< T, N > lhs, Vector< T, N > rhs) |
Scalar product. More... | |
template<typename T , std::size_t N> | |
constexpr Vector< T, N > | min (Vector< T, N > lhs, Vector< T, N > rhs) |
Component-wise minimum. More... | |
template<typename T , std::size_t N> | |
constexpr Vector< T, N > | max (Vector< T, N > lhs, Vector< T, N > rhs) |
Component-wise maximum. More... | |
template<typename T , std::size_t N> | |
Vector< T, N > | abs (Vector< T, N > val) |
Component-wise absolute value. More... | |
template<typename T , std::size_t N> | |
constexpr Vector< int, N > | sign (Vector< T, N > val) |
Component-wise sign value. More... | |
template<typename T , std::size_t N> | |
constexpr Vector< bool, N > | equals (Vector< T, N > lhs, Vector< T, N > rhs) |
Component-wise equality operator. More... | |
template<typename T , std::size_t N> | |
constexpr Vector< bool, N > | lessThan (Vector< T, N > lhs, Vector< T, N > rhs) |
Component-wise comparison operator. More... | |
template<typename T , std::size_t N> | |
constexpr Vector< bool, N > | greaterThan (Vector< T, N > lhs, Vector< T, N > rhs) |
Component-wise comparison operator. More... | |
template<typename T , std::size_t N> | |
constexpr Vector< T, N > | select (Vector< bool, N > cond, Vector< T, N > lhs, Vector< T, N > rhs) |
Component-wise selection operator. More... | |
template<typename T , std::size_t N> | |
constexpr Vector< T, N > | clamp (Vector< T, N > val, Vector< T, N > lo, Vector< T, N > hi) |
Component-wise clamp function. More... | |
template<typename T , std::size_t N> | |
constexpr Vector< T, N > | clamp (Vector< T, N > val, T lo, T hi) |
Component-wise clamp function. More... | |
template<typename T , typename U , std::size_t N> | |
constexpr Vector< T, N > | lerp (Vector< T, N > lhs, Vector< T, N > rhs, U t) |
Component-wise lerp function. More... | |
template<typename T , std::size_t N> | |
T | manhattanLength (Vector< T, N > vec) |
Manhattan length of a vector. More... | |
template<typename T , std::size_t N> | |
constexpr T | squareLength (Vector< T, N > vec) |
Square Euclidean length of a vector. More... | |
template<typename T , std::size_t N> | |
T | euclideanLength (Vector< T, N > vec) |
Euclidean length of a vector. More... | |
template<typename T , std::size_t N> | |
T | chebyshevLength (Vector< T, N > vec) |
Chebyshev length of a vector. More... | |
template<typename T , std::size_t N> | |
T | naturalLength (Vector< T, N > vec) |
Natural length of a vector. More... | |
template<typename T , std::size_t N> | |
T | manhattanDistance (Vector< T, N > lhs, Vector< T, N > rhs) |
Manhattan distance between two vectors. More... | |
template<typename T , std::size_t N> | |
constexpr T | squareDistance (Vector< T, N > lhs, Vector< T, N > rhs) |
Square Euclidean distance between two vectors. More... | |
template<typename T , std::size_t N> | |
T | euclideanDistance (Vector< T, N > lhs, Vector< T, N > rhs) |
Euclidean distance between two vectors. More... | |
template<typename T , std::size_t N> | |
T | chebyshevDistance (Vector< T, N > lhs, Vector< T, N > rhs) |
Chebyshev distance between two vectors. More... | |
template<typename T , std::size_t N> | |
T | naturalDistance (Vector< T, N > lhs, Vector< T, N > rhs) |
Natural distance between two vectors. More... | |
template<typename T , std::size_t N> | |
Vector< T, N > | normalize (Vector< T, N > vec) |
Normalize a vector. More... | |
template<typename T > | |
constexpr Vector< T, 2 > | projx (Vector< T, 2 > vec) |
Project the vector on the x axis. More... | |
template<typename T > | |
constexpr Vector< T, 2 > | projy (Vector< T, 2 > vec) |
Project the vector on the y axis. More... | |
template<typename T > | |
constexpr Vector< T, 2 > | dirx (T length) |
Create a vector in the x direction. More... | |
template<typename T > | |
constexpr Vector< T, 2 > | diry (T length) |
Create a vector in the y direction. More... | |
template<typename T > | |
Vector< T, 2 > | unit (T angle) |
Unit vector in a specified direction. More... | |
template<typename T > | |
float | angle (Vector< T, 2 > vec) |
Angle of a vector relative to the x-axis. More... | |
template<typename T > | |
constexpr Vector< T, 2 > | perp (Vector< T, 2 > vec) |
Perpendicular vector. More... | |
template<typename T > | |
constexpr Vector< T, 2 > | vectorTripleProduct (Vector< T, 2 > a, Vector< T, 2 > b, Vector< T, 2 > c) |
Regular vector triple product. More... | |
template<typename T > | |
constexpr Vector< T, 2 > | inverseVectorTripleProduct (Vector< T, 2 > a, Vector< T, 2 > b, Vector< T, 2 > c) |
Inverse vector triple product. More... | |
template<typename T > | |
constexpr T | cross (Vector< T, 2 > lhs, Vector< T, 2 > rhs) |
Cross product for 2D vectors. More... | |
template<typename T > | |
constexpr Vector< T, 3 > | cross (Vector< T, 3 > lhs, Vector< T, 3 > rhs) |
Cross product for 3D vectors. More... | |
template<typename T > | |
float | angleTo (Vector< T, 2 > lhs, Vector< T, 2 > rhs) |
Angle between two vectors. More... | |
template<typename T > | |
constexpr Vector< T, 2 > | projectOnto (Vector< T, 2 > vec, Vector< T, 2 > axis) |
Projection of a vector onto an axis. More... | |
template<typename Archive , typename T , std::size_t N> | |
Archive & | operator| (Archive &ar, Vector< T, N > &vec) |
Serialize and deserialize a vector. More... | |
General purpose math vector.
gf::Vector is a class that represents an element of a N
-dimensional space. It is used throughout the library for different purposes.
The template parameter T
is the type of coordinates. . It can be any type that supports arithmetic operations (+, -, *, /) and relational operators (==, !=, <, >).
Several specializations are defined for common use cases:
This class was designed according to the article On Vector Math Libraries by Nathan Reed.
|
default |
Default constructor.
This constructor is defaulted so that this type is trivial.
|
inlineconstexprnoexcept |
Constructor that zero the vector out.
|
inlineexplicitnoexcept |
Constructor that fills the vector with a value.
This constructor takes a value and fills the entire vector with this value. Care must be taken when calling this constructor:
val | The value to fill the vector with |
|
inlineexplicit |
Constructor that takes an array.
This constructor can ease conversion from other math libraries. The array must contain enough data for N
dimensions.
array | An array with the values of the vector |
|
inlinenoexcept |
Constructor that takes an initializer list.
This constructor allows to use an initializer list to define the coordinates of the vector.
list | An initializer list. |
|
default |
Default copy constructor.
This constructor is defaulted so that this type is trivial.
other | The vector to copy from |
|
inlinenoexcept |
Converting copy constructor.
other | The vector to copy from |
|
inline |
Iterator.to the first element.
|
inline |
Iterator.to the first element (const version).
|
inline |
Iterator.on the first element (const version).
|
inline |
Iterator on the element after the last one (const version).
|
inline |
Iterator to the element after the last one.
|
inline |
Iterator on the element after the last one (const version).
|
default |
Default copy assignment.
This operator is defaulted so that this type is trivial.
|
inlineconstexpr |
|
inlineconstexpr |
Access to the \( i \)-th coordinate.
i | the coordinate number |
|
inlineconstexprnoexcept |
Zero out the vector.
Component-wise absolute value.
|
related |
Angle of a vector relative to the x-axis.
vec | The vector |
Angle between two vectors.
lhs | A first vector |
rhs | A second vector |
|
related |
Chebyshev distance between two vectors.
The Chebyshev distance between two vectors is the Chebyshev length of the difference of the two vectors.
lhs | A first vector |
rhs | A second vector |
|
related |
Chebyshev length of a vector.
The Chebyshev length \( \|\mathbf{u}\|_{\infty} \) of a vector \( \mathbf{u} = (u_1, \ldots, u_N) \) is:
\[ \|u\|_{\infty} = \max_{i = 1}^{N} |u_i| \]
The Chebyshev length is also called the infinity norm or maximum norm.
vec | A vector. |
|
related |
Component-wise clamp function.
Relative to two values.
|
related |
Component-wise clamp function.
Relative to two other vectors.
Cross product for 2D vectors.
The cross product of 2D vectors is not really a cross product, it is the magnitude of the vector resulting from a 3D cross product of 2D vectors with \( z = 0 \). The cross product \( \mathbf{a} \times \mathbf{b} \) of the vectors \( \mathbf{a} \) and \( \mathbf{b} \) is:
\[ \mathbf{a} \times \mathbf{b} = \mathbf{a}^{\perp} \cdot \mathbf{b} \]
The 2D cross product is also known as the perp dot product or wedge product.
lhs | A first vector |
rhs | A second vector |
|
related |
Cross product for 3D vectors.
lhs | A first vector |
rhs | A second vector |
|
related |
Create a vector in the x direction.
length | The length of the vector |
|
related |
Create a vector in the y direction.
length | The length of the vector |
|
related |
Scalar product.
The scalar product or dot product of two vectors \( \mathbf{a} = (a_1, \ldots, a_N) \) and \( \mathbf{b} = (b_1, \ldots, b_N) \) is:
\[ \mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^{N} a_i b_i = a_1 b_1 + \ldots + a_N b_N \]
|
related |
Component-wise equality operator.
|
related |
Euclidean distance between two vectors.
The Euclidean distance between two vectors is the Euclidean length of the difference of the two vectors.
lhs | A first vector |
rhs | A second vector |
|
related |
Euclidean length of a vector.
The Euclidean length \( \|\mathbf{u}\|_2 \) of a vector \( \mathbf{u} = (u_1, \ldots, u_N) \) is:
\[ \|\mathbf{u}\|_2 = \sqrt{\sum_{i = 1}^{N} u_i^2} \]
The Euclidean length is also called the 2-norm.
vec | A vector. |
|
related |
Component-wise comparison operator.
|
related |
Inverse vector triple product.
The inverse vector triple product of vectors \( \mathbf{a} \), \( \mathbf{b} \) and \( \mathbf{c} \) is:
\[ (\mathbf{a} \times \mathbf{b}) \times \mathbf{c} = -\mathbf{c} \times (\mathbf{a} \times \mathbf{b}) = -(\mathbf{c} \cdot \mathbf{b}) \mathbf{a} + (\mathbf{c} \cdot \mathbf{a}) \mathbf{b} \]
|
related |
Component-wise lerp function.
|
related |
Component-wise comparison operator.
|
related |
Manhattan distance between two vectors.
The Manhattan distance between two vectors is the Manhattan length of the difference of the two vectors.
lhs | A first vector |
rhs | A second vector |
|
related |
Manhattan length of a vector.
The Manhattan length \( \|\mathbf{u}\|_1 \) of a vector \( \mathbf{u} = (u_1, \ldots, u_N) \) is:
\[ \|\mathbf{u}\|_1 = \sum_{i = 1}^{N} |u_i| \]
The Manhattan length is also called the 1-norm.
vec | A vector. |
|
related |
Component-wise maximum.
|
related |
Component-wise minimum.
|
related |
Natural distance between two vectors.
The natural distance between two vectors is the natural length of the difference of the two vectors.
lhs | A first vector |
rhs | A second vector |
|
related |
Natural length of a vector.
The natural length \( L \) of a vector: \( \mathbf{u} = (u_1, \ldots, u_N) \) is:
\[ L = \|\mathbf{u}\|_1 + \|\mathbf{u}\|_2^2 = \sum_{i = 1}^{N} |u_i| + \sum_{i = 1}^{N} u_i^2 \]
It's the sum of the Manhattan length and the square length.
vec | A vector. |
Normalize a vector.
The normalized vector of \( \mathbf{u} \) is a vector in the same direction but with a (euclidean) length of 1:
\[ \frac{\mathbf{u}}{\|\mathbf{u}\|_2} \]
A normalized vector is also called a unit vector.
vec | A vector |
|
related |
Inequality operator between two vectors.
|
related |
Component-wise logical and operator.
|
related |
Left scalar multiplication.
|
related |
Right scalar multiplication.
|
related |
Component-wise multiplication.
|
related |
Right scalar multiplication and assignment.
|
related |
Component-wise multiplication and assignment.
|
related |
Left scalar addition.
|
related |
Right scalar addition.
|
related |
Component-wise addition.
|
related |
Right scalar addition and assignment.
|
related |
Component-wise addition and assignment.
|
related |
Left scalar substraction.
|
related |
Right scalar substraction.
|
related |
Component-wise substraction.
|
related |
Component-wise unary minus.
|
related |
Right scalar substraction and assignment.
|
related |
Component-wise substraction and assignment.
|
related |
Left scalar division.
|
related |
Right scalar division.
|
related |
Component-wise division.
|
related |
Right scalar division and assignment.
|
related |
Component-wise division and assignment.
|
related |
Equality operator between two vectors.
|
related |
Serialize and deserialize a vector.
ar | The archive |
vec | The vector to serialize |
|
related |
Component-wise logical or operator.
Perpendicular vector.
The perpendicular vector \( \mathbf{u}^{\perp} \) of vector \( \mathbf{u} = (x, y) \) is:
\[ \mathbf{u}^{\perp} = (-y, x) \]
vec | A vector |
|
related |
Projection of a vector onto an axis.
vec | The vector to project |
axis | The vector being projected onto |
vec
onto axis
Project the vector on the x axis.
vec | A vector |
Project the vector on the y axis.
vec | A vector |
|
related |
Component-wise selection operator.
Component-wise sign value.
|
related |
Square Euclidean distance between two vectors.
The square Euclidean distance between two vectors is the square Euclidean length of the difference of the two vectors.
lhs | A first vector |
rhs | A second vector |
|
related |
Square Euclidean length of a vector.
The square Euclidean length \( \|\mathbf{u}\|_2^2 \) of a vector \( \mathbf{u} = (u_1, \ldots, u_N) \) is:
\[ \|\mathbf{u}\|_2^2 = \sum_{i = 1}^{N} u_i^2 \]
vec | A vector. |
|
related |
Swap two vectors.
|
related |
Unit vector in a specified direction.
angle | The angle of the direction |
|
related |
Universal vector factory.
values | The values for the vector |
|
related |
Regular vector triple product.
The regular vector triple product of vectors \( \mathbf{a} \), \( \mathbf{b} \) and \( \mathbf{c} \) is:
\[ \mathbf{a} \times (\mathbf{b} \times \mathbf{c}) = (\mathbf{a} \cdot \mathbf{c}) \mathbf{b} - (\mathbf{a} \cdot \mathbf{b}) \mathbf{c} \]
T gf::Vector< T, N >::data[N] |
The internal representation of the vector.
A vector is represented with an array of N
values of type T
. It can be accessed directly, like an array or like a pointer, which can ease interoperability with other libraries.