Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
wze::math Class Referencefinal

Math modul. More...

#include <math.hpp>

Public Member Functions

 math ()=delete
 Deleted explicit constructor.
 

Static Public Member Functions

static constexpr float epsilon ()
 Single precision epsilon.
 
static constexpr float pi ()
 Single precision PI.
 
static constexpr float to_radians (float degrees)
 Converts degrees to radians.
 
static constexpr float to_degrees (float radians)
 Converts radians to degrees.
 
static constexpr std::pair< float, float > normal (float x, float y)
 Returns the normal vector of a vector.
 
static float length (float x, float y)
 Returns the length of a vector.
 
static float angle (float x, float y)
 Returns the angle of a vector.
 
static std::pair< float, float > normalize (float x, float y)
 Normalizes a vector.
 
static float move_x (float length, float angle)
 Moves the x component of a vector.
 
static float move_y (float length, float angle)
 Moves the y component of a vector.
 
static std::array< float, 4 > transformation_matrix (float angle, float scale)
 Creates a transformation matrix.
 
static constexpr float transform_x (float x, float y, std::array< float, 4 > const &transformation_matrix)
 Transforms the x component of a vector.
 
static constexpr float transform_y (float x, float y, std::array< float, 4 > const &transformation_matrix)
 Transforms the y component of a vector.
 
template<typename T >
static T random (T minimum=std::numeric_limits< T >::lowest(), T maximum=std::numeric_limits< T >::max())
 Returns a random numeric value from an interval.
 
template<typename T >
static bool random (float probability=(float) true/2)
 Returns a random boolean value.
 

Detailed Description

Math modul.

Definition at line 37 of file math.hpp.

Member Function Documentation

◆ epsilon()

static constexpr float wze::math::epsilon ( )
inlinestaticnodiscardconstexpr

Single precision epsilon.

Definition at line 47 of file math.hpp.

47 {
48 // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
49 return .01;
50 }

◆ pi()

static constexpr float wze::math::pi ( )
inlinestaticnodiscardconstexpr

Single precision PI.

Definition at line 55 of file math.hpp.

55 {
56 // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
57 return 3.1415927;
58 }

◆ to_radians()

static constexpr float wze::math::to_radians ( float degrees)
inlinestaticnodiscardconstexpr

Converts degrees to radians.

Parameters
degreesAngle in degrees.
Returns
Angle in radians.

Definition at line 65 of file math.hpp.

65 {
66 // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
67 return degrees * pi() / 180;
68 }
static constexpr float pi()
Single precision PI.
Definition math.hpp:55

◆ to_degrees()

static constexpr float wze::math::to_degrees ( float radians)
inlinestaticnodiscardconstexpr

Converts radians to degrees.

Parameters
radiansAngle in radians.
Returns
Angle in degrees.

Definition at line 75 of file math.hpp.

75 {
76 // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
77 return radians * 180 / pi();
78 }

◆ normal()

static constexpr std::pair< float, float > wze::math::normal ( float x,
float y )
inlinestaticnodiscardconstexpr

Returns the normal vector of a vector.

Parameters
xX component of the vector.
yY component of the vector.
Returns
Normal vector of the vector.

Definition at line 86 of file math.hpp.

87 {
88 return {y, -x};
89 }

◆ length()

float wze::math::length ( float x,
float y )
staticnodiscard

Returns the length of a vector.

Parameters
xX component of the vector.
yY component of the vector.
Returns
Length of the vector.

Definition at line 33 of file math.cpp.

33 {
34 return sqrtf(powf(x, 2) + powf(y, 2));
35}

◆ angle()

float wze::math::angle ( float x,
float y )
staticnodiscard

Returns the angle of a vector.

Parameters
xX component of the vector.
yY component of the vector.
Returns
Angle of the vector.

Definition at line 37 of file math.cpp.

37 {
38 return atan2f(y, x);
39}

◆ normalize()

std::pair< float, float > wze::math::normalize ( float x,
float y )
staticnodiscard

Normalizes a vector.

Parameters
xX component of the vector.
yY component of the vector.
Returns
Normalized vector.

Definition at line 41 of file math.cpp.

41 {
42 if ((bool)x || (bool)y) {
43 float length{math::length(x, y)};
44 return {x / length, y / length};
45 }
46 return {0, 0};
47}
static float length(float x, float y)
Returns the length of a vector.
Definition math.cpp:33

◆ move_x()

float wze::math::move_x ( float length,
float angle )
staticnodiscard

Moves the x component of a vector.

Parameters
lengthLength of the movement.
angleAngle of the movement.
Returns
Moved x component of the vector.

Definition at line 49 of file math.cpp.

49 {
50 return length * cosf(angle);
51}
static float angle(float x, float y)
Returns the angle of a vector.
Definition math.cpp:37

◆ move_y()

float wze::math::move_y ( float length,
float angle )
staticnodiscard

Moves the y component of a vector.

Parameters
lengthLength of the movement.
angleAngle of the movement
Returns
Moved y component of the vector.

Definition at line 53 of file math.cpp.

53 {
54 return length * sinf(angle);
55}

◆ transformation_matrix()

std::array< float, 4 > wze::math::transformation_matrix ( float angle,
float scale )
staticnodiscard

Creates a transformation matrix.

Parameters
angleAngle of the transformation matrix.
scaleScale of the transformation matrix.
Returns
Created transformation matrix.

Definition at line 57 of file math.cpp.

58 {
59 float cosine_scale{cosf(angle) * scale};
60 float sine_scale{sinf(angle) * scale};
61 return {cosine_scale, -sine_scale, sine_scale, cosine_scale};
62}

◆ transform_x()

static constexpr float wze::math::transform_x ( float x,
float y,
std::array< float, 4 > const & transformation_matrix )
inlinestaticnodiscardconstexpr

Transforms the x component of a vector.

Parameters
xX component of the vector.
yY component of the vector.
transformation_matrixTransformation matrix.
Returns
Transformed x component of the vector.

Definition at line 148 of file math.hpp.

149 {
150 return (x * transformation_matrix.at(0)) +
151 (y * transformation_matrix.at(1));
152 }
static std::array< float, 4 > transformation_matrix(float angle, float scale)
Creates a transformation matrix.
Definition math.cpp:57

◆ transform_y()

static constexpr float wze::math::transform_y ( float x,
float y,
std::array< float, 4 > const & transformation_matrix )
inlinestaticnodiscardconstexpr

Transforms the y component of a vector.

Parameters
xX component of the vector.
yY component of the vector.
transformation_matrixTransformation matrix.
Returns
Transformed y component of the vector.

Definition at line 162 of file math.hpp.

163 {
164 return (x * transformation_matrix.at(2)) +
165 (y * transformation_matrix.at(3));
166 }

◆ random() [1/2]

template<typename T >
static T wze::math::random ( T minimum = std::numeric_limits<T>::lowest(),
T maximum = std::numeric_limits<T>::max() )
inlinestaticnodiscard

Returns a random numeric value from an interval.

Template Parameters
TNumeric type.
Parameters
minimumMinimum inclusive value of the interval.
maximumMaximum inclusive value of the interval.
Returns
Random numeric value from the interval.

Definition at line 176 of file math.hpp.

177 {
178 static_assert(!std::is_same_v<T, bool>);
179 if constexpr (std::is_integral_v<T>) {
180 return std::uniform_int_distribution<T>{minimum,
181 maximum}(_mt19937_64);
182 } else if constexpr (std::is_floating_point_v<T>) {
183 return std::uniform_real_distribution<T>{minimum,
184 maximum}(_mt19937_64);
185 }
186 }

◆ random() [2/2]

template<typename T >
static bool wze::math::random ( float probability = (float)true / 2)
inlinestaticnodiscard

Returns a random boolean value.

Parameters
TBoolean type.
probabilityProbability of a true value.
Returns
Random boolean value.

Definition at line 195 of file math.hpp.

195 {
196 static_assert(std::is_same_v<T, bool>);
197 return std::bernoulli_distribution{(double)probability}(_mt19937_64);
198 }

The documentation for this class was generated from the following files: