Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Classes | Typedefs | Functions | Variables
Maths

Maths classes and functions. More...

Classes

struct  gf::Matrix< T, ROWS, COLS >
 General purpose math matrix. More...
 
struct  gf::Matrix< T, 2, 2 >
 A 2x2 matrix. More...
 
struct  gf::Matrix< T, 3, 3 >
 A 3x3 matrix. More...
 
struct  gf::Matrix< T, 4, 4 >
 A 4x4 matrix. More...
 
struct  gf::Range< T >
 A half-open range of values. More...
 
struct  gf::Range< T >::Iterator
 A range iterator. More...
 
struct  gf::PositionRange< T >
 A 2D range. More...
 
struct  gf::PositionRange< T >::Iterator
 An iterator for a 2D range. More...
 
struct  gf::NeighborSquareRange< T >
 A 2D range. More...
 
struct  gf::NeighborSquareRange< T >::Iterator
 An iterator for a 2D range. More...
 
struct  gf::NeighborDiamondRange< T >
 A 2D range. More...
 
struct  gf::NeighborDiamondRange< T >::Iterator
 An iterator for a 2D range. More...
 
struct  gf::Vector< T, N >
 General purpose math vector. More...
 
struct  gf::Vector< T, 2 >
 A 2D vector. More...
 
struct  gf::Vector< T, 3 >
 A 3D vector. More...
 
struct  gf::Vector< T, 4 >
 A 4D vector. More...
 

Typedefs

template<typename T >
using gf::Step = T(*)(T)
 A step is a function with special features. More...
 
using gf::Matrix2f = Matrix< float, 2, 2 >
 A float square matrix of size 2. More...
 
using gf::Matrix2d = Matrix< double, 2, 2 >
 A double square matrix of size 2. More...
 
using gf::Matrix3f = Matrix< float, 3, 3 >
 A float square matrix of size 3. More...
 
using gf::Matrix3d = Matrix< double, 3, 3 >
 A double square matrix of size 3. More...
 
using gf::Matrix4f = Matrix< float, 4, 4 >
 A float square matrix of size 4. More...
 
using gf::Matrix4d = Matrix< double, 4, 4 >
 A double square matrix of size 4. More...
 
using gf::RangeF = Range< float >
 A float range. More...
 
using gf::RangeI = Range< int >
 A int range. More...
 
using gf::RangeU = Range< unsigned >
 A unsigned range. More...
 
using gf::RangeZ = Range< std::size_t >
 A std::size_t range. More...
 
using gf::Vector2f = Vector< float, 2 >
 A float vector with 2 components. More...
 
using gf::Vector3f = Vector< float, 3 >
 A float vector with 3 components. More...
 
using gf::Vector4f = Vector< float, 4 >
 A float vector with 4 components. More...
 
using gf::Vector2d = Vector< double, 2 >
 A double vector with 2 components. More...
 
using gf::Vector3d = Vector< double, 3 >
 A double vector with 3 components. More...
 
using gf::Vector4d = Vector< double, 4 >
 A double vector with 4 components. More...
 
using gf::Vector2i = Vector< int, 2 >
 A int vector with 2 components. More...
 
using gf::Vector3i = Vector< int, 3 >
 A int vector with 3 components. More...
 
using gf::Vector4i = Vector< int, 4 >
 A int vector with 4 components. More...
 
using gf::Vector2u = Vector< unsigned, 2 >
 A unsigned vector with 2 components. More...
 
using gf::Vector3u = Vector< unsigned, 3 >
 A unsigned vector with 3 components. More...
 
using gf::Vector4u = Vector< unsigned, 4 >
 A unsigned vector with 4 components. More...
 
using gf::Vector2z = Vector< std::size_t, 2 >
 A std::size_t vector with 2 components. More...
 
using gf::Vector3z = Vector< std::size_t, 3 >
 A std::size_t vector with 3 components. More...
 
using gf::Vector4z = Vector< std::size_t, 4 >
 A std::size_t vector with 4 components. More...
 
using gf::Vector2b = Vector< bool, 2 >
 A bool vector with 2 components. More...
 
using gf::Vector3b = Vector< bool, 3 >
 A bool vector with 3 components. More...
 
using gf::Vector4b = Vector< bool, 4 >
 A bool vector with 4 components. More...
 
template<typename T >
using gf::Color3 = Vector< T, 3 >
 A color vector with 3 components. More...
 
template<typename T >
using gf::Color4 = Vector< T, 4 >
 A color vector with 4 components. More...
 
using gf::Color3f = Color3< float >
 A float color vector with 3 components. More...
 
using gf::Color4f = Color4< float >
 A float color vector with 4 components. More...
 
using gf::Color3d = Color3< double >
 A double color vector with 3 components. More...
 
using gf::Color4d = Color4< double >
 A double color vector with 4 components. More...
 
using gf::Color3u = Color3< uint8_t >
 A uint8_t color vector with 3 components. More...
 
using gf::Color4u = Color4< uint8_t >
 A uint8_t color vector with 4 components. More...
 
template<typename T , std::size_t N>
using gf::Distance = T(*)(Vector< T, N >, Vector< T, N >)
 A distance function. More...
 
template<typename T >
using gf::Distance2 = Distance< T, 2 >
 A distance function for 2D vectors. More...
 
template<typename T >
using gf::Distance3 = Distance< T, 3 >
 A distance function for 3D vectors. More...
 

Functions

template<typename T >
constexpr T gf::pi ()
 Templated value of \( \pi \). More...
 
template<typename T >
constexpr bool gf::almostEquals (T a, T b, T epsilon=std::numeric_limits< T >::epsilon())
 Compare two floats. More...
 
template<typename T >
constexpr T gf::degreesToRadians (T degrees)
 Convert degrees to radians. More...
 
template<typename T >
constexpr T gf::radiansToDegrees (T radians)
 Convert radians to degrees. More...
 
template<typename T >
constexpr T gf::linearStep (T t)
 Linear step. More...
 
template<typename T >
constexpr T gf::cubicStep (T t)
 Cubic step (smoothstep) More...
 
template<typename T >
constexpr T gf::quinticStep (T t)
 Quintic step (smootherstep) More...
 
template<typename T >
gf::cosineStep (T t)
 Cosine step. More...
 
template<typename T , typename U >
constexpr T gf::lerp (T lhs, T rhs, U t)
 Linear interpolation function. More...
 
template<typename T >
constexpr T gf::clamp (T val, T lo, T hi)
 Clamping function. More...
 
template<typename T >
constexpr T gf::square (T val)
 Square function. More...
 
template<typename T >
constexpr T gf::cube (T val)
 Cube function. More...
 
template<typename T >
constexpr int gf::sign (T val)
 Sign function. More...
 
template<typename T >
constexpr T gf::absdiff (T lhs, T rhs)
 Absolute difference of two values. More...
 

Variables

constexpr float gf::Pi = pi<float>()
 The \( \pi \) constant. More...
 
constexpr float gf::Pi2 = Pi / 2
 The \( \frac{\pi}{2} \) constant. More...
 
constexpr float gf::Pi3 = Pi / 3
 The \( \frac{\pi}{3} \) constant. More...
 
constexpr float gf::Pi4 = Pi / 4
 The \( \frac{\pi}{4} \) constant. More...
 
constexpr float gf::Pi6 = Pi / 6
 The \( \frac{\pi}{6} \) constant. More...
 
constexpr float gf::Sqrt2 = 1.41421356237309504880f
 The \( \sqrt{2} \) constant. More...
 
constexpr float gf::InvSqrt2 = 1 / Sqrt2
 The \( \frac{1}{\sqrt{2}} \) constant. More...
 
constexpr float gf::Sqrt3 = 1.7320508075688772935f
 The \( \sqrt{3} \) constant. More...
 
constexpr float gf::Epsilon = std::numeric_limits<float>::epsilon()
 Machine epsilon. More...
 

Detailed Description

Maths classes and functions.

Typedef Documentation

◆ Color3

template<typename T >
using gf::Color3 = typedef Vector<T, 3>

A color vector with 3 components.

See also
gf::Vector, gf::Vector<T, 4>

◆ Color3d

using gf::Color3d = typedef Color3<double>

A double color vector with 3 components.

See also
gf::Vector, gf::Vector<T, 3>

◆ Color3f

using gf::Color3f = typedef Color3<float>

A float color vector with 3 components.

See also
gf::Vector, gf::Vector<T, 3>

◆ Color3u

using gf::Color3u = typedef Color3<uint8_t>

A uint8_t color vector with 3 components.

See also
gf::Vector, gf::Vector<T, 3>

◆ Color4

template<typename T >
using gf::Color4 = typedef Vector<T, 4>

A color vector with 4 components.

See also
gf::Vector, gf::Vector<T, 4>

◆ Color4d

using gf::Color4d = typedef Color4<double>

A double color vector with 4 components.

See also
gf::Vector, gf::Vector<T, 4>

◆ Color4f

using gf::Color4f = typedef Color4<float>

A float color vector with 4 components.

See also
gf::Vector, gf::Vector<T, 4>

◆ Color4u

using gf::Color4u = typedef Color4<uint8_t>

A uint8_t color vector with 4 components.

See also
gf::Vector, gf::Vector<T, 4>

◆ Distance

template<typename T , std::size_t N>
using gf::Distance = typedef T (*)(Vector<T, N>, Vector<T, N>)

A distance function.

A distance function is a function that gives the distance between two vectors.

gf::Distance<float, 3> distFn = gf::manhattanDistance<float, 3>;
Vector3f vec1 = ...;
Vector3f vec2 = ...;
float distance = distFn(vec1, vec2);
T(*)(Vector< T, N >, Vector< T, N >) Distance
A distance function.
Definition: Vector.h:1367
General purpose math vector.
Definition: Vector.h:61
See also
manhattanDistance(), squareDistance(), euclideanDistance(), chebyshevDistance()
gf::Distance2, gf::Distance3

◆ Distance2

template<typename T >
using gf::Distance2 = typedef Distance<T, 2>

A distance function for 2D vectors.

See also
gf::Distance

◆ Distance3

template<typename T >
using gf::Distance3 = typedef Distance<T, 3>

A distance function for 3D vectors.

See also
gf::Distance

◆ Matrix2d

using gf::Matrix2d = typedef Matrix<double, 2, 2>

A double square matrix of size 2.

See also
gf::Matrix, gf::Matrix<T, 2, 2>

◆ Matrix2f

using gf::Matrix2f = typedef Matrix<float, 2, 2>

A float square matrix of size 2.

See also
gf::Matrix, gf::Matrix<T, 2, 2>

◆ Matrix3d

using gf::Matrix3d = typedef Matrix<double, 3, 3>

A double square matrix of size 3.

See also
gf::Matrix, gf::Matrix<T, 3, 3>

◆ Matrix3f

using gf::Matrix3f = typedef Matrix<float, 3, 3>

A float square matrix of size 3.

See also
gf::Matrix, gf::Matrix<T, 3, 3>

◆ Matrix4d

using gf::Matrix4d = typedef Matrix<double, 4, 4>

A double square matrix of size 4.

See also
gf::Matrix, gf::Matrix<T, 4, 4>

◆ Matrix4f

using gf::Matrix4f = typedef Matrix<float, 4, 4>

A float square matrix of size 4.

See also
gf::Matrix, gf::Matrix<T, 4, 4>

◆ RangeF

using gf::RangeF = typedef Range<float>

A float range.

See also
gf::Range

◆ RangeI

using gf::RangeI = typedef Range<int>

A int range.

See also
gf::Range

◆ RangeU

using gf::RangeU = typedef Range<unsigned>

A unsigned range.

See also
gf::Range

◆ RangeZ

using gf::RangeZ = typedef Range<std::size_t>

A std::size_t range.

See also
gf::Range

◆ Step

template<typename T >
using gf::Step = typedef T (*)(T)

A step is a function with special features.

A step \( f \) is a (mathematical) function which has the following properties:

  • \( f(0) = 0 \)
  • \( f(1) = 1 \)

It can be used to smooth a linear interpolation.

gf::Vector2f point1 = ...;
gf::Vector2f point2 = ...;
float t = ...;
gf::Step<float> step = gf::cubicStep<float>;
gf::Vector2f interpolatedPoint = gf::lerp(point1, point2, t);
gf::Vector2f smoothedInterpolatedPoint = gf::lerp(point1, point2, step(t));
T(*)(T) Step
A step is a function with special features.
Definition: Math.h:177
constexpr T lerp(T lhs, T rhs, U t)
Linear interpolation function.
Definition: Math.h:266
See also
linearStep(), cubicStep(), quinticStep(), cosineStep()

◆ Vector2b

using gf::Vector2b = typedef Vector<bool, 2>

A bool vector with 2 components.

See also
gf::Vector, gf::Vector<T, 2>

◆ Vector2d

using gf::Vector2d = typedef Vector<double, 2>

A double vector with 2 components.

See also
gf::Vector, gf::Vector<T, 2>

◆ Vector2f

using gf::Vector2f = typedef Vector<float, 2>

A float vector with 2 components.

See also
gf::Vector, gf::Vector<T, 2>

◆ Vector2i

using gf::Vector2i = typedef Vector<int, 2>

A int vector with 2 components.

See also
gf::Vector, gf::Vector<T, 2>

◆ Vector2u

using gf::Vector2u = typedef Vector<unsigned, 2>

A unsigned vector with 2 components.

See also
gf::Vector, gf::Vector<T, 2>

◆ Vector2z

using gf::Vector2z = typedef Vector<std::size_t, 2>

A std::size_t vector with 2 components.

See also
gf::Vector, gf::Vector<T, 2>

◆ Vector3b

using gf::Vector3b = typedef Vector<bool, 3>

A bool vector with 3 components.

See also
gf::Vector, gf::Vector<T, 3>

◆ Vector3d

using gf::Vector3d = typedef Vector<double, 3>

A double vector with 3 components.

See also
gf::Vector, gf::Vector<T, 3>

◆ Vector3f

using gf::Vector3f = typedef Vector<float, 3>

A float vector with 3 components.

See also
gf::Vector, gf::Vector<T, 3>

◆ Vector3i

using gf::Vector3i = typedef Vector<int, 3>

A int vector with 3 components.

See also
gf::Vector, gf::Vector<T, 3>

◆ Vector3u

using gf::Vector3u = typedef Vector<unsigned, 3>

A unsigned vector with 3 components.

See also
gf::Vector, gf::Vector<T, 3>

◆ Vector3z

using gf::Vector3z = typedef Vector<std::size_t, 3>

A std::size_t vector with 3 components.

See also
gf::Vector, gf::Vector<T, 3>

◆ Vector4b

using gf::Vector4b = typedef Vector<bool, 4>

A bool vector with 4 components.

See also
gf::Vector, gf::Vector<T, 4>

◆ Vector4d

using gf::Vector4d = typedef Vector<double, 4>

A double vector with 4 components.

See also
gf::Vector, gf::Vector<T, 4>

◆ Vector4f

using gf::Vector4f = typedef Vector<float, 4>

A float vector with 4 components.

See also
gf::Vector, gf::Vector<T, 4>

◆ Vector4i

using gf::Vector4i = typedef Vector<int, 4>

A int vector with 4 components.

See also
gf::Vector, gf::Vector<T, 4>

◆ Vector4u

using gf::Vector4u = typedef Vector<unsigned, 4>

A unsigned vector with 4 components.

See also
gf::Vector, gf::Vector<T, 4>

◆ Vector4z

using gf::Vector4z = typedef Vector<std::size_t, 4>

A std::size_t vector with 4 components.

See also
gf::Vector, gf::Vector<T, 4>

Function Documentation

◆ absdiff()

template<typename T >
constexpr T gf::absdiff ( lhs,
rhs 
)
constexpr

Absolute difference of two values.

This function is defined as:

\[ \text{absdiff}(a, b) = | a - b | \]

Parameters
lhsThe first value
rhsThe second value
Returns
The absolute difference between the two values

◆ almostEquals()

template<typename T >
constexpr bool gf::almostEquals ( a,
b,
epsilon = std::numeric_limits<T>::epsilon() 
)
constexpr

Compare two floats.

Parameters
aThe first float
bThe second float
epsilonA small value that controls the equality comparison
See also
Comparison - The Floating-Point Guide

◆ clamp()

template<typename T >
constexpr T gf::clamp ( val,
lo,
hi 
)
constexpr

Clamping function.

This function takes a value and returns it if it is in a specified range. If not, the returned value is the nearest value in the range.

Parameters
valThe value to clamp
loThe minimum accepted value
hiThe maximum accepted value
Returns
The clamped value

◆ cosineStep()

template<typename T >
T gf::cosineStep ( t)
inline

Cosine step.

\[ f(t) = (1 - \cos(\pi * t)) * 0.5 \]

See also
gf::Step

◆ cube()

template<typename T >
constexpr T gf::cube ( val)
constexpr

Cube function.

For a value \( x \), the cube value is \( x^3 \).

Parameters
valA value
Returns
The cube of the value

◆ cubicStep()

template<typename T >
constexpr T gf::cubicStep ( t)
constexpr

Cubic step (smoothstep)

\[ f(t) = -2 * t^3 + 3 * t^2 \]

Compared to a step, it has the following properties:

  • \( f'(0) = 0 \)
  • \( f'(1) = 0 \)

This function is also known as smoothstep

See also
gf::Step

◆ degreesToRadians()

template<typename T >
constexpr T gf::degreesToRadians ( degrees)
constexpr

Convert degrees to radians.

Parameters
degreesAn angle in degrees
Returns
The angle in radians
See also
radiansToDegrees()

◆ lerp()

template<typename T , typename U >
constexpr T gf::lerp ( lhs,
rhs,
t 
)
constexpr

Linear interpolation function.

This functions returns an interpolated value between two values lhs and rhs according to a parameter \( t \). When \( t = 0 \) then lhs is returned, and when \( t = 1 \) then rhs is returned.

Parameters
lhsThe first value
rhsThe second value
tThe interpolation parameter, generally in the interval \( [0,1] \)
Returns
A value between the first and second value

◆ linearStep()

template<typename T >
constexpr T gf::linearStep ( t)
constexpr

Linear step.

\[ f(t) = t \]

See also
gf::Step

◆ pi()

template<typename T >
constexpr T gf::pi ( )
constexpr

Templated value of \( \pi \).

◆ quinticStep()

template<typename T >
constexpr T gf::quinticStep ( t)
constexpr

Quintic step (smootherstep)

\[ f(t) = 6 * t^5 - 15 * t^4 + 10 * t^3 \]

Compared to a step, it has the following properties:

  • \( f'(0) = 0 \)
  • \( f'(1) = 0 \)
  • \( f''(0) = 0 \)
  • \( f''(1) = 0 \)

This function is also known as smootherstep

See also
gf::Step

◆ radiansToDegrees()

template<typename T >
constexpr T gf::radiansToDegrees ( radians)
constexpr

Convert radians to degrees.

Parameters
radiansAn angle in radians
Returns
The angle in degrees
See also
degreesToRadians()

◆ sign()

template<typename T >
constexpr int gf::sign ( val)
constexpr

Sign function.

The sign function of \( x \) is:

  • \( -1 \) if \( x < 0 \)
  • \( 0 \) if \( x = 0 \)
  • \( 1 \) if \( x > 0 \)
Parameters
valA value
Returns
The sign of the value
See also
Sign function

◆ square()

template<typename T >
constexpr T gf::square ( val)
constexpr

Square function.

For a value \( x \), the square value is \( x^2 \).

Parameters
valA value
Returns
The square of the value

Variable Documentation

◆ Epsilon

constexpr float gf::Epsilon = std::numeric_limits<float>::epsilon()
constexpr

Machine epsilon.

◆ InvSqrt2

constexpr float gf::InvSqrt2 = 1 / Sqrt2
constexpr

The \( \frac{1}{\sqrt{2}} \) constant.

◆ Pi

constexpr float gf::Pi = pi<float>()
constexpr

The \( \pi \) constant.

◆ Pi2

constexpr float gf::Pi2 = Pi / 2
constexpr

The \( \frac{\pi}{2} \) constant.

◆ Pi3

constexpr float gf::Pi3 = Pi / 3
constexpr

The \( \frac{\pi}{3} \) constant.

◆ Pi4

constexpr float gf::Pi4 = Pi / 4
constexpr

The \( \frac{\pi}{4} \) constant.

◆ Pi6

constexpr float gf::Pi6 = Pi / 6
constexpr

The \( \frac{\pi}{6} \) constant.

◆ Sqrt2

constexpr float gf::Sqrt2 = 1.41421356237309504880f
constexpr

The \( \sqrt{2} \) constant.

◆ Sqrt3

constexpr float gf::Sqrt3 = 1.7320508075688772935f
constexpr

The \( \sqrt{3} \) constant.