diff --git a/Cargo.lock b/Cargo.lock index 7232d0a..33126b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "approx" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" +dependencies = [ + "num-traits", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -424,6 +433,7 @@ checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" name = "wipe" version = "2.0.0" dependencies = [ + "approx", "crossterm", "mockall", ] diff --git a/Cargo.toml b/Cargo.toml index 6bd82a1..c9e42c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ authors = ["Rico Riedel"] crossterm = "0.24" [dev-dependencies] -mockall = "0.11" \ No newline at end of file +mockall = "0.11" +approx = "0.5" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index d0d8b73..c6314e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ pub mod error; +mod pattern; pub mod printer; pub mod term; +mod vec; fn main() -> Result<(), error::Error> { Ok(()) diff --git a/src/vec.rs b/src/vec.rs new file mode 100644 index 0000000..abf8e33 --- /dev/null +++ b/src/vec.rs @@ -0,0 +1,69 @@ +use std::ops::Sub; + +#[derive(Copy, Clone, PartialEq, Debug)] +pub struct Vector { + pub x: f32, + pub y: f32, +} + +impl Vector { + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } + + pub fn len(&self) -> f32 { + (self.x * self.x + self.y * self.y).sqrt() + } + + pub fn center(&self) -> Vector { + Self::new(self.x / 2.0, self.y / 2.0) + } + + pub fn angle(&self) -> f32 { + self.y.atan2(self.x) + } +} + +impl Sub for Vector { + type Output = Vector; + + fn sub(self, rhs: Self) -> Self::Output { + Self::new(self.x - rhs.x, self.y - rhs.y) + } +} + +#[cfg(test)] +mod test { + use super::*; + use approx::*; + + #[test] + fn new() { + let v = Vector::new(4.0, 7.0); + assert_eq!(4.0, v.x); + assert_eq!(7.0, v.y); + } + + #[test] + fn len() { + assert_abs_diff_eq!(8.5, Vector::new(3.0, 8.0).len(), epsilon = 0.1); + } + + #[test] + fn center() { + assert_eq!(Vector::new(4.0, 9.0), Vector::new(8.0, 18.0).center()); + } + + #[test] + fn angle() { + assert_abs_diff_eq!(-1.5, Vector::new(2.0, -20.0).angle(), epsilon = 0.1); + } + + #[test] + fn sub() { + assert_eq!( + Vector::new(-5.0, 10.0), + Vector::new(3.0, 16.0) - Vector::new(8.0, 6.0) + ); + } +}