use crate::formulas::dot::length;
use core::ops::{Add, Div, Mul, Sub};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Vec2d {
pub x: f32,
pub y: f32,
}
impl Vec2d {
#[must_use]
pub fn new(x: f32, y: f32) -> Self {
Vec2d { x, y }
}
#[must_use]
pub fn component(&self, target: Vec2d) -> Vec2d {
Vec2d {
x: (target.x - self.x),
y: (target.y - self.y),
}
}
#[must_use]
pub fn magnitude(&self) -> f32 {
length(*self)
}
#[must_use]
pub fn distance(&self, target: Vec2d) -> f32 {
let ab = self.component(target);
length(ab)
}
}
impl Add for Vec2d {
type Output = Vec2d;
fn add(self, rhs: Self) -> Self::Output {
Vec2d {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl Sub for Vec2d {
type Output = Vec2d;
fn sub(self, rhs: Self) -> Self::Output {
Vec2d {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl Mul for Vec2d {
type Output = f32;
fn mul(self, rhs: Self) -> Self::Output {
self.x * rhs.x + self.y * rhs.y
}
}
impl Div<f32> for Vec2d {
type Output = Vec2d;
fn div(self, rhs: f32) -> Self::Output {
Vec2d {
x: self.x / rhs,
y: self.y / rhs,
}
}
}
#[inline]
pub fn vec2<A: Into<f32>, B: Into<f32>>(a: A, b: B) -> Vec2d {
Vec2d::new(a.into(), b.into())
}