diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-30 20:22:50 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-30 20:22:50 -0300 |
commit | 8c81561f9eb2922e16e4ce2b81a2b25b2e51ac0f (patch) | |
tree | 5e1f70f9fbad4fb25a0b9c66bd7c65c0823a3d70 /src/lib.rs | |
parent | fc22d14dfdb041797db66a9232b3beb06fada08d (diff) | |
download | bezier-8c81561f9eb2922e16e4ce2b81a2b25b2e51ac0f.tar.gz bezier-8c81561f9eb2922e16e4ce2b81a2b25b2e51ac0f.zip |
Remove intermediate Lerp object
Now polynomials can be created directly from bezier components.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 33 |
1 files changed, 27 insertions, 6 deletions
@@ -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<f32>) -> 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; |