use std::fmt::{self, Display, Formatter}; use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Sub, SubAssign}; /// Just a vector #[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)] pub struct Vector2 { pub x: f32, pub y: f32, } impl Display for Vector2 { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "[{}, {}]", self.x, self.y) } } impl Vector2 { pub fn new(x: f32, y: f32) -> Self { Self { x, y } } pub fn abs(self) -> Self { Self { x: self.x.abs(), y: self.y.abs(), } } } impl Add for Vector2 { type Output = Vector2; fn add(self, o: Vector2) -> Self::Output { Vector2 { x: self.x + o.x, y: self.y + o.y, } } } impl AddAssign for Vector2 { fn add_assign(&mut self, o: Self) { *self = Self { x: self.x + o.x, y: self.y + o.y, } } } impl Sub for Vector2 { type Output = Vector2; fn sub(self, o: Vector2) -> Self::Output { Vector2 { x: self.x - o.x, y: self.y - o.y, } } } impl SubAssign for Vector2 { fn sub_assign(&mut self, o: Self) { *self = Self { x: self.x - o.x, y: self.y - o.y, } } } impl Mul for Vector2 { type Output = Vector2; fn mul(self, o: Vector2) -> Self::Output { Vector2 { x: self.x * o.x, y: self.y * o.y, } } } impl Div for Vector2 { type Output = Vector2; fn div(self, o: f32) -> Self::Output { Vector2 { x: self.x / o, y: self.y / o, } } } impl Mul for Vector2 { type Output = Vector2; fn mul(self, o: f32) -> Self::Output { Self { x: self.x * o, y: self.y * o, } } } impl MulAssign for Vector2 { fn mul_assign(&mut self, o: Self) { *self = Self { x: self.x * o.x, y: self.y * o.y, } } } impl From for mint::Point2 { fn from(v: Vector2) -> Self { [v.x, v.y].into() } } impl From> for Vector2 { fn from(v: mint::Point2) -> Self { Self { x: v.x, y: v.y } } }