diff options
author | tom barrett <spalf0@gmail.com> | 2019-02-10 03:55:05 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-02-10 03:55:05 -0600 |
commit | f617213b4a48d73acd245580f8551a7c37ce9ad8 (patch) | |
tree | 46ab5a696d91d6405ff2d2875a57c403b94edfbd /src/math.rs | |
parent | 46fa862e04bc43311ba79ef3db70abf9014b9104 (diff) |
added vector math and tractorbeam module
Diffstat (limited to 'src/math.rs')
-rw-r--r-- | src/math.rs | 91 |
1 files changed, 87 insertions, 4 deletions
diff --git a/src/math.rs b/src/math.rs index 689ee65..0816149 100644 --- a/src/math.rs +++ b/src/math.rs @@ -4,13 +4,96 @@ use self::rand::distributions::Alphanumeric; use self::rand::Rng; use std::iter::repeat; -pub fn distance(l0: (f64, f64, f64), l1: (f64, f64, f64)) -> f64 { - ((l1.0 - l0.0).powf(2.0) + (l1.1 - l0.1).powf(2.0) + (l1.2 - l0.2).powf(2.0)).sqrt() -} - pub fn rand_name() -> String { repeat(()) .map(|()| rand::thread_rng().sample(Alphanumeric)) .take(8) .collect() } + +#[derive(Serialize, Deserialize, Debug, Clone, Default)] +pub struct Vector { + pub a: f64, + pub b: f64, + pub c: f64, +} + +impl Vector { + pub fn new(v: (f64, f64, f64)) -> Vector { + Vector { + a: v.0, + b: v.1, + c: v.2, + } + } + + pub fn distance_from(&self, other: Vector) -> f64 { + ((self.a - other.a).powf(2.0) + (self.b - other.b).powf(2.0) + (self.c - other.c).powf(2.0)) + .sqrt() + } + + pub fn unitize(&self) -> Vector { + let denominator = self.magnitude(); + Vector { + a: self.a / denominator, + b: self.b / denominator, + c: self.c / denominator, + } + } + + pub fn magnitude(&self) -> f64 { + (self.a.powf(2.0) + self.b.powf(2.0) + self.c.powf(2.0)).sqrt() + } +} + +impl std::fmt::Display for Vector { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "({:.2}, {:.2}, {:.2})", self.a, self.b, self.c) + } +} + +impl std::ops::Add for Vector { + type Output = Vector; + + fn add(self, other: Vector) -> Vector { + Vector { + a: self.a + other.a, + b: self.b + other.b, + c: self.c + other.c, + } + } +} + +impl std::ops::Sub for Vector { + type Output = Vector; + + fn sub(self, other: Vector) -> Vector { + Vector { + a: self.a - other.a, + b: self.b - other.b, + c: self.c - other.c, + } + } +} + +impl std::ops::Mul<f64> for Vector { + type Output = Vector; + + fn mul(self, other: f64) -> Vector { + Vector { + a: self.a * other, + b: self.b * other, + c: self.c * other, + } + } +} + +impl std::ops::AddAssign for Vector { + fn add_assign(&mut self, other: Vector) { + *self = Vector { + a: self.a + other.a, + b: self.b + other.b, + c: self.c + other.c, + } + } +} |