pub type Vec2 = maths_rs::vec::Vec2<f64>;
pub fn vec2(x: f64, y: f64) -> Vec2 {
Vec2::new(x, y)
}
pub trait Vec2Extras {
fn length(self) -> f64;
fn normalize(self) -> Vec2;
fn distance(self, other: Vec2) -> f64;
fn dot(self, other: Vec2) -> f64;
fn angle(self) -> f64;
fn rotate(self, angle: f64) -> Vec2;
}
impl Vec2Extras for Vec2 {
fn length(self) -> f64 {
(self.x * self.x + self.y * self.y).sqrt()
}
fn normalize(self) -> Vec2 {
self / self.length()
}
fn distance(self, other: Vec2) -> f64 {
(self - other).length()
}
fn dot(self, other: Vec2) -> f64 {
self.x * other.x + self.y * other.y
}
fn angle(self) -> f64 {
let mut a = self.y.atan2(self.x);
if a < 0.0 {
a += std::f64::consts::TAU;
}
a
}
fn rotate(self, angle: f64) -> Vec2 {
let cos = angle.cos();
let sin = angle.sin();
Vec2 {
x: self.x * cos - self.y * sin,
y: self.x * sin + self.y * cos,
}
}
}