basic_pathfinding/
coord.rs

1use std::cmp::Ordering;
2
3#[derive(Default, Eq, Clone, Copy, Serialize, Deserialize, Debug)]
4pub struct Coord {
5  pub x: i32,
6  pub y: i32,
7}
8
9impl Coord {
10  pub fn new(x: i32, y: i32) -> Coord {
11    Coord { x: x, y: y }
12  }
13
14  pub fn matches(&self, x: i32, y: i32) -> bool {
15    self.x == x && self.y == y
16  }
17
18  pub fn distance(&self, other: &Self) -> i32 {
19    (self.x - other.x).abs() + (self.y - other.y).abs()
20  }
21
22  pub fn to_flat_repr(coords: Vec<Coord>) -> Vec<i32> {
23    let mut flat_result = vec![];
24    for coord in coords {
25      flat_result.push(coord.x);
26      flat_result.push(coord.y);
27    }
28    flat_result
29  }
30}
31
32impl Ord for Coord {
33  fn cmp(&self, other: &Self) -> Ordering {
34    match self.x.cmp(&other.x) {
35      Ordering::Equal => self.y.cmp(&other.y),
36      ord => ord,
37    }
38  }
39}
40
41impl PartialOrd for Coord {
42  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
43    Some(self.cmp(other))
44  }
45}
46
47impl PartialEq for Coord {
48  fn eq(&self, other: &Self) -> bool {
49    (self.x == other.x) & (self.y == other.y)
50  }
51}