-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Ease.h
167 lines (124 loc) · 3.9 KB
/
Ease.h
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
#pragma once
#include <algorithm>
#include <cmath>
namespace swoosh {
namespace ease {
static double pi = 3.14159265358979323846; //!< Precalculated pi
/**
@brief calculates radians
*/
template<typename T>
static T radians(T degrees) { return static_cast<T>((static_cast<double>(degrees) * pi) / 180.0); }
/**
@brief Interpolate values from a to b with percentage factor
*/
template<typename T>
static T interpolate(T factor, T a, T b) {
return a + ((b - a) * factor);
}
/**
y = x;
@brief percentile values from 0 => 1.0
*/
template<typename T>
static T linear(T delta, T length, T power) {
T normal = static_cast<T>(1.0 / static_cast<double>(length));
T x = delta * normal;
if (x >= 1) {
x = 1;
}
T y = std::pow(x, power);
return y;
}
/**
y = (1 - abs(2-x*4) + 1)/2
@brief sharp back and forth, no easing
*/
template<typename T>
static T inOut(T delta, T length) {
T normal = static_cast<T>(1.0 / static_cast<double>(length));
T x = delta * normal;
if (x >= 1) {
x = 1;
}
T y = static_cast<T>((1.0 - std::fabs(2.0 - static_cast<double>(x) * 4.0) + 1.0) / 2.0);
return y;
}
/**
@brief output is 1.0 only at the half-way mark and returns to 0 at the end of the curve
*/
template<typename T>
static T wideParabola(T delta, T length, T power) {
T normal = static_cast<T>(2.0 / static_cast<double>(length));
// Convert seconds elapsed to x values of 0 -> 2
T x = delta * normal;
// When x = 2, the parabola drops into the negatives
// prevent that
if (x >= 2) {
x = 2;
}
// y = 1 - (x ^ 2 - 2x + 1) ^ n
T poly = static_cast<T>((x*x) - (2.0 * static_cast<double>(x)) + 1.0);
T y = static_cast<T>(1.0 - static_cast<double>(std::pow(poly, power)));
return y;
}
/**
y = 3x ^ 2 - 2x ^ 4
@brief overshoot destination and slide back at the end
*/
template<typename T>
static T bezierPopIn(T delta, T length) {
T normal = static_cast<T>(1.0 / static_cast<double>(length));
T x = delta * normal;
if (x >= 1) {
x = 1;
}
double part1 = static_cast<double>(3 * x * x);
double part2 = static_cast<double>(2 * x * x * x * x);
T y = static_cast<T>(part1 - part2);
return y;
}
/**
y = 3(1-x) ^ 2 - 2(1-x) ^ 4
@brief pop out and then slide out
*/
template<typename T>
static T bezierPopOut(T delta, T length) {
T normal = static_cast<T>(1.0 / static_cast<double>(length));
T x = delta * normal;
if (x >= 1) {
x = 1;
}
double x2 = static_cast<T>(1.0 - static_cast<double>(x));
double part1 = 3 * x2 * x2;
double part2 = 2 * x2 * x2 * x2 * x2;
T y = static_cast<T>(part1 - part2);
return y;
}
/**
y = 1-(sin(x+90)*cos(-2x)
@brief bounces closer to the target value over the length of the sequence (time)
*/
template<typename T>
static T sinuoidBounceOut(T delta, T length) {
T normal = static_cast<T>(3.0 / static_cast<double>(length));
T x = delta * normal;
if (x >= 3) {
x = 3;
}
double y = 1.0 - (std::sin(static_cast<double>(x) + 90.0)*std::cos(-2.0 * static_cast<double>(x)));
// Transform y into canonical [0,1] values
return static_cast<T>(y/2.0);
}
/**
y = (2 + (factor * log(x/t)))/2
@brief quick burst from 0.0 and then slowly approaches 1.0 over the length (time)
*/
template<typename T>
static T wane(T delta, T length, T factor) {
double x = (2.0 + (static_cast<double>(factor) * std::log(static_cast<double>(delta) / static_cast<double>(length)))) * 0.5;
x = std::min(1.0, std::max(x, 0.0)); // limit x between [0,1] values
return x;
}
}
}