pax_runtime_api/math/
point.rs1use 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
12pub struct Point2<W = Generic> {
13 pub x: f64,
14 pub y: f64,
15 _panthom: PhantomData<W>,
16}
17
18impl<W: Space> std::fmt::Debug for Point2<W> {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 write!(f, "({} {})", self.x, self.y)
24 }
25}
26
27impl<W: Space> Clone for Point2<W> {
28 fn clone(&self) -> Self {
29 Self {
30 x: self.x,
31 y: self.y,
32 _panthom: PhantomData,
33 }
34 }
35}
36
37impl<W: Space> Copy for Point2<W> {}
38
39impl<W: Space> PartialEq for Point2<W> {
40 fn eq(&self, other: &Self) -> bool {
41 self.x == other.x && self.y == other.y
42 }
43}
44
45impl<W: Space> Default for Point2<W> {
46 fn default() -> Self {
47 Self::new(0.0, 0.0)
48 }
49}
50
51impl<W: Space> Point2<W> {
52 pub fn new(x: f64, y: f64) -> Self {
53 Point2 {
54 x,
55 y,
56 _panthom: PhantomData,
57 }
58 }
59
60 pub fn to_vector(self) -> Vector2<W> {
61 Vector2::new(self.x, self.y)
62 }
63
64 pub fn cast_space<WNew: Space>(self) -> Point2<WNew> {
65 Point2::new(self.x, self.y)
66 }
67
68 pub fn midpoint_towards(self, other: Self) -> Self {
69 self.lerp_towards(other, 1.0 / 2.0)
70 }
71
72 pub fn lerp_towards(self, other: Self, l: f64) -> Self {
73 let v = other - self;
74 self + l * v
75 }
76}
77
78impl<W: Space> Sub for Point2<W> {
79 type Output = Vector2<W>;
80 fn sub(self, rhs: Point2<W>) -> Self::Output {
81 Self::Output::new(self.x - rhs.x, self.y - rhs.y)
82 }
83}
84
85impl<W: Space> Add<Vector2<W>> for Point2<W> {
86 type Output = Point2<W>;
87 fn add(self, rhs: Vector2<W>) -> Self::Output {
88 Self::Output::new(self.x + rhs.x, self.y + rhs.y)
89 }
90}
91impl<W: Space> Add<Point2<W>> for Vector2<W> {
92 type Output = Point2<W>;
93 fn add(self, rhs: Point2<W>) -> Self::Output {
94 Self::Output::new(self.x + rhs.x, self.y + rhs.y)
95 }
96}
97
98impl<W: Space> Sub<Vector2<W>> for Point2<W> {
99 type Output = Point2<W>;
100 fn sub(self, rhs: Vector2<W>) -> Self::Output {
101 Self::Output::new(self.x - rhs.x, self.y - rhs.y)
102 }
103}