CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Math/curves.cpp
Views: 1401
1
2
#include <cmath>
3
4
#include "Common/Math/math_util.h"
5
#include "curves.h"
6
7
float linearInOut(int t, int fadeInLength, int solidLength, int fadeOutLength) {
8
if (t < 0) return 0;
9
if (t < fadeInLength) {
10
return (float)t / fadeInLength;
11
}
12
t -= fadeInLength;
13
if (t < solidLength) {
14
return 1.0f;
15
}
16
t -= solidLength;
17
if (t < fadeOutLength) {
18
return 1.0f - (float)t / fadeOutLength;
19
}
20
return 0.0f;
21
}
22
23
float linearIn(int t, int fadeInLength) {
24
if (t < 0) return 0;
25
if (t < fadeInLength) {
26
return (float)t / fadeInLength;
27
}
28
return 1.0f;
29
}
30
31
float linearOut(int t, int fadeOutLength) {
32
return 1.0f - linearIn(t, fadeOutLength);
33
}
34
35
float ease(float val) {
36
if (val >= 1.0f) return 1.0f;
37
if (val <= 0.0f) return 0.0f;
38
return (float)(((-cosf(val * PI)) + 1.0f) * 0.5);
39
}
40
41
float ease(int t, int fadeLength)
42
{
43
if (t <= 0.0f) return 0.0f;
44
if (t >= fadeLength) return 1.0f;
45
return ease((float)t / (float)fadeLength);
46
}
47
48
template <int hundredthsX1, int hundredthsX2, int hundredthsY1 = 0, int hundredthsY2 = 100>
49
inline float bezierEaseFunc(float val) {
50
constexpr float x1 = hundredthsX1 / 100.0f;
51
constexpr float x2 = hundredthsX2 / 100.0f;
52
constexpr float a = 1.0f - 3.0f * x2 + 3.0f * x1;
53
constexpr float b = 3.0f * x2 - 6.0f * x1;
54
constexpr float c = 3.0f * x1;
55
56
constexpr float y1 = hundredthsY1 / 100.0f;
57
constexpr float y2 = hundredthsY2 / 100.0f;
58
constexpr float ya = 1.0f - 3.0f * y2 + 3.0f * y1;
59
constexpr float yb = 3.0f * y2 - 6.0f * y1;
60
constexpr float yc = 3.0f * y1;
61
62
float guess = val;
63
// Newton-Raphson calculation, no need to be too precise.
64
for (int i = 0; i < 4; ++i) {
65
float slope = 3.0f * a * guess * guess + 2.0f * b * guess + c;
66
if (slope == 0.0f) {
67
break;
68
}
69
70
float x = ((a * guess + b) * guess + c) * guess - val;
71
guess -= x / slope;
72
}
73
74
return ((ya * guess + yb) * guess + yc) * guess;
75
}
76
77
float bezierEase(float val) {
78
return bezierEaseFunc<25, 25, 10, 100>(val);
79
}
80
81
float bezierEaseInOut(float val) {
82
return bezierEaseFunc<42, 58>(val);
83
}
84
85
float bezierEaseIn(float val) {
86
return bezierEaseFunc<42, 100>(val);
87
}
88
89
float bezierEaseOut(float val) {
90
return bezierEaseFunc<0, 58>(val);
91
}
92
93
float sawtooth(int t, int period) {
94
return (t % period) * (1.0f / (period - 1));
95
}
96
97
float passWithPause(int t, int fadeInLength, int pauseLength, int fadeOutLength)
98
{
99
if (t < fadeInLength) {
100
return -1.0f + (float)t / fadeInLength;
101
}
102
t -= fadeInLength;
103
if (t < pauseLength) {
104
return 0.0f;
105
}
106
t -= pauseLength;
107
if (t < fadeOutLength) {
108
return (float)t / fadeOutLength;
109
}
110
return 1.0f;
111
}
112
113