Skip to main content

pax_runtime_api/math/
point.rs

1use std::{
2    marker::PhantomData,
3    ops::{Add, Sub},
4};
5
6use crate::Interpolatable;
7
8use super::{vector::Vector2, Generic, Space};
9
10impl<W: Space> Interpolatable for Point2<W> {}
11
12/// A representation of a point in 2D space (float).
13pub struct Point2<W = Generic> {
14    /// Horizontal coordinate.
15    pub x: f64,
16    /// Vertical coordinate.
17    pub y: f64,
18    _phantom: PhantomData<W>,
19}
20
21// Implement Clone, Copy, PartialEq, etc manually, as
22// to not require the Space to implement these.
23
24impl<W: Space> std::fmt::Debug for Point2<W> {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        write!(f, "({} {})", self.x, self.y)
27    }
28}
29
30impl<W: Space> Clone for Point2<W> {
31    fn clone(&self) -> Self {
32        Self {
33            x: self.x,
34            y: self.y,
35            _phantom: PhantomData,
36        }
37    }
38}
39
40impl<W: Space> Copy for Point2<W> {}
41
42impl<W: Space> PartialEq for Point2<W> {
43    fn eq(&self, other: &Self) -> bool {
44        self.x == other.x && self.y == other.y
45    }
46}
47
48impl<W: Space> Default for Point2<W> {
49    fn default() -> Self {
50        Self::new(0.0, 0.0)
51    }
52}
53
54impl<W: Space> Point2<W> {
55    /// Constructs a point from x and y coordinates.
56    pub fn new(x: f64, y: f64) -> Self {
57        Point2 {
58            x,
59            y,
60            _phantom: PhantomData,
61        }
62    }
63
64    /// Reinterprets this point as a vector from the origin.
65    pub fn to_vector(self) -> Vector2<W> {
66        Vector2::new(self.x, self.y)
67    }
68
69    /// Casts this point into another phantom coordinate space.
70    pub fn cast_space<WNew: Space>(self) -> Point2<WNew> {
71        Point2::new(self.x, self.y)
72    }
73
74    /// Returns the midpoint between this point and `other`.
75    pub fn midpoint_towards(self, other: Self) -> Self {
76        self.lerp_towards(other, 1.0 / 2.0)
77    }
78
79    /// Linearly interpolates toward `other` by `l`.
80    pub fn lerp_towards(self, other: Self, l: f64) -> Self {
81        let v = other - self;
82        self + l * v
83    }
84}
85
86impl<W: Space> Sub for Point2<W> {
87    type Output = Vector2<W>;
88    fn sub(self, rhs: Point2<W>) -> Self::Output {
89        Self::Output::new(self.x - rhs.x, self.y - rhs.y)
90    }
91}
92
93impl<W: Space> Add<Vector2<W>> for Point2<W> {
94    type Output = Point2<W>;
95    fn add(self, rhs: Vector2<W>) -> Self::Output {
96        Self::Output::new(self.x + rhs.x, self.y + rhs.y)
97    }
98}
99impl<W: Space> Add<Point2<W>> for Vector2<W> {
100    type Output = Point2<W>;
101    fn add(self, rhs: Point2<W>) -> Self::Output {
102        Self::Output::new(self.x + rhs.x, self.y + rhs.y)
103    }
104}
105
106impl<W: Space> Sub<Vector2<W>> for Point2<W> {
107    type Output = Point2<W>;
108    fn sub(self, rhs: Vector2<W>) -> Self::Output {
109        Self::Output::new(self.x - rhs.x, self.y - rhs.y)
110    }
111}