eazy_data/easing/exponential/
expo2.rs

1//! # The Exponent 2 Curve.
2//!
3//! #### notes.
4//!
5//! `Exponent2` needs clamping for `0` and `1`.
6//! Use exp2f instead of powf(2.0, x) - exp2f is optimized for base 2.
7
8use crate::easing::Curve;
9
10use libm::exp2f;
11
12/// ### The [`InExpo2`] Easing Function.
13///
14/// #### examples.
15///
16/// ```rust
17/// use eazy::Curve;
18/// use eazy::exponential::expo2::InExpo2;
19///
20/// let p = InExpo2.y(1.0);
21/// ```
22#[derive(Debug)]
23pub struct InExpo2;
24
25impl Curve for InExpo2 {
26  #[inline(always)]
27  fn y(&self, p: f32) -> f32 {
28    if p <= 0.0 {
29      return 0.0;
30    }
31
32    exp2f(10.0 * (p - 1.0))
33  }
34}
35
36#[test]
37fn test_in_expo2() {
38  let p = InExpo2.y(1.0);
39
40  assert_eq!(p, 1.0);
41}
42
43/// ### The [`OutExpo2`] Easing Function.
44///
45/// #### examples.
46///
47/// ```rust
48/// use eazy::Curve;
49/// use eazy::exponential::expo2::OutExpo2;
50///
51/// let p = OutExpo2.y(1.0);
52/// ```
53#[derive(Debug)]
54pub struct OutExpo2;
55
56impl Curve for OutExpo2 {
57  #[inline(always)]
58  fn y(&self, p: f32) -> f32 {
59    if p >= 1.0 {
60      return 1.0;
61    }
62
63    1.0 - exp2f(-10.0 * p)
64  }
65}
66
67#[test]
68fn test_out_expo2() {
69  let p = OutExpo2.y(1.0);
70
71  assert_eq!(p, 1.0);
72}
73
74/// ### The [`InOutExpo2`] Easing Function.
75///
76/// #### examples.
77///
78/// ```rust
79/// use eazy::Curve;
80/// use eazy::exponential::expo2::InOutExpo2;
81///
82/// let p = InOutExpo2.y(1.0);
83/// ```
84#[derive(Debug)]
85pub struct InOutExpo2;
86
87impl Curve for InOutExpo2 {
88  #[inline(always)]
89  fn y(&self, p: f32) -> f32 {
90    if p <= 0.0 {
91      return 0.0;
92    }
93
94    if p >= 1.0 {
95      return 1.0;
96    }
97
98    if p < 0.5 {
99      return exp2f(10.0 * (2.0 * p - 1.0) - 1.0);
100    }
101
102    1.0 - exp2f(-10.0 * (2.0 * p - 1.0) - 1.0)
103  }
104}
105
106#[test]
107fn test_in_out_expo2() {
108  let p = InOutExpo2.y(1.0);
109
110  assert_eq!(p, 1.0);
111}