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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
pub enum CellDir {
TR,
BR,
B,
BL,
TL,
T,
C,
}
impl CellDir {
pub fn from(edge: u8) -> Self {
match edge {
0 => CellDir::TR,
1 => CellDir::BR,
2 => CellDir::B,
3 => CellDir::BL,
4 => CellDir::TL,
5 => CellDir::T,
_ => CellDir::C,
}
}
pub fn flip(&self) -> Self {
match self {
CellDir::TR => CellDir::BL,
CellDir::BR => CellDir::TL,
CellDir::B => CellDir::T,
CellDir::BL => CellDir::TR,
CellDir::TL => CellDir::BR,
CellDir::T => CellDir::B,
CellDir::C => CellDir::T,
}
}
#[inline]
pub fn is_output(&self) -> bool {
let e = self.as_edge();
e <= 2
}
#[inline]
pub fn is_input(&self) -> bool {
!self.is_output()
}
#[inline]
pub fn as_edge(&self) -> u8 {
*self as u8
}
pub fn as_menu_pos(&self) -> (i32, i32) {
match self {
CellDir::TR => (0, 1),
CellDir::BR => (1, 1),
CellDir::B => (0, 1),
CellDir::BL => (-1, 1),
CellDir::TL => (-1, 0),
CellDir::T => (0, -1),
_ => (0, 0),
}
}
pub fn path_from_to(mut a: (usize, usize), b: (usize, usize)) -> Vec<CellDir> {
let mut path = vec![];
let mut defensive_max: i32 = 1024;
while (a.0 != b.0 || a.1 != b.1) && defensive_max > 0 {
defensive_max -= 1;
let mut min_distance = 99999.0;
let mut min_dir = CellDir::C;
let mut min_new_a = a;
for e in 0..6 {
let dir = Self::from(e);
if let Some(new_pos) = dir.offs_pos(a) {
let dist = (b.0 as f32 - new_pos.0 as f32).powf(2.0)
+ (b.1 as f32 - new_pos.1 as f32).powf(2.0);
if dist < min_distance {
min_distance = dist;
min_dir = dir;
min_new_a = new_pos;
}
} else {
}
}
if min_distance < 99999.0 {
a = min_new_a;
path.push(min_dir);
} else {
break;
}
}
path
}
pub fn offs_pos(&self, pos: (usize, usize)) -> Option<(usize, usize)> {
let offs = self.as_offs(pos.0);
let new_pos = (pos.0 as i32 + offs.0, pos.1 as i32 + offs.1);
if new_pos.0 >= 0 && new_pos.1 >= 0 {
Some((new_pos.0 as usize, new_pos.1 as usize))
} else {
None
}
}
pub fn as_offs(&self, x: usize) -> (i32, i32) {
let even = x % 2 == 0;
match self {
CellDir::TR => (1, if even { -1 } else { 0 }),
CellDir::BR => (1, i32::from(!even)),
CellDir::B => (0, 1),
CellDir::BL => (-1, i32::from(!even)),
CellDir::TL => (-1, if even { -1 } else { 0 }),
CellDir::T => (0, -1),
_ => (0, 0),
}
}
pub fn are_adjacent(pos_a: (usize, usize), pos_b: (usize, usize)) -> Option<CellDir> {
let ipos_a = (pos_a.0 as i32, pos_a.1 as i32);
let ipos_b = (pos_b.0 as i32, pos_b.1 as i32);
let (ox, oy) = CellDir::T.as_offs(pos_a.0);
if ipos_b == (ipos_a.0 + ox, ipos_a.1 + oy) {
return Some(CellDir::T);
}
let (ox, oy) = CellDir::TL.as_offs(pos_a.0);
if ipos_b == (ipos_a.0 + ox, ipos_a.1 + oy) {
return Some(CellDir::TL);
}
let (ox, oy) = CellDir::BL.as_offs(pos_a.0);
if ipos_b == (ipos_a.0 + ox, ipos_a.1 + oy) {
return Some(CellDir::BL);
}
let (ox, oy) = CellDir::TR.as_offs(pos_a.0);
if ipos_b == (ipos_a.0 + ox, ipos_a.1 + oy) {
return Some(CellDir::TR);
}
let (ox, oy) = CellDir::BR.as_offs(pos_a.0);
if ipos_b == (ipos_a.0 + ox, ipos_a.1 + oy) {
return Some(CellDir::BR);
}
let (ox, oy) = CellDir::B.as_offs(pos_a.0);
if ipos_b == (ipos_a.0 + ox, ipos_a.1 + oy) {
return Some(CellDir::B);
}
None
}
}