maze_generator/prelude/
coordinates.rs

1use std::fmt::{Debug, Display, Formatter};
2
3use crate::prelude::*;
4
5/// Two-Dimensional coordinates used for addressing fields in a maze.
6#[derive(Debug, Copy, Clone, Default, Ord, PartialOrd, Eq, PartialEq, Hash)]
7pub struct Coordinates {
8    /// X component
9    pub x: i32,
10    /// Y component
11    pub y: i32,
12}
13
14impl Coordinates {
15    /// Create a new instance from the specified coordinate components
16    pub fn new(x: i32, y: i32) -> Self {
17        Coordinates { x, y }
18    }
19
20    /// Returns the next neighboring coordinates in a specific direction
21    pub fn next(&self, direction: &Direction) -> Self {
22        Self {
23            x: self.x
24                + match direction {
25                    Direction::East => 1,
26                    Direction::West => -1,
27                    _ => 0,
28                },
29            y: self.y
30                + match direction {
31                    Direction::North => -1,
32                    Direction::South => 1,
33                    _ => 0,
34                },
35        }
36    }
37}
38
39impl From<Coordinates> for (i32, i32) {
40    fn from(c: Coordinates) -> Self {
41        (c.x, c.y)
42    }
43}
44
45impl From<(i32, i32)> for Coordinates {
46    fn from(source: (i32, i32)) -> Self {
47        Self {
48            x: source.0,
49            y: source.1,
50        }
51    }
52}
53
54impl Display for Coordinates {
55    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
56        f.write_str(&format!("({}, {})", self.x, self.y))
57    }
58}