use num::Signed; use std::cmp::{max, min}; use std::fmt::{Debug, Display, Formatter}; use std::ops::{Add, Mul, Sub}; use std::str::FromStr; #[derive(Copy, Clone, Default, Eq, Hash, PartialEq)] pub struct Point3D { pub x: T, pub y: T, pub z: T, } impl Point3D { pub fn new(x: T, y: T, z: T) -> Self { Self { x, y, z } } } pub fn manhattan_distance(l: &Point3D, r: &Point3D) -> T where T: Signed + std::cmp::Ord + Copy, { max(l.x, r.x).abs_sub(&min(l.x, r.x)) + max(l.y, r.y).abs_sub(&min(l.y, r.y)) + max(l.z, r.z).abs_sub(&min(l.z, r.z)) } pub fn euclidean_distance_squared(l: Point3D, r: Point3D) -> T where T: Sub + Mul + Add + Copy, T: Signed, { (r - l) * (r - l) } impl FromStr for Point3D where T: std::str::FromStr, ::Err: std::fmt::Debug, { type Err = std::string::ParseError; fn from_str(s: &str) -> Result { let mut split = s.split(','); Ok(Point3D { x: split .next() .unwrap() .parse() .expect("Failed to parse coordinate"), y: split .next() .unwrap() .parse() .expect("Failed to parse coordinate"), z: split .next() .unwrap() .parse() .expect("Failed to parse coordinate"), }) } } impl Display for Point3D where T: Display, { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let Point3D { x, y, z } = &self; write!(f, "({x},{y},{z})") } } impl Debug for Point3D where T: Debug, { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let Point3D { x, y, z } = &self; write!(f, "({x:?},{y:?},{z:?})") } } impl Add for Point3D where T: Add, { type Output = Self; fn add(self, rhs: Self) -> Self::Output { Self { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z, } } } impl Add for Point3D where T: Add + Copy, { type Output = Self; fn add(self, rhs: T) -> Self::Output { Self { x: self.x + rhs, y: self.y + rhs, z: self.z + rhs, } } } impl Sub for Point3D where T: Sub, T: Signed, { type Output = Self; fn sub(self, rhs: Self) -> Self::Output { Self { x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z, } } } impl Mul for Point3D where T: Mul + Copy, { type Output = Self; fn mul(self, rhs: T) -> Self::Output { Self { x: self.x * rhs, y: self.y * rhs, z: self.z * rhs, } } } impl Mul for Point3D where T: Mul + Add, { type Output = T; fn mul(self, rhs: Self) -> Self::Output { (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z) } }