eazy_data/easing/oscillatory/
bounce.rs

1//! # The Bounce Curve.
2
3use crate::easing::Curve;
4
5/// ### The [InBounce] Easing Function.
6///
7/// #### examples.
8///
9/// ```rust
10/// use eazy::Curve;
11/// use eazy::standard::bounce::InBounce;
12///
13/// let p = InBounce.y(1.0);
14/// ```
15#[derive(Debug)]
16pub struct InBounce;
17
18impl Curve for InBounce {
19  #[inline(always)]
20  fn y(&self, p: f32) -> f32 {
21    1.0 - OutBounce.y(1.0 - p)
22  }
23}
24
25#[test]
26fn test_in_bounce() {
27  let p = InBounce.y(1.0);
28
29  assert_eq!(p, 1.0);
30}
31
32/// ### [`OutBounce`] Easing Function.
33///
34/// #### examples.
35///
36/// ```rust
37/// use eazy::Curve;
38/// use eazy::standard::bounce::OutBounce;
39/// let p = OutBounce.y(1.0);
40/// ```
41#[derive(Debug)]
42pub struct OutBounce;
43
44impl Curve for OutBounce {
45  #[inline(always)]
46  fn y(&self, p: f32) -> f32 {
47    let t;
48    let r = 1.0 / 2.75; // -- reciprocal.
49    let k1 = r; // ---------- 36.36%
50    let k2 = 2.0 * r; // ---- 72.72%
51    let k3 = 1.5 * r; // ---- 54.54%
52    let k4 = 2.5 * r; // ---- 90.90%
53    let k5 = 2.25 * r; // --- 81.81%
54    let k6 = 2.625 * r; // -- 95.45%
55    let k0 = 7.5625;
56
57    if p < k1 {
58      k0 * p * p
59    } else if p < k2 {
60      t = p - k3;
61
62      k0 * t * t + 0.75 // ------- 48/64
63    } else if p < k4 {
64      t = p - k5;
65
66      k0 * t * t + 0.9375 // ----- 60/64
67    } else {
68      t = p - k6;
69
70      k0 * t * t + 0.984375 // --- 63/64
71    }
72  }
73}
74
75#[test]
76fn test_out_bounce() {
77  let p = OutBounce.y(1.0);
78
79  assert_eq!(p, 1.0);
80}
81
82/// ### The [`InOutBounce`] Easing Function.
83///
84/// #### examples.
85///
86/// ```rust
87/// use eazy::Curve;
88/// use eazy::standard::bounce::InOutBounce;
89///
90/// let p = InOutBounce.y(1.0);
91/// ```
92#[derive(Debug)]
93pub struct InOutBounce;
94
95impl Curve for InOutBounce {
96  #[inline(always)]
97  fn y(&self, p: f32) -> f32 {
98    let t = p * 2.0;
99
100    if t < 1.0 {
101      return 0.5 - 0.5 * OutBounce.y(1.0 - t);
102    }
103
104    0.5 + 0.5 * OutBounce.y(t - 1.0)
105  }
106}
107
108#[test]
109fn test_in_out_bounce() {
110  let p = InOutBounce.y(1.0);
111
112  assert_eq!(p, 1.0);
113}