eazy_data/interpolation/piecewize/
polynomial.rs

1//! The Piecewize Polynomial Curve.
2
3use crate::easing::Curve;
4
5use libm::powf;
6
7/// The [`InPiecewizePolynomial`] Curve.
8#[derive(Debug)]
9pub struct InPiecewizePolynomial;
10
11impl Curve for InPiecewizePolynomial {
12  #[inline(always)]
13  fn y(&self, p: f32) -> f32 {
14    if p < 0.5 {
15      return 4.0 * powf(p, 3.0);
16    }
17
18    1.0 - powf(-2.0 * p + 2.0, 2.0) / 2.0
19  }
20}
21
22#[test]
23fn test_in_piecewize_polynomial() {
24  let p = InPiecewizePolynomial.y(1.0);
25
26  assert_eq!(p, 1.0)
27}
28
29/// The [`OutPiecewizePolynomial`] Curve.
30#[derive(Debug)]
31pub struct OutPiecewizePolynomial;
32
33impl Curve for OutPiecewizePolynomial {
34  #[inline(always)]
35  fn y(&self, p: f32) -> f32 {
36    if p < 0.5 {
37      return powf(2.0 * p, 2.0) / 2.0;
38    }
39
40    1.0 - powf(-2.0 * p + 2.0, 3.0) / 2.0
41  }
42}
43
44#[test]
45fn test_out_piecewize_polynomial() {
46  let p = OutPiecewizePolynomial.y(1.0);
47
48  assert_eq!(p, 1.0)
49}