diff options
| author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-07 11:57:02 -0300 | 
|---|---|---|
| committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-07 11:57:02 -0300 | 
| commit | ad4c09192e6144e3e914748f9ce4edf371cf75b8 (patch) | |
| tree | 6488e069da854096cbce1465907484fe4b47f5f3 /src | |
| download | bezier-ad4c09192e6144e3e914748f9ce4edf371cf75b8.tar.gz bezier-ad4c09192e6144e3e914748f9ce4edf371cf75b8.zip | |
Initial commit
Starting off with a function able to turn recursive linear
interpolations into their corresponding polynomial.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 53 | ||||
| -rw-r--r-- | src/main.rs | 5 | 
2 files changed, 58 insertions, 0 deletions
| diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..31f230e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,53 @@ +pub type Number = f32; + +pub type Poly = Vec<Number>; + +#[derive(Debug)] +pub enum Lerp { +    Node(Box<Lerp>, Box<Lerp>), +    Leaf(Number, Number) +} + +impl Lerp { +    pub fn new(v: Vec<Number>) -> Box<Lerp> { +        Lerp::new_s(&v[..]) +    } + +    fn new_s(v: &[Number]) -> Box<Lerp> { +        match v.len() { +            0 => Box::new(Lerp::Leaf(0.0, 0.0)), +            1 => Box::new(Lerp::Leaf(v[0], 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 lp(l: Box<Lerp>) -> Poly { +    match *l { +        Lerp::Leaf(a, b) => vec![a, b - a], +        Lerp::Node(a, b) => { +            let a = lp(a); +            let b = lp(b); +            let c = poly_sub(&b, &a); +            skewed_sum(a, c) +        } +    } +} + +fn poly_sub(a: &Poly, b: &Poly) -> Poly { +    let mut r = a.clone(); +    for i in 0..r.len() { +        r[i] -= b[i]; +    } +    r +} + +fn skewed_sum(a: Poly, b: Poly) -> Poly { +    let mut r = a.clone(); +    for i in 0..r.len() - 1 { +        r[i + 1] += b[i]; +    } +    r.push(b[b.len() - 1]); +    r +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1e4f704 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +fn main() { +    let a = poly::Lerp::new(vec![1.0, 5.0, 8.0, 3.0, 10.0]); +    println!("{:?}", a); +    println!("{:?}", poly::lp(a)); +} | 
