#![allow(dead_code)]
use crate::coords;
use std::ops;
#[derive(Clone, Copy, Debug)]
pub struct Vector {
pub start: coords::Coord,
pub end: coords::Coord,
pub magnitude: f64,
}
pub fn get_magnitude(start: coords::Coord, end: coords::Coord) -> f64 {
(((end.x - start.x).pow(2) + (end.y - start.y).pow(2)) as f64).sqrt()
}
impl Vector {
pub fn to_vec(self: Vector) -> Vec<coords::Coord> {
vec![self.start, self.end]
}
pub fn to_tuple(self: Vector) -> (coords::Coord, coords::Coord, f64) {
(self.start, self.end, self.magnitude)
}
pub fn get_magnitude(self: Vector) -> f64 {
(((self.end.x - self.start.x).pow(2) + (self.end.y - self.start.y).pow(2)) as f64).sqrt()
}
pub fn get_midpoint(self: Vector) -> coords::Coord {
coords::Coord {
x: (self.start.x + self.end.x) / 2,
y: (self.start.y + self.end.y) / 2,
}
}
}
impl ops::Add<Vector> for Vector {
type Output = Vector;
fn add(self: Vector, rhs: Vector) -> Vector {
Vector {
start: self.start + rhs.start,
end: self.end + rhs.end,
magnitude: get_magnitude(self.start, self.end),
}
}
}
impl ops::Sub<Vector> for Vector {
type Output = Vector;
fn sub(self: Vector, rhs: Vector) -> Vector {
Vector {
start: self.start - rhs.start,
end: self.end - rhs.end,
magnitude: get_magnitude(self.start, self.end),
}
}
}
impl ops::Mul<Vector> for Vector {
type Output = Vector;
fn mul(self: Vector, rhs: Vector) -> Vector {
Vector {
start: self.start * rhs.start,
end: self.end * rhs.end,
magnitude: get_magnitude(self.start, self.end),
}
}
}
impl ops::Div<Vector> for Vector {
type Output = Vector;
fn div(self: Vector, rhs: Vector) -> Vector {
Vector {
start: self.start / rhs.start,
end: self.end / rhs.end,
magnitude: get_magnitude(self.start, self.end),
}
}
}