Gamedev Framework (gf)
0.1.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... | |
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 | 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> | |
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 > | |
constexpr Vector< T, 2 > | perp (const Vector< T, 2 > &vec) |
Perpendicular vector. More... | |
template<typename T > | |
constexpr Vector< T, 3 > | cross (const Vector< T, 3 > &lhs, const Vector< T, 3 > &rhs) |
Cross product. 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 |
|
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.
|
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 \( l \) of a vector \( (u_1, \ldots, u_N) \) is:
\[ l = \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.
|
related |
Cross product.
lhs | A first vector |
rhs | A second vector |
|
related |
Scalar product.
|
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 \( l \) of a vector \( (u_1, \ldots, u_N) \) is:
\[ l = \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 |
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 \( l \) of a vector \( (u_1, \ldots, u_N) \) is:
\[ l = \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.
Normalize a vector.
The normalized vector of \( v \) is a vector in the same direction but with a (euclidean) length of 1. 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.
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 \( l \) of a vector \( (u_1, \ldots, u_N) \) is:
\[ l = \sum_{i = 1}^{N} u_i^2 \]
vec | A vector. |
Unit vector in a specified direction.
angle | The angle of the direction |
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.