ranim_core/utils/rate_functions.rs
1/// Linear rate function
2///
3/// t
4#[inline]
5pub fn linear(t: f64) -> f64 {
6 t
7}
8
9/// Smooth rate function
10///
11/// t * t * t * (10.0 * s * s + 5.0 * s * t + t * t)
12///
13/// from <https://github.com/3b1b/manim/blob/003c4d86262565bb21001f74f67e6788cae62df4/manimlib/utils/rate_functions.py#L17>
14#[inline]
15pub fn smooth(t: f64) -> f64 {
16 let s = 1.0 - t;
17 t * t * t * (10.0 * s * s + 5.0 * s * t + t * t)
18}
19
20/// Ease-in quad rate function
21///
22/// t * t
23#[inline]
24pub fn ease_in_quad(t: f64) -> f64 {
25 t * t
26}
27
28/// Ease-out quad rate function
29///
30/// t * (2.0 - t)
31#[inline]
32pub fn ease_out_quad(t: f64) -> f64 {
33 t * (2.0 - t)
34}
35
36/// Ease-in-out quad rate function
37///
38/// when t < 0.5: 2.0 * t * t
39/// when t >= 0.5: 1.0 - 2.0 * (t - 1.0) * (t - 1.0)
40///
41#[inline]
42pub fn ease_in_out_quad(t: f64) -> f64 {
43 if t < 0.5 {
44 2.0 * t * t
45 } else {
46 1.0 - 2.0 * (t - 1.0) * (t - 1.0)
47 }
48}
49
50/// Ease-in cubic rate function
51///
52/// t * t * t
53#[inline]
54pub fn ease_in_cubic(t: f64) -> f64 {
55 t * t * t
56}
57
58/// Ease-out cubic rate function
59///
60/// t * (t - 1.0) * (t - 1.0) + 1.0
61#[inline]
62pub fn ease_out_cubic(t: f64) -> f64 {
63 t * (t - 1.0) * (t - 1.0) + 1.0
64}
65
66/// Ease-in-out cubic rate function
67///
68/// when t < 0.5: 4.0 * t * t * t
69/// when t >= 0.5: 1.0 - 4.0 * (t - 1.0) * (t - 1.0) * (t - 1.0)
70#[inline]
71pub fn ease_in_out_cubic(t: f64) -> f64 {
72 if t < 0.5 {
73 4.0 * t * t * t
74 } else {
75 1.0 - 4.0 * (t - 1.0) * (t - 1.0) * (t - 1.0)
76 }
77}