From 8c81561f9eb2922e16e4ce2b81a2b25b2e51ac0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sat, 30 Jan 2021 20:22:50 -0300 Subject: Remove intermediate Lerp object Now polynomials can be created directly from bezier components. --- src/lerp.rs | 87 ------------------------------------------------------------- src/lib.rs | 33 ++++++++++++++++++----- 2 files changed, 27 insertions(+), 93 deletions(-) delete mode 100644 src/lerp.rs (limited to 'src') diff --git a/src/lerp.rs b/src/lerp.rs deleted file mode 100644 index b5b771d..0000000 --- a/src/lerp.rs +++ /dev/null @@ -1,87 +0,0 @@ -use crate::poly::Poly; -use std::rc::Rc; - -#[derive(PartialEq, Debug)] -pub enum Lerp { - Node(Rc, Rc), - Leaf(f32, f32), - Just(f32), -} - -impl Lerp { - pub fn new(v: &Vec) -> Rc { - match v.len() { - 0 => Rc::new(Lerp::Just(0.0)), - 1 => Rc::new(Lerp::Just(v[0])), - 2 => Rc::new(Lerp::Leaf(v[0], v[1])), - _ => { - let mut lv = Vec::new(); - for i in 0..v.len() - 1 { - lv.push(Rc::new(Lerp::Leaf(v[i], v[i + 1]))); - } - while lv.len() > 1 { - let mut nlv = Vec::new(); - for i in 0..lv.len() - 1 { - nlv.push(Rc::new(Lerp::Node(lv[i].clone(), lv[i + 1].clone()))); - } - lv = nlv; - } - lv[0].clone() - } - } - } - - 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.shift() - } - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn new2_test() { - let a = Lerp::new(&vec![1.0, 2.0]); - assert_eq!(a, Rc::new(Lerp::Leaf(1.0, 2.0))); - } - - #[test] - fn new3_test() { - let a = Lerp::new(&vec![1.0, 2.0, 3.0]); - assert_eq!( - a, - Rc::new(Lerp::Node( - Rc::new(Lerp::Leaf(1.0, 2.0)), - Rc::new(Lerp::Leaf(2.0, 3.0)) - )) - ); - } - - #[test] - fn new4_test() { - let a = Lerp::new(&vec![1.0, 2.0, 3.0, 4.0]); - assert_eq!( - a, - Rc::new(Lerp::Node( - Rc::new(Lerp::Node( - Rc::new(Lerp::Leaf(1.0, 2.0)), - Rc::new(Lerp::Leaf(2.0, 3.0)) - )), - Rc::new(Lerp::Node( - Rc::new(Lerp::Leaf(2.0, 3.0)), - Rc::new(Lerp::Leaf(3.0, 4.0)) - )) - )) - ); - } -} diff --git a/src/lib.rs b/src/lib.rs index db5d42f..218d5b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,5 @@ -mod lerp; mod poly; -use lerp::Lerp; use poly::Poly; use std::ffi::{CStr, CString}; @@ -18,6 +16,29 @@ pub struct Bezier { pub dpy: Poly, } +pub fn to_poly(v: &Vec) -> Poly { + match v.len() { + 0 => Poly::new(vec![0.0]), + 1 => Poly::new(vec![v[0]]), + 2 => Poly::new(vec![v[0], v[1] - v[0]]), + _ => { + let mut pv = Vec::new(); + for i in 0..v.len() - 1 { + pv.push(Poly::new(vec![v[i], v[i + 1] - v[i]])); + } + while pv.len() > 1 { + let mut npv = Vec::new(); + for i in 0..pv.len() - 1 { + let c = &pv[i + 1] - &pv[i]; + npv.push(&pv[i] + &c.shift()); + } + pv = npv; + } + pv[0].clone() + } + } +} + impl Bezier { pub fn new() -> Bezier { Bezier { @@ -34,8 +55,8 @@ impl Bezier { pub fn push(&mut self, x: i32, y: i32) { self.vx.push(x as f32); self.vy.push(y as f32); - self.px = Lerp::new(&self.vx).to_poly(); - self.py = Lerp::new(&self.vy).to_poly(); + self.px = to_poly(&self.vx); + self.py = to_poly(&self.vy); self.dpx = self.px.deriv(); self.dpy = self.py.deriv(); self.degree += 1; @@ -44,8 +65,8 @@ impl Bezier { pub fn remove(&mut self, index: usize) { self.vx.remove(index); self.vy.remove(index); - self.px = Lerp::new(&self.vx).to_poly(); - self.py = Lerp::new(&self.vy).to_poly(); + self.px = to_poly(&self.vx); + self.py = to_poly(&self.vy); self.dpx = self.px.deriv(); self.dpy = self.py.deriv(); self.degree -= 1; -- cgit v1.2.3