use crate::*; use std::fmt; use std::ops::{Add, Sub, AddAssign, SubAssign}; #[derive(Copy, Clone, PartialEq)] pub struct Point { pub x: O, pub y: O, } pub trait HasPosition { fn position(&self) -> Point; fn x(&self) -> O { self.position().x } fn y(&self) -> O { self.position().y } } impl Point { pub const ZERO: Self = Self { x: O::ZERO, y: O::ZERO }; pub const fn new(x: O, y: O) -> Point { Point { x, y } } pub fn is_zero(&self) -> bool { self.x == O::ZERO && self.y == O::ZERO } } impl HasPosition for Point { fn position(&self) -> Point { *self } } impl Add> for Point { type Output = Self; fn add(self, point: Point) -> Self { Point::new(self.x + point.x, self.y + point.y) } } impl Sub> for Point { type Output = Self; fn sub(self, point: Point) -> Self { Point::new(self.x - point.x, self.y - point.y) } } impl AddAssign> for Point { fn add_assign(&mut self, point: Point) { self.x += point.x; self.y += point.y; } } impl SubAssign> for Point { fn sub_assign(&mut self, point: Point) { self.x -= point.x; self.y -= point.y; } } impl fmt::Debug for Point { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Point {{ x:{:?} y:{:?} }}", self.x, self.y) } }