/// A 2D vector. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub struct Vector { /// The X component of the [`Vector`] pub x: T, /// The Y component of the [`Vector`] pub y: T, } impl Vector { /// Creates a new [`Vector`] with the given components. pub const fn new(x: T, y: T) -> Self { Self { x, y } } } impl Vector { /// The zero [`Vector`]. pub const ZERO: Self = Self::new(0.0, 0.0); } impl std::ops::Neg for Vector where T: std::ops::Neg, { type Output = Self; fn neg(self) -> Self::Output { Self::new(-self.x, -self.y) } } impl std::ops::Add for Vector where T: std::ops::Add, { type Output = Self; fn add(self, b: Self) -> Self { Self::new(self.x + b.x, self.y + b.y) } } impl std::ops::AddAssign for Vector where T: std::ops::AddAssign, { fn add_assign(&mut self, b: Self) { self.x += b.x; self.y += b.y; } } impl std::ops::Sub for Vector where T: std::ops::Sub, { type Output = Self; fn sub(self, b: Self) -> Self { Self::new(self.x - b.x, self.y - b.y) } } impl std::ops::SubAssign for Vector where T: std::ops::SubAssign, { fn sub_assign(&mut self, b: Self) { self.x -= b.x; self.y -= b.y; } } impl std::ops::Mul for Vector where T: std::ops::Mul + Copy, { type Output = Self; fn mul(self, scale: T) -> Self { Self::new(self.x * scale, self.y * scale) } } impl std::ops::MulAssign for Vector where T: std::ops::MulAssign + Copy, { fn mul_assign(&mut self, scale: T) { self.x *= scale; self.y *= scale; } } impl std::ops::Div for Vector where T: std::ops::Div + Copy, { type Output = Self; fn div(self, scale: T) -> Self { Self::new(self.x / scale, self.y / scale) } } impl std::ops::DivAssign for Vector where T: std::ops::DivAssign + Copy, { fn div_assign(&mut self, scale: T) { self.x /= scale; self.y /= scale; } } impl From<[T; 2]> for Vector { fn from([x, y]: [T; 2]) -> Self { Self::new(x, y) } } impl From> for [T; 2] where T: Copy, { fn from(other: Vector) -> Self { [other.x, other.y] } }