use std::fmt;
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CGPoint {
pub x: f64,
pub y: f64,
}
impl std::hash::Hash for CGPoint {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.x.to_bits().hash(state);
self.y.to_bits().hash(state);
}
}
impl Eq for CGPoint {}
impl CGPoint {
pub const fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
pub const fn zero() -> Self {
Self::new(0.0, 0.0)
}
pub const fn is_zero(&self) -> bool {
self.x == 0.0 && self.y == 0.0
}
pub fn distance_to(&self, other: &Self) -> f64 {
let dx = self.x - other.x;
let dy = self.y - other.y;
dx.hypot(dy)
}
pub const fn distance_squared_to(&self, other: &Self) -> f64 {
let dx = self.x - other.x;
let dy = self.y - other.y;
dx * dx + dy * dy
}
}
impl Default for CGPoint {
fn default() -> Self {
Self::zero()
}
}
impl fmt::Display for CGPoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}