use crate::geometry::{F64Ext, Transform, Transformation, Vector};
use std::ops::{Add, AddAssign, Sub, SubAssign};
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Point {
pub x: f64,
pub y: f64,
}
impl Point {
pub fn new(x: f64, y: f64) -> Point {
Point { x, y }
}
pub fn origin() -> Point {
Point::new(0.0, 0.0)
}
pub fn to_vector(self) -> Vector {
Vector::new(self.x, self.y)
}
pub fn lerp(self, other: Point, t: f64) -> Point {
Point::new(self.x.ext_lerp(other.x, t), self.y.ext_lerp(other.y, t))
}
}
impl AddAssign<Vector> for Point {
fn add_assign(&mut self, vector: Vector) {
*self = *self + vector;
}
}
impl SubAssign<Vector> for Point {
fn sub_assign(&mut self, vector: Vector) {
*self = *self - vector;
}
}
impl Add<Vector> for Point {
type Output = Point;
fn add(self, v: Vector) -> Point {
Point::new(self.x + v.x, self.y + v.y)
}
}
impl Sub for Point {
type Output = Vector;
fn sub(self, other: Point) -> Vector {
Vector::new(self.x - other.x, self.y - other.y)
}
}
impl Sub<Vector> for Point {
type Output = Point;
fn sub(self, v: Vector) -> Point {
Point::new(self.x - v.x, self.y - v.y)
}
}
impl Transform for Point {
fn transform<T>(self, t: &T) -> Point
where
T: Transformation,
{
t.transform_point(self)
}
fn transform_mut<T>(&mut self, t: &T)
where
T: Transformation,
{
*self = self.transform(t);
}
}