maze_generator/prelude/
coordinates.rs1use std::fmt::{Debug, Display, Formatter};
2
3use crate::prelude::*;
4
5#[derive(Debug, Copy, Clone, Default, Ord, PartialOrd, Eq, PartialEq, Hash)]
7pub struct Coordinates {
8 pub x: i32,
10 pub y: i32,
12}
13
14impl Coordinates {
15 pub fn new(x: i32, y: i32) -> Self {
17 Coordinates { x, y }
18 }
19
20 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}