diff --git a/Easing.h b/Easing.h index 93fa045..5191912 100644 --- a/Easing.h +++ b/Easing.h @@ -1,21 +1,23 @@ #pragma once +#include + namespace EasingFunc { typedef float(*eFunc)(float); - + static float easeLinear(float t) { return t; } static float easeInSine(float t) { - return sin(1.5707963 * t); + return sinf((t * M_PI) / 2.0f); } static float easeOutSine(float t) { - return 1 + sin(1.5707963 * (--t)); + return sinf((t * M_PI) / 2.0f); } static float easeInOutSine(float t) { - return 0.5 * (1 + sin(3.1415926 * (t - 0.5))); + return -(cosf(M_PI * t) - 1.0f) / 2.0f; } static float easeInQuad(float t) { @@ -23,11 +25,11 @@ namespace EasingFunc { } static float easeOutQuad(float t) { - return t * (2 - t); + return 1 - (1 - t) * (1 - t); } static float easeInOutQuad(float t) { - return t < 0.5 ? 2 * t * t : t * (4 - 2 * t) - 1; + return t < 0.5f ? 2 * t * t : 1 - powf(-2 * t + 2, 2) / 2; } static float easeInCubic(float t) { @@ -35,138 +37,127 @@ namespace EasingFunc { } static float easeOutCubic(float t) { - return 1 + (--t) * t * t; + return 1 - powf(1 - t, 3); } static float easeInOutCubic(float t) { - return t < 0.5 ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t); + return t < 0.5f ? 4 * t * t * t : 1 - powf(-2 * t + 2, 3) / 2; } static float easeInQuart(float t) { - t *= t; - return t * t; + return t * t * t * t; } static float easeOutQuart(float t) { - t = (--t) * t; - return 1 - t * t; + return 1 - powf(1 - t, 4); } static float easeInOutQuart(float t) { - if (t < 0.5) { - t *= t; - return 8 * t * t; - } else { - t = (--t) * t; - return 1 - 8 * t * t; - } + return t < 0.5f ? 8 * powf(t, 4) : 1 - powf(-2 * t + 2, 4) / 2; } static float easeInQuint(float t) { - float t2 = t * t; - return t * t2 * t2; + return powf(t, 5); } static float easeOutQuint(float t) { - float t2 = (--t) * t; - return 1 + t * t2 * t2; + return 1 - powf(1 - t, 5); } static float easeInOutQuint(float t) { - float t2; - if (t < 0.5) { - t2 = t * t; - return 16 * t * t2 * t2; - } else { - t2 = (--t) * t; - return 1 + 16 * t * t2 * t2; - } + return t < 0.5f ? 16 * powf(t, 5) : 1 - powf(-2 * t + 2, 5) / 2; } static float easeInExpo(float t) { - return (pow(2, 8 * t) - 1) / 255; + return t == 0 ? 0 : powf(2, 10 * t - 10); } static float easeOutExpo(float t) { - return 1 - pow(2, -8 * t); + return t == 1 ? 1 : 1 - powf(2, -10 * t); } static float easeInOutExpo(float t) { - if (t < 0.5) { - return (pow(2, 16 * t) - 1) / 510; - } else { - return 1 - 0.5 * pow(2, -16 * (t - 0.5)); - } + if (t == 0) return 0; + if (t == 1) return 1; + return t < 0.5f ? powf(2, 20 * t - 10) / 2 : (2 - powf(2, -20 * t + 10)) / 2; } static float easeInCirc(float t) { - return 1 - sqrt(1 - t); + return 1 - sqrtf(1 - t * t); } static float easeOutCirc(float t) { - return sqrt(t); + return sqrtf(1 - powf(t - 1, 2)); } static float easeInOutCirc(float t) { - if (t < 0.5) { - return (1 - sqrt(1 - 2 * t)) * 0.5; - } else { - return (1 + sqrt(2 * t - 1)) * 0.5; - } + return t < 0.5f + ? (1 - sqrtf(1 - 4 * t * t)) / 2 + : (sqrtf(1 - powf(-2 * t + 2, 2)) + 1) / 2; } static float easeInBack(float t) { - return t * t * (2.70158 * t - 1.70158); + constexpr float c1 = 1.70158f; + constexpr float c3 = c1 + 1; + return c3 * t * t * t - c1 * t * t; } static float easeOutBack(float t) { - return 1 + (--t) * t * (2.70158 * t + 1.70158); + constexpr float c1 = 1.70158f; + constexpr float c3 = c1 + 1; + return 1 + c3 * powf(t - 1, 3) + c1 * powf(t - 1, 2); } static float easeInOutBack(float t) { - if (t < 0.5) { - return t * t * (7 * t - 2.5) * 2; - } else { - return 1 + (--t) * t * 2 * (7 * t + 2.5); - } + constexpr float c1 = 1.70158f; + constexpr float c2 = c1 * 1.525f; + return t < 0.5f + ? powf(2 * t, 2) * ((c2 + 1) * 2 * t - c2) / 2 + : (powf(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2; } static float easeInElastic(float t) { - float t2 = t * t; - return t2 * t2 * sin(t * PI * 4.5); + constexpr float c4 = (2 * M_PI) / 3; + return t == 0 ? 0 : t == 1 ? 1 : -powf(2, 10 * t - 10) * sinf((t * 10 - 10.75f) * c4); } static float easeOutElastic(float t) { - float t2 = (t - 1) * (t - 1); - return 1 - t2 * t2 * cos(t * PI * 4.5); + constexpr float c4 = (2 * M_PI) / 3; + return t == 0 ? 0 : t == 1 ? 1 : powf(2, -10 * t) * sinf((t * 10 - 0.75f) * c4) + 1; } static float easeInOutElastic(float t) { - float t2; - if (t < 0.45) { - t2 = t * t; - return 8 * t2 * t2 * sin(t * PI * 9); - } else if (t < 0.55) { - return 0.5 + 0.75 * sin(t * PI * 4); + constexpr float c5 = (2 * M_PI) / 4.5f; + if (t == 0) return 0; + if (t == 1) return 1; + return t < 0.5f + ? -(powf(2, 20 * t - 10) * sinf((20 * t - 11.125f) * c5)) / 2 + : (powf(2, -20 * t + 10) * sinf((20 * t - 11.125f) * c5)) / 2 + 1; + } + + static float easeOutBounce(float t) { + if (t < 1 / 2.75f) { + return 7.5625f * t * t; + } else if (t < 2 / 2.75f) { + t -= 1.5f / 2.75f; + return 7.5625f * t * t + 0.75f; + } else if (t < 2.5f / 2.75f) { + t -= 2.25f / 2.75f; + return 7.5625f * t * t + 0.9375f; } else { - t2 = (t - 1) * (t - 1); - return 1 - 8 * t2 * t2 * sin(t * PI * 9); + t -= 2.625f / 2.75f; + return 7.5625f * t * t + 0.984375f; } } static float easeInBounce(float t) { - return pow(2, 6 * (t - 1)) * abs(sin(t * PI * 3.5)); - } - - static float easeOutBounce(float t) { - return 1 - pow(2, -6 * t) * abs(cos(t * PI * 3.5)); + return 1 - easeOutBounce(1 - t); } static float easeInOutBounce(float t) { - if (t < 0.5) { - return 8 * pow(2, 8 * (t - 1)) * abs(sin(t * PI * 7)); - } else { - return 1 - 8 * pow(2, -8 * t) * abs(sin(t * PI * 7)); - } + return t < 0.5f + ? (1 - easeOutBounce(1 - 2 * t)) / 2 + : (1 + easeOutBounce(2 * t - 1)) / 2; } + }