From 5f88cdc3b48c4beeb954d92d3f1931e84e0637e1 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Wed, 18 Dec 2024 21:56:41 +0100 Subject: [PATCH] updated the lib --- src/coordinate_systems/kartesian/mod.rs | 48 +++++++++++++++++-------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/coordinate_systems/kartesian/mod.rs b/src/coordinate_systems/kartesian/mod.rs index 0986f95..bd0689b 100644 --- a/src/coordinate_systems/kartesian/mod.rs +++ b/src/coordinate_systems/kartesian/mod.rs @@ -1,8 +1,8 @@ use log::debug; -use num::{traits::*, *}; +use num::{traits::*, integer::*}; use std::{ fmt::{Debug, Display}, - ops::Rem, + ops::*, }; pub mod direction; @@ -46,18 +46,6 @@ fn wrap + Copy + MaximumValue + Disp } } -impl Kartesian { - pub fn get_value(self, map: &Vec>) -> Option { - if map.len() > self.x { - if map[self.x].len() > self.y { - return Some(map[self.x][self.y]); - } - } - - None - } -} - impl Kartesian { pub fn checked_add_max(self, rhs: Kartesian, max: Kartesian) -> Option> { let mut new = Kartesian::default(); @@ -117,6 +105,7 @@ impl Kartesian { } #[inline] + #[must_use] pub fn move_dir(self, vector: Kartesian, dir: KartesianDirection) -> Option> { let mut new = self; match dir { @@ -153,6 +142,7 @@ impl Kartesian { } #[inline] + #[must_use] pub fn move_dir_max(self, vector: Kartesian, dir: KartesianDirection, max: Kartesian) -> Option> { match self.move_dir(vector, dir) { None => None, @@ -167,7 +157,20 @@ impl Kartesian { } } +impl Kartesian { + pub fn get_value(self, map: &Vec>) -> Option { + if map.len() > self.x { + if map[self.x].len() > self.y { + return Some(map[self.x][self.y]); + } + } + + None + } +} + impl Kartesian { + #[must_use] pub fn move_velocity(self, velocity: Velocity, max: Kartesian) -> Option> { if velocity.direction == KD::None { return Some(self); @@ -207,6 +210,23 @@ impl Kartesian { } } +impl + Copy + Add + Roots + Pow> Kartesian { + pub fn distance(self, other: Self) -> T { + let power = T::from(2); + let x = if self.x > other.x { + self.x - other.x + } else { + other.x - self.x + }; + let y = if self.y > other.y { + self.y - other.y + } else { + other.y - self.y + }; + (x.pow(power) + y.pow(power)).sqrt() + } +} + impl + Copy> Kartesian { pub fn wrapping_move_velocity(self, velocity: Velocity, max: Kartesian) -> Kartesian { Kartesian {