use std::ops::{Add, Div, Mul, Sub};
#[derive(Clone, Copy, Debug)]
pub struct Vec2 {
pub x: f64,
pub y: f64,
}
impl Vec2 {
pub fn zero() -> Self {
Vec2 { x: 0., y: 0. }
}
pub fn new(x: f64, y: f64) -> Self {
Vec2 { x, y }
}
pub fn dot(self, other: Self) -> f64 {
self.x * other.x + self.y * other.y
}
pub fn modulus(self) -> f64 {
self.dot(self).sqrt()
}
pub fn distance(self, other: Self) -> f64 {
((self.x - other.x) * (self.x - other.x) + (self.y - other.y) * (self.y - other.y)).sqrt()
}
pub fn unit(self) -> Self {
self / self.length()
}
pub fn length(self) -> f64 {
(self.x * self.x + self.y * self.y).sqrt()
}
}
impl Add for Vec2 {
type Output = Self;
fn add(self, other: Self) -> Self {
Vec2 {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
impl Sub for Vec2 {
type Output = Self;
fn sub(self, other: Self) -> Self {
Vec2 {
x: self.x - other.x,
y: self.y - other.y,
}
}
}
impl Mul<f64> for Vec2 {
type Output = Self;
fn mul(self, other: f64) -> Self {
Vec2 {
x: self.x * other,
y: self.y * other,
}
}
}
impl Div<f64> for Vec2 {
type Output = Self;
fn div(self, other: f64) -> Self {
Vec2 {
x: self.x / other,
y: self.y / other,
}
}
}