summaryrefslogtreecommitdiff
path: root/src/lerp.rs
blob: b6136209d4ee6a6d6860ddfc9efa64e248a0d5a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
pub mod poly;

use poly::Poly;

pub enum Lerp {
    Node(Box<Lerp>, Box<Lerp>),
    Leaf(f32, f32),
    Just(f32),
}

impl Lerp {
    pub fn new(v: Vec<f32>) -> Box<Lerp> {
        Lerp::new_s(&v[..])
    }

    fn new_s(v: &[f32]) -> Box<Lerp> {
        match v.len() {
            0 => Box::new(Lerp::Just(0.0)),
            1 => Box::new(Lerp::Just(v[0])),
            2 => Box::new(Lerp::Leaf(v[0], v[1])),
            _ => Box::new(Lerp::Node(
                Lerp::new_s(&v[0..v.len() - 1]),
                Lerp::new_s(&v[1..v.len()]),
            )),
        }
    }

    pub fn to_poly(self) -> Poly {
        match self {
            Lerp::Just(a) => Poly::new(vec![a]),
            Lerp::Leaf(a, b) => Poly::new(vec![a, b - a]),
            Lerp::Node(a, b) => {
                let a = a.to_poly();
                let b = b.to_poly();
                let c = &b - &a;
                &a + &(&c * &Poly::new(vec![0.0, 1.0]))
            }
        }
    }
}