updated the lib

Dieser Commit ist enthalten in:
Sebastian Tobie 2024-12-18 21:56:41 +01:00
Ursprung 16ae19ffa8
Commit 5f88cdc3b4

Datei anzeigen

@ -1,8 +1,8 @@
use log::debug; use log::debug;
use num::{traits::*, *}; use num::{traits::*, integer::*};
use std::{ use std::{
fmt::{Debug, Display}, fmt::{Debug, Display},
ops::Rem, ops::*,
}; };
pub mod direction; pub mod direction;
@ -46,18 +46,6 @@ fn wrap<T: Integer + Rem + MaximumValue + From<u32> + Copy + MaximumValue + Disp
} }
} }
impl Kartesian<usize> {
pub fn get_value<T: Copy>(self, map: &Vec<Vec<T>>) -> Option<T> {
if map.len() > self.x {
if map[self.x].len() > self.y {
return Some(map[self.x][self.y]);
}
}
None
}
}
impl<T: Integer + Eq + Debug + CheckedBasicMath + Default + Copy> Kartesian<T> { impl<T: Integer + Eq + Debug + CheckedBasicMath + Default + Copy> Kartesian<T> {
pub fn checked_add_max(self, rhs: Kartesian<T>, max: Kartesian<T>) -> Option<Kartesian<T>> { pub fn checked_add_max(self, rhs: Kartesian<T>, max: Kartesian<T>) -> Option<Kartesian<T>> {
let mut new = Kartesian::default(); let mut new = Kartesian::default();
@ -117,6 +105,7 @@ impl<T: Integer + Eq + Debug + CheckedBasicMath + Default + Copy> Kartesian<T> {
} }
#[inline] #[inline]
#[must_use]
pub fn move_dir(self, vector: Kartesian<T>, dir: KartesianDirection) -> Option<Kartesian<T>> { pub fn move_dir(self, vector: Kartesian<T>, dir: KartesianDirection) -> Option<Kartesian<T>> {
let mut new = self; let mut new = self;
match dir { match dir {
@ -153,6 +142,7 @@ impl<T: Integer + Eq + Debug + CheckedBasicMath + Default + Copy> Kartesian<T> {
} }
#[inline] #[inline]
#[must_use]
pub fn move_dir_max(self, vector: Kartesian<T>, dir: KartesianDirection, max: Kartesian<T>) -> Option<Kartesian<T>> { pub fn move_dir_max(self, vector: Kartesian<T>, dir: KartesianDirection, max: Kartesian<T>) -> Option<Kartesian<T>> {
match self.move_dir(vector, dir) { match self.move_dir(vector, dir) {
None => None, None => None,
@ -167,7 +157,20 @@ impl<T: Integer + Eq + Debug + CheckedBasicMath + Default + Copy> Kartesian<T> {
} }
} }
impl Kartesian<usize> {
pub fn get_value<T: Copy>(self, map: &Vec<Vec<T>>) -> Option<T> {
if map.len() > self.x {
if map[self.x].len() > self.y {
return Some(map[self.x][self.y]);
}
}
None
}
}
impl<T: Integer + Unsigned + CheckedBasicMath> Kartesian<T> { impl<T: Integer + Unsigned + CheckedBasicMath> Kartesian<T> {
#[must_use]
pub fn move_velocity(self, velocity: Velocity<T>, max: Kartesian<T>) -> Option<Kartesian<T>> { pub fn move_velocity(self, velocity: Velocity<T>, max: Kartesian<T>) -> Option<Kartesian<T>> {
if velocity.direction == KD::None { if velocity.direction == KD::None {
return Some(self); return Some(self);
@ -207,6 +210,23 @@ impl<T: Integer + Unsigned + CheckedBasicMath> Kartesian<T> {
} }
} }
impl<T: Integer + From<u16> + Copy + Add + Roots + Pow<T, Output = T>> Kartesian<T> {
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<T: Integer + Display + Unsigned + WrappingAdd + WrappingSub + MaximumValue + From<u32> + Copy> Kartesian<T> { impl<T: Integer + Display + Unsigned + WrappingAdd + WrappingSub + MaximumValue + From<u32> + Copy> Kartesian<T> {
pub fn wrapping_move_velocity(self, velocity: Velocity<T>, max: Kartesian<T>) -> Kartesian<T> { pub fn wrapping_move_velocity(self, velocity: Velocity<T>, max: Kartesian<T>) -> Kartesian<T> {
Kartesian { Kartesian {