pub struct Vector2D {
pub x: f64,
pub y: f64,
}
impl Vector2D {
pub fn new(x: f64, y: f64) -> Vector2D {
Vector2D { x, y }
}
pub fn magnitude(&self) -> f64 {
(self.x.powi(2) + self.y.powi(2)).sqrt()
}
pub fn add(self, other: Vector2D) -> Vector2D {
Vector2D { x: self.x + other.x, y: self.y + other.y }
}
pub fn subtract(self, other: Vector2D) -> Vector2D {
Vector2D { x: self.x - other.x, y: self.y - other.y }
}
pub fn dot_product(self, other: Vector2D) -> f64 {
self.x * other.x + self.y * other.y
}
pub fn cross_product(self, other: Vector2D) -> f64 {
self.x * other.y - self.y * other.x
}
}