Gamedev Framework (gf)
0.3.0
A C++11 framework for 2D games
|
General purpose math vector. More...
#include <gf/Vector.h>
Public Member Functions | |
Vector ()=default | |
Default constructor. More... | |
Vector (T val) | |
Constructor that fills the vector with a value. More... | |
Vector (T *array) | |
Constructor that takes an array. More... | |
Vector (std::initializer_list< T > list) | |
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) | |
Converting copy constructor. More... | |
Vector & | operator= (const Vector &other)=default |
Default copy assignment. More... | |
T | operator[] (std::size_t i) const |
Access to the \( i \)-th coordinate. More... | |
T & | operator[] (std::size_t i) |
Access to the \( i \)-th coordinate. More... | |
T * | begin (void) |
Iterator.to the first element. More... | |
T * | end (void) |
Iterator to the element after the last one. More... | |
T const * | begin (void) const |
Iterator.to the first element (const version). More... | |
T const * | end (void) const |
Iterator on the element after the last one (const version). More... | |
T const * | cbegin (void) const |
Iterator.on the first element (const version). More... | |
T const * | cend (void) const |
Iterator on the element after the last one (const version). 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> | |
bool | operator== (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Equality operator between two vectors. More... | |
template<typename T , std::size_t N> | |
bool | operator!= (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Inequality operator between two vectors. More... | |
template<typename T , std::size_t N> | |
Vector< T, N > | operator- (const Vector< T, N > &val) |
Component-wise unary minus. More... | |
template<typename T , typename U , std::size_t N> | |
Vector< typename std::common_type< T, U >::type, N > | operator+ (const Vector< T, N > &lhs, const Vector< U, N > &rhs) |
Component-wise addition. More... | |
template<typename T , typename U , std::size_t N> | |
Vector< T, N > & | operator+= (Vector< T, N > &lhs, const 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> | |
Vector< typename std::common_type< T, U >::type, N > | operator+ (const Vector< T, N > &lhs, U rhs) |
Right scalar addition. More... | |
template<typename T , typename U , std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> | |
Vector< typename std::common_type< T, U >::type, N > | operator+ (T lhs, const Vector< U, N > &rhs) |
Left scalar addition. More... | |
template<typename T , typename U , std::size_t N> | |
Vector< typename std::common_type< T, U >::type, N > | operator- (const Vector< T, N > &lhs, const Vector< U, N > &rhs) |
Component-wise substraction. More... | |
template<typename T , typename U , std::size_t N> | |
Vector< T, N > & | operator-= (Vector< T, N > &lhs, const 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> | |
Vector< typename std::common_type< T, U >::type, N > | operator- (const Vector< T, N > &lhs, U rhs) |
Right scalar substraction. More... | |
template<typename T , typename U , std::size_t N, typename E = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> | |
Vector< typename std::common_type< T, U >::type, N > | operator- (T lhs, const Vector< U, N > &rhs) |
Left scalar substraction. More... | |
template<typename T , typename U , std::size_t N> | |
Vector< typename std::common_type< T, U >::type, N > | operator* (const Vector< T, N > &lhs, const Vector< U, N > &rhs) |
Component-wise multiplication. More... | |
template<typename T , typename U , std::size_t N> | |
Vector< T, N > & | operator*= (Vector< T, N > &lhs, const 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> | |
Vector< typename std::common_type< T, U >::type, N > | operator* (const Vector< T, N > &lhs, U rhs) |
Right scalar multiplication. More... | |
template<typename T , typename U , std::size_t N> | |
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> | |
Vector< typename std::common_type< T, U >::type, N > | operator* (T lhs, const Vector< U, N > &rhs) |
Left scalar multiplication. More... | |
template<typename T , typename U , std::size_t N> | |
Vector< typename std::common_type< T, U >::type, N > | operator/ (const Vector< T, N > &lhs, const Vector< U, N > &rhs) |
Component-wise division. More... | |
template<typename T , typename U , std::size_t N> | |
Vector< T, N > & | operator/= (Vector< T, N > &lhs, const 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> | |
Vector< typename std::common_type< T, U >::type, N > | operator/ (const Vector< T, N > &lhs, U rhs) |
Right scalar division. More... | |
template<typename T , typename U , std::size_t N> | |
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> | |
Vector< typename std::common_type< T, U >::type, N > | operator/ (T lhs, const Vector< U, N > &rhs) |
Left scalar division. More... | |
template<std::size_t N> | |
Vector< bool, N > | operator|| (const Vector< bool, N > &lhs, const Vector< bool, N > &rhs) |
Component-wise logical or operator. More... | |
template<std::size_t N> | |
Vector< bool, N > | operator&& (const Vector< bool, N > &lhs, const Vector< bool, N > &rhs) |
Component-wise logical and operator. More... | |
template<typename T , std::size_t N> | |
T | dot (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Scalar product. More... | |
template<typename T , std::size_t N> | |
Vector< T, N > | min (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Component-wise minimum. More... | |
template<typename T , std::size_t N> | |
Vector< T, N > | max (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Component-wise maximum. More... | |
template<typename T , std::size_t N> | |
Vector< T, N > | abs (const Vector< T, N > &val) |
Component-wise absolute value. More... | |
template<typename T , std::size_t N> | |
Vector< bool, N > | equals (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Component-wise equality operator. More... | |
template<typename T , std::size_t N> | |
Vector< bool, N > | lessThan (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Component-wise comparison operator. More... | |
template<typename T , std::size_t N> | |
Vector< bool, N > | greaterThan (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Component-wise comparison operator. More... | |
template<typename T , std::size_t N> | |
Vector< T, N > | select (const Vector< bool, N > &cond, const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Component-wise selection operator. More... | |
template<typename T , std::size_t N> | |
Vector< T, N > | clamp (const Vector< T, N > &val, const Vector< T, N > &lo, const Vector< T, N > &hi) |
Component-wise clamp function. More... | |
template<typename T , std::size_t N> | |
Vector< T, N > | clamp (const Vector< T, N > &val, T lo, T hi) |
Component-wise clamp function. More... | |
template<typename T , typename U , std::size_t N> | |
Vector< typename std::common_type< T, U >::type, N > | lerp (const Vector< T, N > &lhs, const Vector< T, N > &rhs, U t) |
Component-wise lerp function. More... | |
template<typename T , std::size_t N> | |
T | manhattanLength (const Vector< T, N > &vec) |
Manhattan length of a vector. More... | |
template<typename T , std::size_t N> | |
T | squareLength (const Vector< T, N > &vec) |
Square Euclidean length of a vector. More... | |
template<typename T , std::size_t N> | |
T | euclideanLength (const Vector< T, N > &vec) |
Euclidean length of a vector. More... | |
template<typename T , std::size_t N> | |
T | chebyshevLength (const Vector< T, N > &vec) |
Chebyshev length of a vector. More... | |
template<typename T , std::size_t N> | |
T | naturalLength (const Vector< T, N > &vec) |
Natural length of a vector. More... | |
template<typename T , std::size_t N> | |
T | manhattanDistance (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Manhattan distance between two vectors. More... | |
template<typename T , std::size_t N> | |
T | squareDistance (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Square Euclidean distance between two vectors. More... | |
template<typename T , std::size_t N> | |
T | euclideanDistance (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Euclidean distance between two vectors. More... | |
template<typename T , std::size_t N> | |
T | chebyshevDistance (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Chebyshev distance between two vectors. More... | |
template<typename T , std::size_t N> | |
T | naturalDistance (const Vector< T, N > &lhs, const Vector< T, N > &rhs) |
Natural distance between two vectors. More... | |
template<typename T , std::size_t N> | |
Vector< T, N > | normalize (const Vector< T, N > &vec) |
Normalize a vector. 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 > | |
Vector< T, 2 > | vectorTripleProduct (Vector< T, 2 > a, Vector< T, 2 > b, Vector< T, 2 > c) |
Regular vector triple product. More... | |
template<typename T > | |
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 (const Vector< T, 3 > &lhs, const Vector< T, 3 > &rhs) |
Cross product for 3D vectors. 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.
|
inlineexplicit |
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 |
|
inline |
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 |
|
inline |
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.
|
inline |
Access to the \( i \)-th coordinate.
i | the coordinate number |
|
inline |
Access to the \( i \)-th coordinate.
i | the coordinate number |
Component-wise absolute value.
Angle of a vector relative to the x-axis.
vec | The 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 |
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 other vectors.
|
related |
Component-wise clamp function.
Relative to two values.
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.
lhs | A first vector |
rhs | A second vector |
|
related |
Cross product for 3D vectors.
lhs | A first vector |
rhs | A second 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 |
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 |
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 |
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 |
Component-wise multiplication.
|
related |
Right scalar multiplication.
|
related |
Left scalar multiplication.
|
related |
Component-wise multiplication and assignment.
|
related |
Right scalar multiplication and assignment.
|
related |
Component-wise addition.
|
related |
Right scalar addition.
|
related |
Left scalar addition.
|
related |
Component-wise addition and assignment.
Component-wise unary minus.
|
related |
Component-wise substraction.
|
related |
Right scalar substraction.
|
related |
Left scalar substraction.
|
related |
Component-wise substraction and assignment.
|
related |
Component-wise division.
|
related |
Right scalar division.
|
related |
Left scalar division.
|
related |
Component-wise division and assignment.
|
related |
Right scalar division and assignment.
|
related |
Equality operator between two vectors.
|
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 |
Component-wise selection operator.
|
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 |
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. |
Unit vector in a specified direction.
angle | The angle of the direction |
|
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.