mirror of
https://github.com/ricoriedel/wipe.git
synced 2024-11-15 20:46:40 +00:00
Add vector
This commit is contained in:
parent
8cd6e4d50f
commit
afe3dc4122
10
Cargo.lock
generated
10
Cargo.lock
generated
@ -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",
|
||||
]
|
||||
|
@ -11,3 +11,4 @@ crossterm = "0.24"
|
||||
|
||||
[dev-dependencies]
|
||||
mockall = "0.11"
|
||||
approx = "0.5"
|
@ -1,6 +1,8 @@
|
||||
pub mod error;
|
||||
mod pattern;
|
||||
pub mod printer;
|
||||
pub mod term;
|
||||
mod vec;
|
||||
|
||||
fn main() -> Result<(), error::Error> {
|
||||
Ok(())
|
||||
|
69
src/vec.rs
Normal file
69
src/vec.rs
Normal file
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user