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 #include <stdexcept>
4 
5 namespace Eule
6 {
9  class Math
10  {
11  public:
13  [[nodiscard]] static constexpr double Max(const double a, const double b);
14 
16  [[nodiscard]] static constexpr double Min(const double a, const double b);
17 
19  [[nodiscard]] static constexpr double Clamp(const double v, const double min, const double max);
20 
22  [[nodiscard]] static constexpr double Lerp(double a, double b, double t);
23 
25  [[nodiscard]] static constexpr double Abs(const double a);
26 
28  [[nodiscard]] static constexpr bool Similar(const double a, const double b, const double epsilon = 0.00001);
29 
32  [[nodiscard]] static int Mod(const int numerator, const int denominator);
33 
35  static double Random();
36 
38  static unsigned int RandomUint();
39 
41  static unsigned int RandomInt();
42 
45  static double RandomRange(const double min, const double max);
46 
49  static int RandomIntRange(const int max, const int min);
50 
52  static bool RandomChance(const double chance);
53 
58  static double Oscillate(const double a, const double b, const double counter, const double speed);
59 
60  private:
62  static void InitRng();
63 
64  static std::mt19937 rng;
65  static bool isRngInitialized;
66 
67  // No instanciation! >:(
68  Math();
69  };
70 
71 
72 
73  /* These are just the inline methods. They have to lie in the header file. */
74  /* The more sophisticated methods are in the .cpp */
75 
76  constexpr inline double Math::Max(double a, double b)
77  {
78  return (a > b) ? a : b;
79  }
80 
81  constexpr inline double Math::Min(double a, double b)
82  {
83  return (a < b) ? a : b;
84  }
85 
86  constexpr inline double Math::Clamp(double v, double min, double max)
87  {
88  return Max(Min(v, max), min);
89  }
90 
91  constexpr inline double Math::Lerp(double a, double b, double t)
92  {
93  const double it = 1.0 - t;
94  return (a * it) + (b * t);
95  }
96 
97  constexpr inline double Math::Abs(const double a)
98  {
99  return (a > 0.0) ? a : -a;
100  }
101 
102  constexpr inline bool Math::Similar(const double a, const double b, const double epsilon)
103  {
104  return Abs(a - b) <= epsilon;
105  }
106 }
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:97
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:91
Eule::Math::Mod
static int Mod(const int numerator, const int denominator)
Will compute the actual modulo of a fraction.
Definition: Math.cpp:78
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:76
Eule::Math
Math utility class containing basic functions.
Definition: Math.h:9
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:86
Eule::Math::Min
static constexpr double Min(const double a, const double b)
Will return the smaller of two values.
Definition: Math.h:81
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::Similar
static constexpr bool Similar(const double a, const double b, const double epsilon=0.00001)
Compares two double values with a given accuracy.
Definition: Math.h:102
Eule::Math::Random
static double Random()
Will return a random double between 0 and 1
Definition: Math.cpp:29
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