1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use glam::IVec2;
use crate::point::GridPoint;
pub const UP: IVec2 = IVec2::from_array([0, 1]);
pub const DOWN: IVec2 = IVec2::from_array([0, -1]);
pub const LEFT: IVec2 = IVec2::from_array([-1, 0]);
pub const RIGHT: IVec2 = IVec2::from_array([1, 0]);
pub const UP_LEFT: IVec2 = IVec2::from_array([-1, 1]);
pub const UP_RIGHT: IVec2 = IVec2::from_array([1, 1]);
pub const DOWN_LEFT: IVec2 = IVec2::from_array([-1, -1]);
pub const DOWN_RIGHT: IVec2 = IVec2::from_array([1, -1]);
pub const DIR_4: &[IVec2] = &[UP, DOWN, LEFT, RIGHT];
pub const DIR_8: &[IVec2] = &[
UP, DOWN, LEFT, RIGHT, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT,
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Dir4 {
Up,
Down,
Left,
Right,
}
impl From<Dir4> for IVec2 {
fn from(d: Dir4) -> Self {
match d {
Dir4::Up => UP,
Dir4::Down => DOWN,
Dir4::Left => LEFT,
Dir4::Right => RIGHT,
}
}
}
impl Dir4 {
pub fn from_point(p: impl GridPoint) -> Option<Dir4> {
match p.as_ivec2().signum().to_array() {
[0, 1] => Some(Dir4::Up),
[0, -1] => Some(Dir4::Down),
[-1, 0] => Some(Dir4::Left),
[1, 0] => Some(Dir4::Right),
_ => None,
}
}
pub fn from_index(i: usize) -> Option<Dir4> {
match i {
0 => Some(Dir4::Up),
1 => Some(Dir4::Down),
2 => Some(Dir4::Left),
3 => Some(Dir4::Right),
_ => None,
}
}
pub fn to_index(&self) -> usize {
match self {
Dir4::Up => 0,
Dir4::Down => 1,
Dir4::Left => 2,
Dir4::Right => 3,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Dir8 {
Up,
Down,
Left,
Right,
UpLeft,
UpRight,
DownLeft,
DownRight,
}
impl Dir8 {
pub fn from_point(p: impl GridPoint) -> Option<Dir8> {
match p.as_ivec2().signum().to_array() {
[0, 1] => Some(Dir8::Up),
[0, -1] => Some(Dir8::Down),
[-1, 0] => Some(Dir8::Left),
[1, 0] => Some(Dir8::Right),
[-1, 1] => Some(Dir8::UpLeft),
[1, 1] => Some(Dir8::UpRight),
[-1, -1] => Some(Dir8::DownLeft),
[1, -1] => Some(Dir8::DownRight),
_ => None,
}
}
pub fn from_index(i: usize) -> Option<Dir8> {
match i {
0 => Some(Dir8::Up),
1 => Some(Dir8::Down),
2 => Some(Dir8::Left),
3 => Some(Dir8::Right),
4 => Some(Dir8::UpLeft),
5 => Some(Dir8::UpRight),
6 => Some(Dir8::DownLeft),
7 => Some(Dir8::DownRight),
_ => None,
}
}
pub fn to_index(&self) -> usize {
match self {
Dir8::Up => 0,
Dir8::Down => 1,
Dir8::Left => 2,
Dir8::Right => 3,
Dir8::UpLeft => 4,
Dir8::UpRight => 5,
Dir8::DownLeft => 6,
Dir8::DownRight => 7,
}
}
}
impl From<Dir8> for IVec2 {
fn from(d: Dir8) -> Self {
match d {
Dir8::Up => UP,
Dir8::Down => DOWN,
Dir8::Left => LEFT,
Dir8::Right => RIGHT,
Dir8::UpLeft => UP_LEFT,
Dir8::UpRight => UP_RIGHT,
Dir8::DownLeft => DOWN_LEFT,
Dir8::DownRight => DOWN_RIGHT,
}
}
}