Files
Tween/Easing.h
2025-05-12 17:25:26 +03:00

164 lines
4.2 KiB
C++

#pragma once
#include <cmath>
namespace EasingFunc {
typedef float(*eFunc)(float);
static float easeLinear(float t) {
return t;
}
static float easeInSine(float t) {
return sinf((t * M_PI) / 2.0f);
}
static float easeOutSine(float t) {
return sinf((t * M_PI) / 2.0f);
}
static float easeInOutSine(float t) {
return -(cosf(M_PI * t) - 1.0f) / 2.0f;
}
static float easeInQuad(float t) {
return t * t;
}
static float easeOutQuad(float t) {
return 1 - (1 - t) * (1 - t);
}
static float easeInOutQuad(float t) {
return t < 0.5f ? 2 * t * t : 1 - powf(-2 * t + 2, 2) / 2;
}
static float easeInCubic(float t) {
return t * t * t;
}
static float easeOutCubic(float t) {
return 1 - powf(1 - t, 3);
}
static float easeInOutCubic(float t) {
return t < 0.5f ? 4 * t * t * t : 1 - powf(-2 * t + 2, 3) / 2;
}
static float easeInQuart(float t) {
return t * t * t * t;
}
static float easeOutQuart(float t) {
return 1 - powf(1 - t, 4);
}
static float easeInOutQuart(float t) {
return t < 0.5f ? 8 * powf(t, 4) : 1 - powf(-2 * t + 2, 4) / 2;
}
static float easeInQuint(float t) {
return powf(t, 5);
}
static float easeOutQuint(float t) {
return 1 - powf(1 - t, 5);
}
static float easeInOutQuint(float t) {
return t < 0.5f ? 16 * powf(t, 5) : 1 - powf(-2 * t + 2, 5) / 2;
}
static float easeInExpo(float t) {
return t == 0 ? 0 : powf(2, 10 * t - 10);
}
static float easeOutExpo(float t) {
return t == 1 ? 1 : 1 - powf(2, -10 * t);
}
static float easeInOutExpo(float t) {
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 - sqrtf(1 - t * t);
}
static float easeOutCirc(float t) {
return sqrtf(1 - powf(t - 1, 2));
}
static float easeInOutCirc(float t) {
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) {
constexpr float c1 = 1.70158f;
constexpr float c3 = c1 + 1;
return c3 * t * t * t - c1 * t * t;
}
static float easeOutBack(float t) {
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) {
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) {
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) {
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) {
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 {
t -= 2.625f / 2.75f;
return 7.5625f * t * t + 0.984375f;
}
}
static float easeInBounce(float t) {
return 1 - easeOutBounce(1 - t);
}
static float easeInOutBounce(float t) {
return t < 0.5f
? (1 - easeOutBounce(1 - 2 * t)) / 2
: (1 + easeOutBounce(2 * t - 1)) / 2;
}
}