Math.h
Go to the documentation of this file.
#pragma once
#include <random>
namespace Eule
{
class Math
{
public:
[[nodiscard]] static constexpr double Max(const double a, const double b);
[[nodiscard]] static constexpr double Min(const double a, const double b);
[[nodiscard]] static constexpr double Clamp(const double v, const double min, const double max);
[[nodiscard]] static constexpr double Lerp(double a, double b, double t);
[[nodiscard]] static constexpr double Abs(const double a);
[[nodiscard]] static constexpr bool Similar(const double a, const double b, const double epsilon = 0.00001);
static double Random();
static unsigned int RandomUint();
static unsigned int RandomInt();
static double RandomRange(const double min, const double max);
static int RandomIntRange(const int max, const int min);
static bool RandomChance(const double chance);
static double Oscillate(const double a, const double b, const double counter, const double speed);
private:
static void InitRng();
static std::mt19937 rng;
static bool isRngInitialized;
// No instanciation! >:(
Math();
};
/* These are just the inline methods. They have to lie in the header file. */
/* The more sophisticated methods are in the .cpp */
constexpr inline double Math::Max(double a, double b)
{
return (a > b) ? a : b;
}
constexpr inline double Math::Min(double a, double b)
{
return (a < b) ? a : b;
}
constexpr inline double Math::Clamp(double v, double min, double max)
{
return Max(Min(v, max), min);
}
constexpr inline double Math::Lerp(double a, double b, double t)
{
const double it = 1.0 - t;
return (a * it) + (b * t);
}
inline constexpr double Math::Abs(const double a)
{
return (a > 0.0) ? a : -a;
}
inline constexpr bool Math::Math::Similar(const double a, const double b, const double epsilon)
{
return Abs(a - b) <= epsilon;
}
}
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