1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use std::ops::{Add, Mul, Sub};

use crate::traits::{IntoComponents, Roots};
use crate::utils::vec_ord;
use crate::{Angle, Fraction, Zero};

/// A coordinate in a 2d space.
#[derive(Default, Clone, Copy, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Point<Unit> {
    /// The x-axis component.
    pub x: Unit,
    /// The y-axis component
    pub y: Unit,
}

impl<Unit> Point<Unit> {
    /// Returns a new point with the provided `x` and `y` components.
    pub const fn new(x: Unit, y: Unit) -> Self {
        Self { x, y }
    }

    /// Returns a new point with both `x` and `y` initialized with `i`.
    pub fn squared(i: Unit) -> Self
    where
        Unit: Clone,
    {
        Self::new(i.clone(), i)
    }

    /// Converts the contents of this point to `NewUnit` using [`From`].
    pub fn cast<NewUnit>(self) -> Point<NewUnit>
    where
        Unit: Into<NewUnit>,
    {
        Point {
            x: self.x.into(),
            y: self.y.into(),
        }
    }

    /// Converts the contents of this point to `NewUnit` using [`TryFrom`].
    ///
    /// # Errors
    ///
    /// Returns `<NewUnit as TryFrom>::Error` when the inner type cannot be
    /// converted. For this crate's types, this genenerally will be
    /// [`TryFromIntError`](std::num::TryFromIntError).
    pub fn try_cast<NewUnit>(self) -> Result<Point<NewUnit>, Unit::Error>
    where
        Unit: TryInto<NewUnit>,
    {
        Ok(Point {
            x: self.x.try_into()?,
            y: self.y.try_into()?,
        })
    }

    /// Maps each component to `map` and returns a new value with the mapped
    /// components.
    pub fn map<NewUnit>(self, mut map: impl FnMut(Unit) -> NewUnit) -> Point<NewUnit> {
        Point {
            x: map(self.x),
            y: map(self.y),
        }
    }

    /// Returns the dot product of `self` and `other`.
    #[must_use]
    pub fn dot(self, other: Point<Unit>) -> Unit
    where
        Unit: Mul<Output = Unit> + Add<Output = Unit>,
    {
        self.x * other.x + self.y * other.y
    }

    /// Returns the magnitude of self, which is the absolute distance from 0,0.
    #[must_use]
    pub fn magnitude(self) -> Unit
    where
        Unit: Mul<Output = Unit> + Add<Output = Unit> + Roots + Copy,
    {
        (self.x * self.x + self.y * self.y).sqrt()
    }

    /// Returns `self` rotated around `origin` by `angle`.
    #[must_use]
    pub fn rotate_around(self, origin: Point<Unit>, angle: Angle) -> Point<Unit>
    where
        Unit: Copy + Add<Output = Unit> + Sub<Output = Unit> + Mul<Fraction, Output = Unit>,
    {
        let cos = angle.cos();
        let sin = angle.sin();
        let d = self - origin;
        origin + Point::new(d.x * cos - d.y * sin, d.y * cos + d.x * sin)
    }

    /// Returns `self` rotated around `Point::ZERO` by `angle`.
    #[must_use]
    pub fn rotate_by(self, angle: Angle) -> Point<Unit>
    where
        Unit: Zero + Copy + Add<Output = Unit> + Sub<Output = Unit> + Mul<Fraction, Output = Unit>,
    {
        self.rotate_around(Self::ZERO, angle)
    }
}

impl<Unit> Ord for Point<Unit>
where
    Unit: Ord + Copy + Mul<Output = Unit>,
{
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        vec_ord::<Unit>((*self).into_components(), (*other).into_components())
    }

    fn max(self, other: Self) -> Self
    where
        Self: Sized,
    {
        Self {
            x: self.x.max(other.x),
            y: self.y.max(other.y),
        }
    }

    fn min(self, other: Self) -> Self
    where
        Self: Sized,
    {
        Self {
            x: self.x.min(other.x),
            y: self.y.min(other.y),
        }
    }

    fn clamp(self, min: Self, max: Self) -> Self
    where
        Self: Sized,
    {
        Self {
            x: self.x.clamp(min.x, max.x),
            y: self.y.clamp(min.y, max.y),
        }
    }
}

impl<Unit> PartialOrd for Point<Unit>
where
    Unit: Ord + Copy + Mul<Output = Unit>,
{
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

#[cfg(feature = "euclid")]
impl<Unit> From<euclid::Point2D<f32, euclid::UnknownUnit>> for Point<Unit>
where
    Unit: crate::traits::FloatConversion<Float = f32>,
{
    fn from(point: euclid::Point2D<f32, euclid::UnknownUnit>) -> Self {
        Self {
            x: Unit::from_float(point.x),
            y: Unit::from_float(point.y),
        }
    }
}
#[cfg(feature = "euclid")]
impl<Unit> From<Point<Unit>> for euclid::Point2D<f32, euclid::UnknownUnit>
where
    Unit: crate::traits::FloatConversion<Float = f32>,
{
    fn from(point: Point<Unit>) -> Self {
        Self::new(point.x.into_float(), point.y.into_float())
    }
}

#[cfg(feature = "winit")]
impl<Unit> From<winit::dpi::PhysicalPosition<f64>> for Point<Unit>
where
    Unit: crate::traits::FloatConversion<Float = f32>,
{
    fn from(point: winit::dpi::PhysicalPosition<f64>) -> Self {
        Self {
            x: Unit::from_float(intentional::CastFrom::from_cast(point.x)),
            y: Unit::from_float(intentional::CastFrom::from_cast(point.y)),
        }
    }
}

#[cfg(feature = "winit")]
impl From<winit::dpi::PhysicalPosition<i32>> for Point<crate::units::Px> {
    fn from(point: winit::dpi::PhysicalPosition<i32>) -> Self {
        Self {
            x: crate::units::Px::new(point.x),
            y: crate::units::Px::new(point.y),
        }
    }
}

#[cfg(feature = "winit")]
impl From<winit::dpi::PhysicalPosition<u32>> for Point<crate::units::UPx> {
    fn from(point: winit::dpi::PhysicalPosition<u32>) -> Self {
        Self {
            x: crate::units::UPx::new(point.x),
            y: crate::units::UPx::new(point.y),
        }
    }
}

#[cfg(feature = "winit")]
impl From<Point<crate::units::Px>> for winit::dpi::PhysicalPosition<i32> {
    fn from(point: Point<crate::units::Px>) -> Self {
        Self {
            x: point.x.into(),
            y: point.y.into(),
        }
    }
}

#[cfg(feature = "winit")]
impl From<Point<crate::units::UPx>> for winit::dpi::PhysicalPosition<u32> {
    fn from(point: Point<crate::units::UPx>) -> Self {
        Self {
            x: point.x.into(),
            y: point.y.into(),
        }
    }
}

impl_2d_math!(Point, x, y);

#[cfg(feature = "wgpu")]
impl From<Point<crate::units::UPx>> for wgpu::Origin3d {
    fn from(value: Point<crate::units::UPx>) -> Self {
        Self {
            x: value.x.into(),
            y: value.y.into(),
            z: 0,
        }
    }
}