use crate::Vector;
use std::ops::{Add, AddAssign, Sub, SubAssign};
use num_traits::Zero;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Point<T> {
pub x: T,
pub y: T,
}
impl<T> Point<T> {
pub fn new(x: T, y: T) -> Self {
Self { x, y }
}
}
impl<T: Zero> Default for Point<T> {
fn default() -> Self {
Self {
x: T::zero(),
y: T::zero(),
}
}
}
impl<T: Sub<Output = T>> Sub for Point<T> {
type Output = Vector<T>;
fn sub(self, other: Self) -> Vector<T> {
Vector {
x: self.x - other.x,
y: self.y - other.y,
}
}
}
impl<T: Add<Output = T>> Add<Vector<T>> for Point<T> {
type Output = Self;
fn add(self, other: Vector<T>) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
impl<T: Sub<Output = T>> Sub<Vector<T>> for Point<T> {
type Output = Self;
fn sub(self, other: Vector<T>) -> Self {
Self {
x: self.x - other.x,
y: self.y - other.y,
}
}
}
impl<T: AddAssign> AddAssign<Vector<T>> for Point<T> {
fn add_assign(&mut self, other: Vector<T>) {
self.x += other.x;
self.y += other.y;
}
}
impl<T: SubAssign> SubAssign<Vector<T>> for Point<T> {
fn sub_assign(&mut self, other: Vector<T>) {
self.x -= other.x;
self.y -= other.y;
}
}
impl<T> From<[T; 2]> for Point<T> {
fn from([x, y]: [T; 2]) -> Self {
Self { x, y }
}
}
impl<T> From<Point<T>> for [T; 2] {
fn from(Point { x, y }: Point<T>) -> Self {
[x, y]
}
}