eazy_data/easing/trigonometric/
sine.rs

1//! # The Sine Curve.
2
3use crate::easing::Curve;
4
5use libm::{cosf, sinf};
6
7use core::f32::consts::PI;
8
9/// ### The [`InSine`] Easing Function.
10///
11/// #### examples.
12///
13/// ```rust
14/// use eazy::Curve;
15/// use eazy::trigonometric::sine::InSine;
16///
17/// let p = InSine.y(0.5);
18///
19/// assert_eq!(p, 0.29289323);
20/// ```
21#[derive(Debug)]
22pub struct InSine;
23
24impl Curve for InSine {
25  #[inline(always)]
26  fn y(&self, p: f32) -> f32 {
27    1.0 - cosf(p * PI * 0.5)
28  }
29}
30
31#[test]
32fn test_in_sine() {
33  let p = InSine.y(0.5);
34
35  assert_eq!(p, 0.29289323);
36}
37
38/// ### The [`OutSine`] Easing Function.
39///
40/// Also see [`Curve`].
41///
42/// #### examples.
43///
44/// ```rust
45/// use eazy::Curve;
46/// use eazy::trigonometric::sine::OutSine;
47///
48/// let p = OutSine.y(0.1264);
49///
50/// assert_eq!(p, 0.1972467);
51/// ```
52#[derive(Debug)]
53pub struct OutSine;
54
55impl Curve for OutSine {
56  #[inline(always)]
57  fn y(&self, p: f32) -> f32 {
58    sinf(p * PI * 0.5)
59  }
60}
61
62#[test]
63fn test_out_sine() {
64  let p = OutSine.y(0.1264);
65
66  assert_eq!(p, 0.1972467);
67}
68
69/// ### The [`InOutSine`] Easing Function.
70///
71/// #### examples.
72///
73/// ```rust
74/// use eazy::Curve;
75/// use eazy::trigonometric::sine::InOutSine;
76///
77/// let p = InOutSine.y(0.248608);
78///
79/// assert_eq!(p, 0.14490387);
80/// ```
81#[derive(Debug)]
82pub struct InOutSine;
83
84impl Curve for InOutSine {
85  #[inline(always)]
86  fn y(&self, p: f32) -> f32 {
87    0.5 * (1.0 - cosf(p * PI))
88  }
89}
90
91#[test]
92fn test_in_out_sine() {
93  let p = InOutSine.y(0.248608);
94
95  assert_eq!(p, 0.14490387);
96}