use std::ops::{Add, Sub, Mul, Div};
use super::vector3::Vector3;
#[derive(Debug, Copy, Clone)]
pub struct Vector2 {
pub x: f64,
pub y: f64,
}
impl Vector2 {
pub fn new(x: f64, y: f64) -> Self {
Vector2 { x, y }
}
pub fn squared_magnitude(&self) -> f64 {
self.x.powi(2) + self.y.powi(2)
}
pub fn magnitude(&self) -> f64 {
self.squared_magnitude().sqrt()
}
pub fn dot_product(&self, rhs: &Self) -> f64 {
(self.x * rhs.x) + (self.y * rhs.y)
}
pub fn cross_product_magnitude(&self, &rhs: &Self) -> f64 {
(self.x * rhs.y) - (self.y * rhs.x)
}
pub fn cross_product(&self, rhs: &Self) -> Vector3 {
Vector3 {
x: 0.0, y: 0.0,
z: self.cross_product_magnitude(rhs)
}
}
}
impl Add for Vector2 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Vector2 {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl Sub for Vector2 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Vector2 {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl Mul<f64> for Vector2 {
type Output = Self;
fn mul(self, scalar: f64) -> Self::Output {
Vector2 {
x: self.x * scalar,
y: self.y * scalar,
}
}
}
impl Div<f64> for Vector2 {
type Output = Self;
fn div(self, scalar: f64) -> Self::Output {
Vector2 {
x: self.x / scalar,
y: self.y / scalar,
}
}
}