Skip to main content

poline_rs/
fns.rs

1use std::f64::consts::PI;
2
3use crate::types::Transformer;
4
5pub enum PositionFn {
6    Linear,
7    Quadratic,
8    Cubic,
9    Quartic,
10    Quintic,
11    Sinusoidal,
12    Asinusoidal,
13    Arc,
14    SmoothStep,
15}
16
17impl PositionFn {
18    pub fn get_fn(&self) -> Transformer {
19        match self {
20            PositionFn::Linear => linear_fn,
21            PositionFn::Quadratic => quadratic_fn,
22            PositionFn::Cubic => cubic_fn,
23            PositionFn::Quartic => quartic_fn,
24            PositionFn::Quintic => quintic_fn,
25            PositionFn::Sinusoidal => sinusoidal_fn,
26            PositionFn::Asinusoidal => asinusoidal_fn,
27            PositionFn::Arc => arc_fn,
28            PositionFn::SmoothStep => smooth_step_fn,
29        }
30    }
31}
32
33fn linear_fn(t: f64, _inverted: bool) -> f64 {
34    t
35}
36
37fn quadratic_fn(t: f64, inverted: bool) -> f64 {
38    if inverted {
39        1f64 - (1f64 - t).powi(2)
40    } else {
41        t.powi(2)
42    }
43}
44
45fn cubic_fn(t: f64, inverted: bool) -> f64 {
46    if inverted {
47        1f64 - (1f64 - t).powi(3)
48    } else {
49        t.powi(3)
50    }
51}
52
53fn quartic_fn(t: f64, inverted: bool) -> f64 {
54    if inverted {
55        1f64 - (1f64 - t).powi(4)
56    } else {
57        t.powi(4)
58    }
59}
60
61fn quintic_fn(t: f64, inverted: bool) -> f64 {
62    if inverted {
63        1f64 - (1f64 - t).powi(5)
64    } else {
65        t.powi(5)
66    }
67}
68
69fn sinusoidal_fn(t: f64, inverted: bool) -> f64 {
70    if inverted {
71        1f64 - ((1f64 - t) * PI / 2f64).sin()
72    } else {
73        (t * PI / 2f64).sin()
74    }
75}
76
77fn asinusoidal_fn(t: f64, inverted: bool) -> f64 {
78    if inverted {
79        1f64 - (1f64 - t).asin() / (PI / 2f64)
80    } else {
81        t.asin() / (PI / 2f64)
82    }
83}
84
85fn arc_fn(t: f64, inverted: bool) -> f64 {
86    if inverted {
87        (1f64 - (1f64 - t).powi(2)).sqrt()
88    } else {
89        1f64 - (1f64 - t).sqrt()
90    }
91}
92
93fn smooth_step_fn(t: f64, _inverted: bool) -> f64 {
94    t.powi(2) * (3f64 - 2f64 * t)
95}