-
Notifications
You must be signed in to change notification settings - Fork 1
/
EZEasing.cpp
188 lines (144 loc) · 5.23 KB
/
EZEasing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "EZEasing.hpp"
#define _USE_MATH_DEFINES
#include <math.h>
#include <cmath>
namespace eze {
constexpr static float PI { static_cast<float>(M_PI) };
float easeLinear(float ratio) {
return ratio;
}
float easeInSine(float ratio) {
return 1.0f - cos((ratio * PI) / 2.0f);
}
float easeOutSine(float ratio) {
return sin((ratio * PI) / 2.0f);
}
float easeInOutSine(float ratio) {
return -cos(((ratio * PI) - 1.0f) / 2.0f);
}
float easeInQuad(float ratio) {
return ratio * ratio;
}
float easeOutQuad(float ratio) {
return 1.0f - (1.0f - ratio) * (1.0f - ratio);
}
float easeInOutQuad(float ratio) {
return ratio < 0.5f ? 2.0f * ratio * ratio : 1.0f - pow(-2.0f * ratio + 2.0f, 2.0f) / 2.0f;
}
float easeInCubic(float ratio) {
return ratio * ratio * ratio;
}
float easeOutCubic(float ratio) {
return 1.0f - pow(1.0f - ratio, 3.0f);
}
float easeInOutCubic(float ratio) {
return ratio < 0.5f ? 4.0f * ratio * ratio * ratio : 1.0f - pow(-2.0f * ratio + 2.0f, 3.0f) / 2.0f;
}
float easeInQuart(float ratio) {
return ratio * ratio * ratio * ratio;
}
float easeOutQuart(float ratio) {
return 1.0f - pow(1.0f - ratio, 4.0f);
}
float easeInOutQuart(float ratio) {
return ratio < 0.5f ? 8.0f * ratio * ratio * ratio * ratio : 1.0f - pow(-2.0f * ratio + 2.0f, 4.0f) / 2.0f;
}
float easeInQuint(float ratio) {
return ratio * ratio * ratio * ratio * ratio;
}
float easeOutQuint(float ratio) {
return 1.0f - pow(1.0f - ratio, 5.0f);
}
float easeInOutQuint(float ratio) {
return ratio < 0.5f ? 16.0f * ratio * ratio * ratio * ratio * ratio : 1.0f - pow(-2.0f * ratio + 2.0f, 5.0f) / 2.0f;
}
float easeInExpo(float ratio) {
return ratio == 0.0f ? 0.0f : pow(2.0f, 10.0f * ratio - 10.0f);
}
float easeOutExpo(float ratio) {
return ratio == 1.0f ? 1.0f : 1.0f - pow(2.0f, -10.0f * ratio);
}
float easeInOutExpo(float ratio) {
return ratio == 0.0f
? 0.0f
: ratio == 1.0f
? 1.0f
: ratio < 0.5f ? pow(2.0f, 20.0f * ratio - 10.0f) / 2.0f
: (2.0f - pow(2.0f, -20.0f * ratio + 10.0f)) / 2.0f;
}
float easeInCirc(float ratio) {
return 1.0f - sqrt(1.0f - pow(ratio, 2.0f));
}
float easeOutCirc(float ratio) {
return sqrt(1.0f - pow(ratio - 1.0f, 2.0f));
}
float easeInOutCirc(float ratio) {
return ratio < 0.5f
? (1.0f - sqrt(1.0f - pow(2.0f * ratio, 2.0f))) / 2.0f
: (sqrt(1.0f - pow(-2.0f * ratio + 2.0f, 2.0f)) + 1.0f) / 2.0f;
}
float easeInBack(float ratio) {
static constexpr float c1 = 1.70158f;
static constexpr float c3 = c1 + 1.0f;
return c3 * ratio * ratio * ratio - c1 * ratio * ratio;
}
float easeOutBack(float ratio) {
static constexpr float c1 = 1.70158f;
static constexpr float c3 = c1 + 1.0f;
return 1.0f + c3 * pow(ratio - 1.0f, 3.0f) + c1 * pow(ratio - 1.0f, 2.0f);
}
float easeInOutBack(float ratio) {
static constexpr float c1 = 1.70158f;
static constexpr float c2 = c1 * 1.525f;
return ratio < 0.5
? (pow(2.0f * ratio, 2.0f) * ((c2 + 1) * 2.0f * ratio - c2)) / 2.0f
: (pow(2.0f * ratio - 2.0f, 2.0f) * ((c2 + 1.0f) * (ratio * 2.0f - 2.0f) + c2) + 2.0f) / 2.0f;
}
float easeInElastic(float ratio) {
static constexpr float c4 = (2.0f * PI) / 3.0f;
return ratio == 0.0f
? 0.0f
: ratio == 1.0f
? 1.0f
: -pow(2.0f, 10.0f * ratio - 10.0f) * sin((ratio * 10.0f - 10.75f) * c4);
}
float easeOutElastic(float ratio) {
static constexpr float c4 = (2.0f * PI) / 3.0f;
return ratio == 0.0f
? 0.0f
: ratio == 1.0f
? 1.0f
: pow(2.0f, -10.0f * ratio) * sin((ratio * 10.0f - 0.75f) * c4) + 1.0f;
}
float easeInOutElastic(float ratio) {
static const float c5 = (2.0f * PI) / 4.5f;
return ratio == 0.0f
? 0.0f
: ratio == 1.0f
? 1.0f
: ratio < 0.5f
? -(pow(2.0f, 20.0f * ratio - 10.0f) * sin((20.0f * ratio - 11.125f) * c5)) / 2.0f
: (pow(2.0f, -20.0f * ratio + 10.0f) * sin((20.0f * ratio - 11.125f) * c5)) / 2.0f + 1.0f;
}
float easeInBounce(float ratio) {
return 1.0f - easeOutBounce(1.0f - ratio);
}
float easeOutBounce(float ratio) {
static constexpr float n1 = 7.5625f;
static constexpr float d1 = 2.75f;
if (ratio < 1.0f / d1) {
return n1 * ratio * ratio;
} else if (ratio < 2.0f / d1) {
return n1 * (ratio -= 1.5f / d1) * ratio + 0.75f;
} else if (ratio < 2.5f / d1) {
return n1 * (ratio -= 2.25f / d1) * ratio + 0.9375f;
} else {
return n1 * (ratio -= 2.625f / d1) * ratio + 0.984375f;
}
}
float easeInOutBounce(float ratio) {
return ratio < 0.5f
? (1.0f - easeOutBounce(1.0f - 2.0f * ratio)) / 2.0f
: (1.0f + easeOutBounce(2.0f * ratio - 1.0f)) / 2.0f;
}
}