diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-10 19:54:34 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-10 19:54:34 -0300 |
commit | 7fa34698ba8ee80227fa0b8eaab7a7382a5cf465 (patch) | |
tree | 2a2b8bbfd2de5c93744629e0a16bc82f53e63273 /src | |
parent | 6d356f0c681cb2bb0f26d848ed467f65c49769a5 (diff) | |
download | bezier-7fa34698ba8ee80227fa0b8eaab7a7382a5cf465.tar.gz bezier-7fa34698ba8ee80227fa0b8eaab7a7382a5cf465.zip |
Separate project into modules
Diffstat (limited to 'src')
-rw-r--r-- | src/lerp.rs | 33 | ||||
-rw-r--r-- | src/main.rs | 32 | ||||
-rw-r--r-- | src/poly.rs (renamed from src/lib.rs) | 39 |
3 files changed, 53 insertions, 51 deletions
diff --git a/src/lerp.rs b/src/lerp.rs new file mode 100644 index 0000000..7c4994c --- /dev/null +++ b/src/lerp.rs @@ -0,0 +1,33 @@ +use crate::poly::{self, Number, Poly}; + +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); + poly::skewed_sum(a, c) + } + } +} diff --git a/src/main.rs b/src/main.rs index 8831976..8c23d3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,28 +1,22 @@ -use poly::Poly; -use poly::Number; -use poly::Lerp; +mod poly; +mod lerp; -use sdl2::pixels::Color; -use sdl2::event::Event; -use sdl2::keyboard::Keycode; -use sdl2::rect::Rect; +use crate::poly::Number; +use crate::lerp::Lerp; use std::thread; use std::time::Duration; -fn eval_poly(p: &Poly, t: Number) -> Number { - let mut r = 0.0; - for i in 0..p.len() { - r += p[i] * t.powi(i as i32); - } - r -} +use sdl2::pixels::Color; +use sdl2::event::Event; +use sdl2::keyboard::Keycode; +use sdl2::rect::Rect; fn main() { let a = Lerp::new(vec![100.0, 200.0, 300.0, 500.0]); let b = Lerp::new(vec![400.0, 140.0, 500.0, 300.0]); - let pa = poly::lp(a); - let pb = poly::lp(b); + let pa = lerp::lp(a); + let pb = lerp::lp(b); let p = poly::gcd(&pa, &pb); let sdl_context = sdl2::init().unwrap(); @@ -50,9 +44,9 @@ fn main() { } for t in 0..800 { - let x = eval_poly(&pa, t as Number / 800.0); - let y = eval_poly(&pb, t as Number / 800.0); - let z = eval_poly(&p, t as Number / 800.0); + let x = poly::eval_poly(&pa, t as Number / 800.0); + let y = poly::eval_poly(&pb, t as Number / 800.0); + let z = poly::eval_poly(&p, t as Number / 800.0); canvas.set_draw_color(Color::RGB(180, 20, 20)); canvas.fill_rect(Rect::new(t, x as i32, 5, 5)).unwrap(); canvas.set_draw_color(Color::RGB(20, 180, 20)); @@ -4,40 +4,15 @@ 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 = sub(&b, &a); - skewed_sum(a, c) - } +pub fn eval_poly(p: &Poly, t: Number) -> Number { + let mut r = 0.0; + for i in 0..p.len() { + r += p[i] * t.powi(i as i32); } + r } -fn sub(a: &Poly, b: &Poly) -> Poly { +pub fn sub(a: &Poly, b: &Poly) -> Poly { let mut r = a.clone(); for i in 0..r.len() { r[i] -= b[i]; @@ -45,7 +20,7 @@ fn sub(a: &Poly, b: &Poly) -> Poly { r } -fn skewed_sum(a: Poly, b: Poly) -> Poly { +pub 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]; |