added Calc::map and Calc::clamped_map

This commit is contained in:
Noel Berry 2020-12-05 13:49:02 -08:00
parent ef5714bb83
commit 37a966263d
2 changed files with 22 additions and 1 deletions

View File

@ -46,6 +46,16 @@ int Calc::clamp_int(int t, int min, int max)
return t < min ? min : (t > max ? max : t); return t < min ? min : (t > max ? max : t);
} }
float Calc::map(float t, float old_min, float old_max, float new_min, float new_max)
{
return new_min + ((t - old_min) / (old_max - old_min)) * (new_max - new_min);
}
float Calc::clamped_map(float t, float old_min, float old_max, float new_min, float new_max)
{
return map(Calc::clamp(t, old_min, old_max), old_min, old_max, new_min, new_max);
}
int Calc::sign(int x) int Calc::sign(int x)
{ {
return (x < 0 ? -1 : (x > 0 ? 1 : 0)); return (x < 0 ? -1 : (x > 0 ? 1 : 0));
@ -66,6 +76,11 @@ float Calc::abs(float x)
return x < 0 ? -x : x; return x < 0 ? -x : x;
} }
float Calc::round(float x)
{
return roundf(x);
}
float Calc::floor(float x) float Calc::floor(float x)
{ {
return floorf(x); return floorf(x);

View File

@ -28,6 +28,10 @@ namespace Blah
int clamp_int(int t, int min, int max); int clamp_int(int t, int min, int max);
float map(float t, float old_min, float old_max, float new_min, float new_max);
float clamped_map(float t, float old_min, float old_max, float new_min, float new_max);
int sign(int x); int sign(int x);
float sign(float x); float sign(float x);
@ -42,6 +46,8 @@ namespace Blah
template<class T, class U> template<class T, class U>
T max(T a, U b) { return (T)(a > b ? a : b); } T max(T a, U b) { return (T)(a > b ? a : b); }
float round(float x);
float floor(float x); float floor(float x);
float ceiling(float x); float ceiling(float x);