use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Point {
pub x: f64,
pub y: f64,
}
crate::archive_struct!(Point { x, y });
impl Point {
pub const ORIGIN: Self = Self::new(0.0, 0.0);
pub const fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
pub fn is_finite(self) -> bool {
self.x.is_finite() && self.y.is_finite()
}
pub fn length(self) -> f64 {
self.x.hypot(self.y)
}
pub fn distance(self, other: Self) -> f64 {
(other - self).length()
}
pub fn dot(self, other: Self) -> f64 {
self.x * other.x + self.y * other.y
}
pub fn cross(self, other: Self) -> f64 {
self.x * other.y - self.y * other.x
}
#[must_use]
pub fn lerp(self, other: Self, t: f64) -> Self {
self + (other - self) * t
}
#[must_use]
pub fn midpoint(self, other: Self) -> Self {
Self::new(
f64::midpoint(self.x, other.x),
f64::midpoint(self.y, other.y),
)
}
#[must_use]
pub fn min(self, other: Self) -> Self {
Self::new(self.x.min(other.x), self.y.min(other.y))
}
#[must_use]
pub fn max(self, other: Self) -> Self {
Self::new(self.x.max(other.x), self.y.max(other.y))
}
}
impl From<(f64, f64)> for Point {
fn from((x, y): (f64, f64)) -> Self {
Self::new(x, y)
}
}
impl From<[f64; 2]> for Point {
fn from([x, y]: [f64; 2]) -> Self {
Self::new(x, y)
}
}
impl From<Point> for (f64, f64) {
fn from(p: Point) -> Self {
(p.x, p.y)
}
}
impl Add for Point {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self::new(self.x + rhs.x, self.y + rhs.y)
}
}
impl AddAssign for Point {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl Sub for Point {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self::new(self.x - rhs.x, self.y - rhs.y)
}
}
impl SubAssign for Point {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl Mul<f64> for Point {
type Output = Self;
fn mul(self, rhs: f64) -> Self {
Self::new(self.x * rhs, self.y * rhs)
}
}
impl Div<f64> for Point {
type Output = Self;
fn div(self, rhs: f64) -> Self {
Self::new(self.x / rhs, self.y / rhs)
}
}
impl Neg for Point {
type Output = Self;
fn neg(self) -> Self {
Self::new(-self.x, -self.y)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn arithmetic() {
let a = Point::new(1.0, 2.0);
let b = Point::new(4.0, 6.0);
assert_eq!(a + b, Point::new(5.0, 8.0));
assert_eq!(b - a, Point::new(3.0, 4.0));
assert_eq!(a * 2.0, Point::new(2.0, 4.0));
assert_eq!(-a, Point::new(-1.0, -2.0));
assert_eq!(a.distance(b), 5.0);
assert_eq!(a.lerp(b, 0.5), Point::new(2.5, 4.0));
assert_eq!(a.cross(b), -2.0);
}
#[test]
fn finiteness() {
assert!(Point::new(1.0, 2.0).is_finite());
assert!(!Point::new(f64::NAN, 2.0).is_finite());
assert!(!Point::new(1.0, f64::INFINITY).is_finite());
}
}