From 37a966263d1372727e3d11f439b6ec9e270eac81 Mon Sep 17 00:00:00 2001 From: Noel Berry Date: Sat, 5 Dec 2020 13:49:02 -0800 Subject: [PATCH] added Calc::map and Calc::clamped_map --- public/blah/math/calc.cpp | 15 +++++++++++++++ public/blah/math/calc.h | 8 +++++++- 2 files changed, 22 insertions(+), 1 deletion(-) 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);