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
use crate::board::RectangularBoard;
use rand::Rng;
use simplesvg::{Attr, Color, Fig, Svg};
use std::collections::{HashMap, HashSet};
pub fn render_single_tiling_from_vec(boards: Vec<&RectangularBoard>) -> String {
let mut tile_hashmap = HashMap::new();
for i in (1..boards.len()).rev() {
tile_hashmap.insert(boards[i].clone(), vec![boards[i - 1].clone()]);
}
render_single_tiling(boards.last().unwrap(), &tile_hashmap)
}
pub fn render_single_tiling<S: ::std::hash::BuildHasher>(
board: &RectangularBoard,
tile_hashmap: &HashMap<RectangularBoard, Vec<RectangularBoard>, S>,
) -> String {
let gap_size = 0.0;
let box_size = 50.0;
let padding = 10.0;
let colors = vec![
Color(30, 56, 136),
Color(71, 115, 170),
Color(245, 230, 99),
Color(255, 173, 105),
Color(156, 56, 72),
Color(124, 178, 135),
Color(251, 219, 136),
];
let mut boxes = Vec::new();
let mut color_index = rand::thread_rng().gen_range(0, colors.len());
let mut current = board;
while tile_hashmap.contains_key(current) {
let next = rand::thread_rng().gen_range(0, tile_hashmap[current].len());
let next_board = tile_hashmap.get(current).unwrap().get(next).unwrap();
let mut tiled_positions = HashSet::new();
for y in 0..next_board.height {
for x in 0..next_board.width {
if next_board.board[y][x] ^ current.board[y][x] {
tiled_positions.insert((x, y));
}
}
}
for (x, y) in tiled_positions.iter() {
let rect = Fig::Rect(
(*x as f32) * (box_size + gap_size) + padding,
(*y as f32) * (box_size + gap_size) + padding,
box_size,
box_size,
)
.styled(Attr::default().fill(colors[color_index]));
boxes.push(rect);
enum Border {
Left,
Right,
Top,
Bottom,
};
let border = |x: usize, y: usize, b: Border, gray: bool| {
let xs = match b {
Border::Right => (x as f32 + 1.0) * (box_size + gap_size) + padding - gap_size,
_ => (x as f32) * (box_size + gap_size) + padding,
};
let ys = match b {
Border::Top => (y as f32 + 1.0) * (box_size + gap_size) + padding - gap_size,
_ => (y as f32) * (box_size + gap_size) + padding,
};
let xe = match b {
Border::Left => (x as f32) * (box_size + gap_size) + padding,
_ => (x as f32 + 1.0) * (box_size + gap_size) + padding - gap_size,
};
let ye = match b {
Border::Bottom => (y as f32) * (box_size + gap_size) + padding,
_ => (y as f32 + 1.0) * (box_size + gap_size) + padding - gap_size,
};
let mut b = Fig::Line(xs, ys, xe, ye);
b = b.styled(
Attr::default()
.stroke(if gray {
Color(211, 211, 211)
} else {
Color(0, 0, 0)
})
.stroke_width(0.5),
);
b
};
boxes.push(border(
*x,
*y,
Border::Left,
tiled_positions.contains(&(*x - 1, *y)),
));
boxes.push(border(
*x,
*y,
Border::Right,
tiled_positions.contains(&(*x + 1, *y)),
));
boxes.push(border(
*x,
*y,
Border::Top,
tiled_positions.contains(&(*x, *y + 1)),
));
boxes.push(border(
*x,
*y,
Border::Bottom,
tiled_positions.contains(&(*x, *y - 1)),
));
}
color_index = (color_index + 1) % colors.len();
current = next_board;
}
Svg(
vec![Fig::Multiple(boxes)],
(50 * board.width) as u32 + 2 * (padding as u32),
(50 * board.height) as u32 + 2 * (padding as u32),
)
.to_string()
}