diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lerp.rs | 7 | ||||
-rw-r--r-- | src/main.rs | 36 | ||||
-rw-r--r-- | src/poly.rs | 27 | ||||
-rw-r--r-- | src/poly/iter.rs | 10 |
4 files changed, 52 insertions, 28 deletions
diff --git a/src/lerp.rs b/src/lerp.rs index 2ea444e..222671f 100644 --- a/src/lerp.rs +++ b/src/lerp.rs @@ -3,7 +3,7 @@ use crate::poly::Poly; pub enum Lerp { Node(Box<Lerp>, Box<Lerp>), - Leaf(Number, Number) + Leaf(Number, Number), } impl Lerp { @@ -16,7 +16,10 @@ impl Lerp { 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()]))) + _ => Box::new(Lerp::Node( + Lerp::new_s(&v[0..v.len() - 1]), + Lerp::new_s(&v[1..v.len()]), + )), } } } diff --git a/src/main.rs b/src/main.rs index bd7e1a2..7424f7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,16 @@ -mod poly; -mod number; mod lerp; +mod number; +mod poly; -use number::Number; use lerp::Lerp; +use number::Number; use std::thread; use std::time::Duration; -use sdl2::pixels::Color; use sdl2::event::Event; use sdl2::keyboard::Keycode; +use sdl2::pixels::Color; use sdl2::rect::Rect; fn main() { @@ -23,7 +23,8 @@ fn main() { let sdl_context = sdl2::init().unwrap(); let video_subsystem = sdl_context.video().unwrap(); - let window = video_subsystem.window("rust-sdl2 demo", 800, 600) + let window = video_subsystem + .window("rust-sdl2 demo", 800, 600) .position_centered() .build() .unwrap(); @@ -36,10 +37,11 @@ fn main() { canvas.clear(); for event in event_pump.poll_iter() { match event { - Event::Quit {..} | - Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { - break 'running - }, + Event::Quit { .. } + | Event::KeyDown { + keycode: Some(Keycode::Escape), + .. + } => break 'running, _ => {} } } @@ -51,13 +53,21 @@ fn main() { let y = k + s * pb.eval(t as Number / 100.0); let z = k + s * p.eval(t as Number / 100.0); canvas.set_draw_color(Color::RGB(180, 20, 20)); - canvas.fill_rect(Rect::new(400 + t, x as i32, 5, 5)).unwrap(); + canvas + .fill_rect(Rect::new(400 + t, x as i32, 5, 5)) + .unwrap(); canvas.set_draw_color(Color::RGB(20, 180, 20)); - canvas.fill_rect(Rect::new(400 + t, y as i32, 5, 5)).unwrap(); + canvas + .fill_rect(Rect::new(400 + t, y as i32, 5, 5)) + .unwrap(); canvas.set_draw_color(Color::RGB(20, 20, 180)); - canvas.fill_rect(Rect::new(400 + t, z as i32, 2, 2)).unwrap(); + canvas + .fill_rect(Rect::new(400 + t, z as i32, 2, 2)) + .unwrap(); canvas.set_draw_color(Color::RGB(255, 0, 0)); - canvas.fill_rect(Rect::new(400 + t, k as i32, 2, 2)).unwrap(); + canvas + .fill_rect(Rect::new(400 + t, k as i32, 2, 2)) + .unwrap(); } canvas.present(); diff --git a/src/poly.rs b/src/poly.rs index cb84d64..7d964a4 100644 --- a/src/poly.rs +++ b/src/poly.rs @@ -2,12 +2,12 @@ mod iter; use crate::number::Number; use iter::Iter; -use std::ops::{Add, Sub, Mul, Div}; +use std::ops::{Add, Div, Mul, Sub}; #[derive(PartialEq, Debug, Clone)] pub struct Poly { data: Vec<Number>, - degree: usize + degree: usize, } impl Poly { @@ -16,7 +16,7 @@ impl Poly { while data[i] == 0.0 && i > 0 { i -= 1; } - Poly { data, degree : i } + Poly { data, degree: i } } fn mono(degree: usize, coefficient: Number) -> Poly { @@ -52,7 +52,7 @@ impl Poly { fn is_zero(&self) -> bool { for i in 0..self.data.len() { if self.data[i] != 0.0 { - return false + return false; } } true @@ -63,7 +63,7 @@ impl Add for &Poly { type Output = Poly; fn add(self, other: Self) -> Poly { - Poly::new(self.iter().zip(other.iter()).map(|(x, y)| {x + y}).collect()) + Poly::new(self.iter().zip(other.iter()).map(|(x, y)| x + y).collect()) } } @@ -71,7 +71,7 @@ impl Sub for &Poly { type Output = Poly; fn sub(self, other: Self) -> Poly { - Poly::new(self.iter().zip(other.iter()).map(|(x, y)| {x - y}).collect()) + Poly::new(self.iter().zip(other.iter()).map(|(x, y)| x - y).collect()) } } @@ -82,9 +82,10 @@ impl Mul for &Poly { let mut r = Vec::new(); for i in 0..other.degree() + 1 { let mut prefix = vec![0.0; i]; - let mut suffix: Vec<Number> = self.iter() + let mut suffix: Vec<Number> = self + .iter() .take(self.degree() + 1) - .map(|x| {x * other.data[i]}) + .map(|x| x * other.data[i]) .collect(); prefix.append(&mut suffix); r.push(Poly::new(prefix)); @@ -139,14 +140,20 @@ mod tests { fn div_equal() { let a = Poly::new(vec![6.0, 7.0, 1.0]); let b = Poly::new(vec![-6.0, -5.0, 1.0]); - assert_eq!(&a / &b, (Poly::new(vec![1.0]), Poly::new(vec![12.0, 12.0, 0.0]))); + assert_eq!( + &a / &b, + (Poly::new(vec![1.0]), Poly::new(vec![12.0, 12.0, 0.0])) + ); } #[test] fn div_greater() { let a = Poly::new(vec![-6.0, -5.0, 1.0]); let b = Poly::new(vec![12.0, 12.0]); - assert_eq!(&a / &b, (Poly::new(vec![-0.5, 1.0/12.0]), Poly::new(vec![0.0, 0.0]))); + assert_eq!( + &a / &b, + (Poly::new(vec![-0.5, 1.0 / 12.0]), Poly::new(vec![0.0, 0.0])) + ); } #[test] diff --git a/src/poly/iter.rs b/src/poly/iter.rs index abc7462..059d7e3 100644 --- a/src/poly/iter.rs +++ b/src/poly/iter.rs @@ -1,16 +1,20 @@ use crate::number::Number; use std::cmp; -use std::iter::{Zip, Take}; +use std::iter::{Take, Zip}; pub struct Iter { index: usize, data: Vec<Number>, - degree: usize + degree: usize, } impl Iter { pub fn new(data: Vec<Number>, degree: usize) -> Iter { - Iter { index: 0, data, degree } + Iter { + index: 0, + data, + degree, + } } pub fn zip(self, other: Self) -> Zip<Take<Iter>, Take<Iter>> { |