#![allow(dead_code)]
use std::ops;
#[derive(Clone, Copy, Debug)]
pub struct Coord {
pub x: f64,
pub y: f64,
}
#[macro_export]
macro_rules! new_coord {
() => {
$crate::coord::Coord { x: 0.0, y: 0.0 }
};
($y:expr) => {
$crate::coord::Coord {
x: 0.0,
y: $y as f64,
}
};
($x:expr, $y:expr) => {
$crate::coord::Coord {
x: $x as f64,
y: $y as f64,
}
};
}
impl Coord {
pub fn to_vec(self: Coord) -> Vec<f64> {
vec![self.x, self.y]
}
pub fn to_tuple(self: Coord) -> (f64, f64) {
(self.x, self.y)
}
}
impl ops::Add<Coord> for Coord {
type Output = Coord;
fn add(self: Coord, rhs: Coord) -> Coord {
Coord {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl ops::Sub<Coord> for Coord {
type Output = Coord;
fn sub(self: Coord, rhs: Coord) -> Coord {
Coord {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl ops::Mul<Coord> for Coord {
type Output = Coord;
fn mul(self: Coord, rhs: Coord) -> Coord {
Coord {
x: self.x * rhs.x,
y: self.y * rhs.y,
}
}
}
impl ops::Div<Coord> for Coord {
type Output = Self;
fn div(self: Self, rhs: Self) -> Self {
if rhs.x == 0.0 || rhs.y == 0.0 {
panic!("[Coord]: division by 0");
}
Coord {
x: self.x / rhs.x,
y: self.y / rhs.y,
}
}
}
impl PartialEq for Coord {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
}