diff --git a/public/blah/math/calc.cpp b/public/blah/math/calc.cpp index 3bc2f9b..800a4ea 100644 --- a/public/blah/math/calc.cpp +++ b/public/blah/math/calc.cpp @@ -46,6 +46,16 @@ int Calc::clamp_int(int t, int min, int max) 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) { return (x < 0 ? -1 : (x > 0 ? 1 : 0)); @@ -66,6 +76,11 @@ float Calc::abs(float x) return x < 0 ? -x : x; } +float Calc::round(float x) +{ + return roundf(x); +} + float Calc::floor(float x) { return floorf(x); diff --git a/public/blah/math/calc.h b/public/blah/math/calc.h index b33c973..8bfd6f2 100644 --- a/public/blah/math/calc.h +++ b/public/blah/math/calc.h @@ -28,6 +28,10 @@ namespace Blah 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); float sign(float x); @@ -41,7 +45,9 @@ namespace Blah template T max(T a, U b) { return (T)(a > b ? a : b); } - + + float round(float x); + float floor(float x); float ceiling(float x);