Created
April 23, 2020 23:02
-
-
Save sayrer/7bea4c6d843b25ed54916c4571ea2e6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug, Clone, Copy)] | |
struct Coord { | |
x: f64, | |
y: f64, | |
} | |
impl Coord { | |
pub fn as_pair(&self) -> (String, String) { | |
(format!("{:.2}", self.x), format!("{:.2}", self.y)) | |
} | |
fn equal_x(&self, x: f64) -> bool { | |
let self_x = format!("{:.2}", self.x); | |
let other_x = format!("{:.2}", x); | |
self_x == other_x | |
} | |
fn equal_y(&self, y: f64) -> bool { | |
let self_y = format!("{:.2}", self.y); | |
let other_y = format!("{:.2}", y); | |
self_y == other_y | |
} | |
} | |
impl Ord for Coord { | |
fn cmp(&self, other: &Self) -> Ordering { | |
if self.x < other.x || self.equal_x(other.x) && self.y < other.y { | |
Ordering::Less | |
} else { | |
Ordering::Greater | |
} | |
} | |
} | |
impl PartialOrd for Coord { | |
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | |
Some(self.cmp(other)) | |
} | |
} | |
impl PartialEq for Coord { | |
fn eq(&self, other: &Self) -> bool { | |
self.as_pair() == other.as_pair() | |
} | |
} | |
impl Eq for Coord {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment