#![allow(dead_code)]
use crate::coord;
use std::ops;
#[derive(Clone, Copy, Debug)]
pub struct Vector {
pub start: coord::Coord,
pub end: coord::Coord,
pub magnitude: f64,
}
#[macro_export]
macro_rules! new_vector {
() => {
$crate::vector::Vector {
start: coord::Coord { x: 0.0, y: 0.0 },
end: coord::Coord { x: 0.0, y: 0.0 },
magnitude: 0.0,
}
};
($end:expr) => {
vector::Vector {
start: coord::Coord { x: 0.0, y: 0.0 },
end: $end,
magnitude: vector::get_magnitude(coord::Coord { x: 0.0, y: 0.0 }, $end as coord::Coord),
}
};
($start:expr, $end:expr) => {
vector::Vector {
start: $start,
end: $end,
magnitude: vector::get_magnitude($start as coord::Coord, $end as coord::Coord),
}
};
}
pub fn get_magnitude(start: coord::Coord, end: coord::Coord) -> f64 {
(((end.x - start.x).powi(2) + (end.y - start.y).powi(2)) as f64).sqrt()
}
impl Vector {
pub fn to_vec(self: Vector) -> Vec<coord::Coord> {
vec![self.start, self.end]
}
pub fn to_tuple(self: Vector) -> (coord::Coord, coord::Coord, f64) {
(self.start, self.end, self.magnitude)
}
pub fn get_magnitude(self: Vector) -> f64 {
(((self.end.x - self.start.x).powi(2) + (self.end.y - self.start.y).powi(2)) as f64).sqrt()
}
pub fn get_midpoint(self: Vector) -> coord::Coord {
coord::Coord {
x: (self.start.x + self.end.x) / 2.0,
y: (self.start.y + self.end.y) / 2.0,
}
}
}
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 = Self;
fn div(self: Self, rhs: Self) -> Self {
if rhs.end.x == 0.0 || rhs.end.y == 0.0 {
panic!("[Vector]: division by 0");
}
Vector {
start: self.start / rhs.start,
end: self.end / rhs.end,
magnitude: get_magnitude(self.start, self.end),
}
}
}
impl PartialEq for Vector {
fn eq(&self, other: &Self) -> bool {
self.start == other.start && self.end == other.end
}
}