1use std::fmt;
4
5#[repr(C)]
19#[derive(Debug, Clone, Copy, PartialEq)]
20pub struct CGPoint {
21 pub x: f64,
22 pub y: f64,
23}
24
25impl std::hash::Hash for CGPoint {
26 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
27 self.x.to_bits().hash(state);
28 self.y.to_bits().hash(state);
29 }
30}
31
32impl Eq for CGPoint {}
33
34impl CGPoint {
35 #[must_use]
46 pub const fn new(x: f64, y: f64) -> Self {
47 Self { x, y }
48 }
49
50 #[must_use]
61 pub const fn zero() -> Self {
62 Self::new(0.0, 0.0)
63 }
64
65 #[must_use]
67 pub const fn is_zero(&self) -> bool {
68 self.x == 0.0 && self.y == 0.0
69 }
70
71 #[must_use]
73 pub fn distance_to(&self, other: &Self) -> f64 {
74 let dx = self.x - other.x;
75 let dy = self.y - other.y;
76 dx.hypot(dy)
77 }
78
79 #[must_use]
81 pub const fn distance_squared_to(&self, other: &Self) -> f64 {
82 let dx = self.x - other.x;
83 let dy = self.y - other.y;
84 dx * dx + dy * dy
85 }
86}
87
88impl Default for CGPoint {
89 fn default() -> Self {
90 Self::zero()
91 }
92}
93
94impl fmt::Display for CGPoint {
95 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96 write!(f, "({}, {})", self.x, self.y)
97 }
98}