diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-10-31 23:30:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-31 23:30:50 +0100 |
commit | ea0e9426175e897618bb4ad332e61c808be730c5 (patch) | |
tree | 5a7df916806dd69da33050da5d35eb6b630eecad /core | |
parent | cb3f594b148e33bebf99955c0bb7dce02c8a50c7 (diff) | |
parent | 216a8aa643503b898e1562634240b93406ae1ce3 (diff) |
Merge pull request #12035 from Chaosus/wrapfunc
Added new Wrap functions for numbers
Diffstat (limited to 'core')
-rw-r--r-- | core/math/math_funcs.cpp | 15 | ||||
-rw-r--r-- | core/math/math_funcs.h | 5 |
2 files changed, 19 insertions, 1 deletions
diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp index 6fb688f16b..64e01e5841 100644 --- a/core/math/math_funcs.cpp +++ b/core/math/math_funcs.cpp @@ -176,3 +176,18 @@ float Math::random(float from, float to) { float ret = (float)r / (float)RANDOM_MAX; return (ret) * (to - from) + from; } + +int Math::wrapi(int value, int min, int max) { + --max; + int rng = max - min + 1; + value = ((value - min) % rng); + if (value < 0) + return max + 1 + value; + else + return min + value; +} + +float Math::wrapf(float value, float min, float max) { + float rng = max - min; + return min + (value - min) - (rng * floor((value - min) / rng)); +} diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 65b2ffb0df..7715e5d6e5 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -207,6 +207,9 @@ public: static _ALWAYS_INLINE_ double round(double p_val) { return (p_val >= 0) ? Math::floor(p_val + 0.5) : -Math::floor(-p_val + 0.5); } static _ALWAYS_INLINE_ float round(float p_val) { return (p_val >= 0) ? Math::floor(p_val + 0.5) : -Math::floor(-p_val + 0.5); } + static int wrapi(int value, int min, int max); + static float wrapf(float value, float min, float max); + // double only, as these functions are mainly used by the editor and not performance-critical, static double ease(double p_x, double p_c); static int step_decimals(double p_step); @@ -268,7 +271,7 @@ public: #elif defined(_MSC_VER) && _MSC_VER < 1800 __asm fld a __asm fistp b -/*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) ) + /*#elif defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) ) // use AT&T inline assembly style, document that // we use memory as output (=m) and input (m) __asm__ __volatile__ ( |