Leonetienne/Eule
Homemade math library, mainly targetted towards computer graphics
Math.h
Go to the documentation of this file.
1 #pragma once
2 #include <random>
3 
4 namespace Eule
5 {
8  class Math
9  {
10  public:
12  [[nodiscard]] static constexpr double Max(const double a, const double b);
13 
15  [[nodiscard]] static constexpr double Min(const double a, const double b);
16 
18  [[nodiscard]] static constexpr double Clamp(const double v, const double min, const double max);
19 
21  [[nodiscard]] static constexpr double Lerp(double a, double b, double t);
22 
24  [[nodiscard]] static constexpr double Abs(const double a);
25 
27  [[nodiscard]] static constexpr bool Similar(const double a, const double b, const double epsilon = 0.00001);
28 
30  static double Random();
31 
33  static unsigned int RandomUint();
34 
36  static unsigned int RandomInt();
37 
40  static double RandomRange(const double min, const double max);
41 
44  static int RandomIntRange(const int max, const int min);
45 
47  static bool RandomChance(const double chance);
48 
53  static double Oscillate(const double a, const double b, const double counter, const double speed);
54 
55  private:
57  static void InitRng();
58 
59  static std::mt19937 rng;
60  static bool isRngInitialized;
61 
62  // No instanciation! >:(
63  Math();
64  };
65 
66 
67 
68  /* These are just the inline methods. They have to lie in the header file. */
69  /* The more sophisticated methods are in the .cpp */
70 
71  constexpr inline double Math::Max(double a, double b)
72  {
73  return (a > b) ? a : b;
74  }
75 
76  constexpr inline double Math::Min(double a, double b)
77  {
78  return (a < b) ? a : b;
79  }
80 
81  constexpr inline double Math::Clamp(double v, double min, double max)
82  {
83  return Max(Min(v, max), min);
84  }
85 
86  constexpr inline double Math::Lerp(double a, double b, double t)
87  {
88  const double it = 1.0 - t;
89  return (a * it) + (b * t);
90  }
91 
92  inline constexpr double Math::Abs(const double a)
93  {
94  return (a > 0.0) ? a : -a;
95  }
96 
97  inline constexpr bool Math::Math::Similar(const double a, const double b, const double epsilon)
98  {
99  return Abs(a - b) <= epsilon;
100  }
101 }
Eule::Math::RandomChance
static bool RandomChance(const double chance)
Will 'roll' a dice, returning true percent of the time.
Definition: Math.cpp:73
Eule::Math::Abs
static constexpr double Abs(const double a)
Will return the absolute value of a
Definition: Math.h:92
Eule::Math::RandomRange
static double RandomRange(const double min, const double max)
Will return a random double within a range These bounds are INCLUSIVE!
Definition: Math.cpp:56
Eule::Math::Lerp
static constexpr double Lerp(double a, double b, double t)
Will return the linear interpolation between a and b by t
Definition: Math.h:86
Eule::Math::RandomInt
static unsigned int RandomInt()
Will return a random integer.
Definition: Math.cpp:45
Eule::Math::Max
static constexpr double Max(const double a, const double b)
Will return the bigger of two values.
Definition: Math.h:71
Eule::Math
Math utility class containing basic functions.
Definition: Math.h:8
Eule::Math::Clamp
static constexpr double Clamp(const double v, const double min, const double max)
Will return v, but at least min, and at most max
Definition: Math.h:81
Eule::Math::Min
static constexpr double Min(const double a, const double b)
Will return the smaller of two values.
Definition: Math.h:76
Eule::Math::RandomIntRange
static int RandomIntRange(const int max, const int min)
Will return a random integer within a range.
Definition: Math.cpp:63
Eule::Math::Random
static double Random()
Will return a random double between 0 and 1
Definition: Math.cpp:29
Eule::Math::Similar
static constexpr bool Similar(const double a, const double b, const double epsilon=0.00001)
Compares two double values with a given accuracy.
Eule::Math::RandomUint
static unsigned int RandomUint()
Will return a random unsigned integer.
Definition: Math.cpp:37
Eule
Definition: Collider.h:4
Eule::Math::Oscillate
static double Oscillate(const double a, const double b, const double counter, const double speed)
Kind of like , but it oscillates over instead of , by a given speed.
Definition: Math.cpp:68