-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDVGEasing.c
More file actions
302 lines (269 loc) · 6.14 KB
/
DVGEasing.c
File metadata and controls
302 lines (269 loc) · 6.14 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
// easing.c
//
// Copyright (c) 2011, Auerhaus Development, LLC
//
// This program is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What The Fuck You Want
// To Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/wtfpl/COPYING for more details.
//
#include <math.h>
#include "DVGEasing.h"
// Modeled after the line y = x
DVGFloat DVGLinearInterpolation(DVGFloat p)
{
return p;
}
// Modeled after the parabola y = x^2
DVGFloat DVGQuadraticEaseIn(DVGFloat p)
{
return p * p;
}
// Modeled after the parabola y = -x^2 + 2x
DVGFloat DVGQuadraticEaseOut(DVGFloat p)
{
return -(p * (p - 2));
}
// Modeled after the piecewise quadratic
// y = (1/2)((2x)^2) ; [0, 0.5)
// y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1]
DVGFloat DVGQuadraticEaseInOut(DVGFloat p)
{
if(p < 0.5)
{
return 2 * p * p;
}
else
{
return (-2 * p * p) + (4 * p) - 1;
}
}
// Modeled after the cubic y = x^3
DVGFloat DVGCubicEaseIn(DVGFloat p)
{
return p * p * p;
}
// Modeled after the cubic y = (x - 1)^3 + 1
DVGFloat DVGCubicEaseOut(DVGFloat p)
{
DVGFloat f = (p - 1);
return f * f * f + 1;
}
// Modeled after the piecewise cubic
// y = (1/2)((2x)^3) ; [0, 0.5)
// y = (1/2)((2x-2)^3 + 2) ; [0.5, 1]
DVGFloat DVGCubicEaseInOut(DVGFloat p)
{
if(p < 0.5)
{
return 4 * p * p * p;
}
else
{
DVGFloat f = ((2 * p) - 2);
return 0.5 * f * f * f + 1;
}
}
// Modeled after the quartic x^4
DVGFloat DVGQuarticEaseIn(DVGFloat p)
{
return p * p * p * p;
}
// Modeled after the quartic y = 1 - (x - 1)^4
DVGFloat DVGQuarticEaseOut(DVGFloat p)
{
DVGFloat f = (p - 1);
return f * f * f * (1 - p) + 1;
}
// Modeled after the piecewise quartic
// y = (1/2)((2x)^4) ; [0, 0.5)
// y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1]
DVGFloat DVGQuarticEaseInOut(DVGFloat p)
{
if(p < 0.5)
{
return 8 * p * p * p * p;
}
else
{
DVGFloat f = (p - 1);
return -8 * f * f * f * f + 1;
}
}
// Modeled after the quintic y = x^5
DVGFloat DVGQuinticEaseIn(DVGFloat p)
{
return p * p * p * p * p;
}
// Modeled after the quintic y = (x - 1)^5 + 1
DVGFloat DVGQuinticEaseOut(DVGFloat p)
{
DVGFloat f = (p - 1);
return f * f * f * f * f + 1;
}
// Modeled after the piecewise quintic
// y = (1/2)((2x)^5) ; [0, 0.5)
// y = (1/2)((2x-2)^5 + 2) ; [0.5, 1]
DVGFloat DVGQuinticEaseInOut(DVGFloat p)
{
if(p < 0.5)
{
return 16 * p * p * p * p * p;
}
else
{
DVGFloat f = ((2 * p) - 2);
return 0.5 * f * f * f * f * f + 1;
}
}
// Modeled after quarter-cycle of sine wave
DVGFloat DVGSineEaseIn(DVGFloat p)
{
return sin((p - 1) * M_PI_2) + 1;
}
// Modeled after quarter-cycle of sine wave (different phase)
DVGFloat DVGSineEaseOut(DVGFloat p)
{
return sin(p * M_PI_2);
}
// Modeled after half sine wave
DVGFloat DVGSineEaseInOut(DVGFloat p)
{
return 0.5 * (1 - cos(p * M_PI));
}
// Modeled after shifted quadrant IV of unit circle
DVGFloat DVGCircularEaseIn(DVGFloat p)
{
return 1 - sqrt(1 - (p * p));
}
// Modeled after shifted quadrant II of unit circle
DVGFloat DVGCircularEaseOut(DVGFloat p)
{
return sqrt((2 - p) * p);
}
// Modeled after the piecewise circular function
// y = (1/2)(1 - sqrt(1 - 4x^2)) ; [0, 0.5)
// y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1]
DVGFloat DVGCircularEaseInOut(DVGFloat p)
{
if(p < 0.5)
{
return 0.5 * (1 - sqrt(1 - 4 * (p * p)));
}
else
{
return 0.5 * (sqrt(-((2 * p) - 3) * ((2 * p) - 1)) + 1);
}
}
// Modeled after the exponential function y = 2^(10(x - 1))
DVGFloat DVGExponentialEaseIn(DVGFloat p)
{
return (p == 0.0) ? p : pow(2, 10 * (p - 1));
}
// Modeled after the exponential function y = -2^(-10x) + 1
DVGFloat DVGExponentialEaseOut(DVGFloat p)
{
return (p == 1.0) ? p : 1 - pow(2, -10 * p);
}
// Modeled after the piecewise exponential
// y = (1/2)2^(10(2x - 1)) ; [0,0.5)
// y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1]
DVGFloat DVGExponentialEaseInOut(DVGFloat p)
{
if(p == 0.0 || p == 1.0) return p;
if(p < 0.5)
{
return 0.5 * pow(2, (20 * p) - 10);
}
else
{
return -0.5 * pow(2, (-20 * p) + 10) + 1;
}
}
// Modeled after the damped sine wave y = sin(13pi/2*x)*pow(2, 10 * (x - 1))
DVGFloat DVGElasticEaseIn(DVGFloat p)
{
return sin(13 * M_PI_2 * p) * pow(2, 10 * (p - 1));
}
// Modeled after the damped sine wave y = sin(-13pi/2*(x + 1))*pow(2, -10x) + 1
DVGFloat DVGElasticEaseOut(DVGFloat p)
{
return sin(-13 * M_PI_2 * (p + 1)) * pow(2, -10 * p) + 1;
}
// Modeled after the piecewise exponentially-damped sine wave:
// y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) ; [0,0.5)
// y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1]
DVGFloat DVGElasticEaseInOut(DVGFloat p)
{
if(p < 0.5)
{
return 0.5 * sin(13 * M_PI_2 * (2 * p)) * pow(2, 10 * ((2 * p) - 1));
}
else
{
return 0.5 * (sin(-13 * M_PI_2 * ((2 * p - 1) + 1)) * pow(2, -10 * (2 * p - 1)) + 2);
}
}
// Modeled after the overshooting cubic y = x^3-x*sin(x*pi)
DVGFloat DVGBackEaseIn(DVGFloat p)
{
return p * p * p - p * sin(p * M_PI);
}
// Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*sin((1-x)*pi))
DVGFloat DVGBackEaseOut(DVGFloat p)
{
DVGFloat f = (1 - p);
return 1 - (f * f * f - f * sin(f * M_PI));
}
// Modeled after the piecewise overshooting cubic function:
// y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) ; [0, 0.5)
// y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1]
DVGFloat DVGBackEaseInOut(DVGFloat p)
{
if(p < 0.5)
{
DVGFloat f = 2 * p;
return 0.5 * (f * f * f - f * sin(f * M_PI));
}
else
{
DVGFloat f = (1 - (2*p - 1));
return 0.5 * (1 - (f * f * f - f * sin(f * M_PI))) + 0.5;
}
}
DVGFloat DVGBounceEaseIn(DVGFloat p)
{
return 1 - DVGBounceEaseOut(1 - p);
}
DVGFloat DVGBounceEaseOut(DVGFloat p)
{
if(p < 4/11.0)
{
return (121 * p * p)/16.0;
}
else if(p < 8/11.0)
{
return (363/40.0 * p * p) - (99/10.0 * p) + 17/5.0;
}
else if(p < 9/10.0)
{
return (4356/361.0 * p * p) - (35442/1805.0 * p) + 16061/1805.0;
}
else
{
return (54/5.0 * p * p) - (513/25.0 * p) + 268/25.0;
}
}
DVGFloat DVGBounceEaseInOut(DVGFloat p)
{
if(p < 0.5)
{
return 0.5 * DVGBounceEaseIn(p*2);
}
else
{
return 0.5 * DVGBounceEaseOut(p * 2 - 1) + 0.5;
}
}