Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Math.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_MATH_H
22 #define GF_MATH_H
23 
24 #include <cmath>
25 #include <limits>
26 
27 namespace gf {
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 inline namespace v1 {
30 #endif
31 
36  template<typename T>
37  constexpr
38  T pi() {
39  return T(3.141592653589793238462643383279502884197169399L);
40  }
41 
46  constexpr float Pi = pi<float>();
47 
52  constexpr float Pi2 = Pi / 2;
53 
58  constexpr float Pi3 = Pi / 3;
59 
64  constexpr float Pi4 = Pi / 4;
65 
70  constexpr float Pi6 = Pi / 6;
71 
76  constexpr float Sqrt2 = 1.41421356237309504880f;
77 
82  constexpr float InvSqrt2 = 1 / Sqrt2;
83 
88  constexpr float Sqrt3 = 1.7320508075688772935f;
89 
94  constexpr float Epsilon = std::numeric_limits<float>::epsilon();
95 
106  template<typename T>
107  constexpr
108  bool almostEquals(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) {
109  if (a == b) {
110  return true;
111  }
112 
113  T diff = std::abs(a - b);
114 
115  if (a == 0 || b == 0 || diff < std::numeric_limits<T>::denorm_min()) {
116  return diff < (epsilon * std::numeric_limits<T>::denorm_min());
117  }
118 
119  T sum = std::abs(a) + std::abs(b);
120  sum = (sum < std::numeric_limits<T>::max()) ? sum : std::numeric_limits<T>::max();
121 
122  return (diff / sum) < epsilon;
123  }
124 
133  template<typename T>
134  constexpr
135  T degreesToRadians(T degrees) {
136  return degrees * gf::pi<T>() / T(180);
137  }
138 
147  template<typename T>
148  constexpr
149  T radiansToDegrees(T radians) {
150  return radians * T(180) / gf::pi<T>();
151  }
152 
176  template<typename T>
177  using Step = T (*)(T);
178 
187  template<typename T>
188  constexpr
190  return t;
191  }
192 
208  template<typename T>
209  constexpr
210  T cubicStep(T t) {
211  return (-T(2) * t + T(3)) * t * t;
212  }
213 
231  template<typename T>
232  constexpr
234  return ((T(6) * t - T(15)) * t + T(10)) * t * t * t;
235  }
236 
245  template<typename T>
246  inline
248  return (T(1) - std::cos(gf::pi<T>() * t)) * T(0.5);
249  }
250 
264  template<typename T, typename U>
265  constexpr
266  T lerp(T lhs, T rhs, U t) {
267  return (U(1) - t) * lhs + t * rhs;
268  }
269 
282  template<typename T>
283  constexpr
284  T clamp(T val, T lo, T hi) {
285  return val < lo ? lo : (val > hi ? hi : val);
286  }
287 
297  template<typename T>
298  constexpr
299  T square(T val) {
300  return val * val;
301  }
302 
312  template<typename T>
313  constexpr
314  T cube(T val) {
315  return val * val * val;
316  }
317 
332  template<typename T>
333  constexpr
334  int sign(T val) {
335  return (val > T(0)) - (val < T(0));
336  }
337 
350  template<typename T>
351  constexpr
352  T absdiff(T lhs, T rhs) {
353  return lhs > rhs ? lhs - rhs : rhs - lhs;
354  }
355 
356 #ifndef DOXYGEN_SHOULD_SKIP_THIS
357 }
358 #endif
359 }
360 
361 #endif // GF_MATH_H
T cosineStep(T t)
Cosine step.
Definition: Math.h:247
constexpr float Pi2
The constant.
Definition: Math.h:52
constexpr T clamp(T val, T lo, T hi)
Clamping function.
Definition: Math.h:284
constexpr float Epsilon
Machine epsilon.
Definition: Math.h:94
constexpr bool almostEquals(T a, T b, T epsilon=std::numeric_limits< T >::epsilon())
Compare two floats.
Definition: Math.h:108
constexpr float Pi6
The constant.
Definition: Math.h:70
constexpr T square(T val)
Square function.
Definition: Math.h:299
constexpr float Sqrt3
The constant.
Definition: Math.h:88
constexpr float Sqrt2
The constant.
Definition: Math.h:76
constexpr T cube(T val)
Cube function.
Definition: Math.h:314
constexpr float InvSqrt2
The constant.
Definition: Math.h:82
constexpr float Pi3
The constant.
Definition: Math.h:58
constexpr T lerp(T lhs, T rhs, U t)
Linear interpolation function.
Definition: Math.h:266
constexpr T cubicStep(T t)
Cubic step (smoothstep)
Definition: Math.h:210
The namespace for gf classes.
Definition: Action.h:35
constexpr T degreesToRadians(T degrees)
Convert degrees to radians.
Definition: Math.h:135
constexpr float Pi4
The constant.
Definition: Math.h:64
constexpr T radiansToDegrees(T radians)
Convert radians to degrees.
Definition: Math.h:149
T(*)(T) Step
A step is a function with special features.
Definition: Math.h:177
constexpr T quinticStep(T t)
Quintic step (smootherstep)
Definition: Math.h:233
constexpr T pi()
Templated value of .
Definition: Math.h:38
constexpr T absdiff(T lhs, T rhs)
Absolute difference of two values.
Definition: Math.h:352
constexpr T linearStep(T t)
Linear step.
Definition: Math.h:189
constexpr float Pi
The constant.
Definition: Math.h:46
constexpr int sign(T val)
Sign function.
Definition: Math.h:334