This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08233e8
commit ded33b4
Showing
6 changed files
with
65 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include "Math.h" | ||
|
||
std::random_device Math::device{}; | ||
std::mt19937 Math::random_engine{Math::device()}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include <random> | ||
#include <cmath> | ||
|
||
namespace { | ||
std::random_device device{}; | ||
std::mt19937 rand_engine{device()}; | ||
} | ||
|
||
class Math { | ||
private: | ||
static std::random_device device; | ||
static std::mt19937 random_engine; | ||
|
||
public: | ||
static constexpr long double Pi = 3.141592653589793238462643383279502884L; | ||
static constexpr inline float deg2rad(float degrees) { return (degrees * Pi) / 180.0f; } | ||
static constexpr inline float rad2deg(float radians) { return (radians * 180.0f) / Pi; } | ||
|
||
|
||
static constexpr inline float clamp(float value, float min, float max) { return (value < min) ? min : (max < value) ? max : value; } | ||
|
||
|
||
static constexpr int wrap(int value, int min, int max) { | ||
int64_t range = max - min; | ||
return range == 0 ? min : min + ((((value - min) % range) + range) % range); | ||
} | ||
|
||
|
||
static constexpr inline int abs(int value) { return value < 0 ? -value : value; } | ||
static constexpr inline float abs(float value) { return value < 0.0f ? -value : value; } | ||
|
||
static inline float lerp(float from, float to, float weight) { return from + (to - from) * weight; } | ||
static inline float lerp_delta(float from, float to, float weight, float delta) { return lerp(from, to, 1.0f - std::pow(weight, delta)); } | ||
|
||
|
||
static inline int rand_range(int min, int max) { | ||
std::uniform_int_distribution<> distr{min, max}; | ||
return distr(random_engine); | ||
} | ||
|
||
|
||
static inline float rand_range(float min, float max) { | ||
std::uniform_real_distribution<float> distr{min, max}; | ||
return distr(random_engine); | ||
} | ||
|
||
|
||
template <typename T, typename... Args> | ||
static T choose(T first, Args... args) { | ||
T array[] = {first, args...}; | ||
std::uniform_int_distribution<> distr{0, sizeof...(Args)}; | ||
return array[distr(random_engine)]; | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters