use crate::geometry::{F32Ext, Transform, Transformation, Vector};
use std::ops::{Add, AddAssign, Sub, SubAssign};
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Point {
pub x: f32,
pub y: f32,
}
impl Point {
pub(crate) fn new(x: f32, y: f32) -> Point {
Point { x, y }
}
pub(crate) fn origin() -> Point {
Point::new(0.0, 0.0)
}
pub(crate) fn lerp(self, other: Point, t: f32) -> Point {
Point::new(F32Ext::lerp(self.x, other.x, t), F32Ext::lerp(self.y, 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);
}
}