use core::ops::{AddAssign, Mul, MulAssign, Add}; /// 2D point (x, y) #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub struct Point2 { /// x axis distance. pub x: T, /// y axis distance. pub y: T, } impl AddAssign for Point2 where T: AddAssign, { fn add_assign(&mut self, rhs: Self) { self.x += rhs.x; self.y += rhs.y; } } impl Add for Point2 where T: Add, { type Output = Self; fn add(self, rhs: Self) -> Self { Self { x: self.x + rhs.x, y: self.y + rhs.y, } } } impl Add<&Self> for Point2 where T: Add + Clone, { type Output = Self; fn add(self, rhs: &Self) -> Self { Self { x: self.x + rhs.x.clone(), y: self.y + rhs.y.clone(), } } } impl Add for &Point2 where T: Add + Clone, { type Output = Point2; fn add(self, rhs: Self) -> Point2 { Point2 { x: self.x.clone() + rhs.x.clone(), y: self.y.clone() + rhs.y.clone(), } } } impl AddAssign<&Point2> for Point2 where T: AddAssign + Copy, { fn add_assign(&mut self, rhs: &Point2) { self.x += rhs.x; self.y += rhs.y; } } impl AddAssign> for Point2 where T: AddAssign + Copy, { fn add_assign(&mut self, rhs: Vector2) { self.x += rhs.x; self.y += rhs.y; } } /// 2D size (width, height) #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] pub struct Size2 { /// Width component pub width: T, /// Height component pub height: T, } /// 2D vector (x, y) #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] pub struct Vector2 { /// x axis magnitude. pub x: T, /// y axis magnitude. pub y: T, } impl Mul for Vector2 where T: MulAssign + Copy, { type Output = Vector2; fn mul(mut self, rhs: T) -> Self { self.x *= rhs; self.y *= rhs; self } }