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
192
193
194
195
196
197
198
199
use roads2csv::{InputStreetValue, StreetName, GridPosition};
#[derive(Debug, Clone)]
pub struct Grid {
pub bbox: Bbox,
pub config: GridConfig,
fonts: Vec<InputStreetValue>,
}
#[derive(Debug, Copy, Clone)]
pub struct Millimeter(pub f32);
#[derive(Debug, Copy, Clone)]
pub struct Bbox {
pub width: Millimeter,
pub height: Millimeter,
}
#[derive(Debug, Copy, Clone)]
pub struct GridConfig {
pub cell_height: Millimeter,
pub cell_width: Millimeter,
}
#[derive(Debug, Clone)]
pub struct StreetNameRect {
pub street_name: String,
pub x_from_left: Millimeter,
pub y_from_top: Millimeter,
pub width: Millimeter,
pub height: Millimeter,
}
impl Grid {
pub fn new(bbox: Bbox, config: GridConfig) -> Self {
Self {
bbox,
config,
fonts: Vec::new(),
}
}
pub fn insert_street(&mut self, rect: StreetNameRect) {
let min_position_x = (rect.x_from_left.0 / self.config.cell_width.0).floor() as usize;
let max_position_x = ((rect.x_from_left.0 + rect.width.0) / self.config.cell_width.0).floor() as usize;
let mut min_position_y = (rect.y_from_top.0 / self.config.cell_height.0).floor() as usize;
let mut max_position_y = ((rect.y_from_top.0 + rect.height.0) / self.config.cell_height.0).floor() as usize;
min_position_y += 1;
max_position_y += 1;
let positions_to_add = match (min_position_x == max_position_x, min_position_y == max_position_y) {
(true, true) => {
vec![
(number_to_alphabet_value(min_position_x), min_position_y),
]
},
(true, false) => {
vec![
(number_to_alphabet_value(min_position_x), min_position_y),
(number_to_alphabet_value(min_position_x), max_position_y),
]
},
(false, true) => {
vec![
(number_to_alphabet_value(min_position_x), min_position_y),
(number_to_alphabet_value(max_position_x), min_position_y),
]
},
(false, false) => {
vec![
(number_to_alphabet_value(min_position_x), min_position_y),
(number_to_alphabet_value(min_position_x), max_position_y),
(number_to_alphabet_value(max_position_x), min_position_y),
(number_to_alphabet_value(max_position_x), max_position_y),
]
}
};
for (column, row) in positions_to_add {
self.fonts.push(InputStreetValue {
street_name: StreetName(rect.street_name.clone()),
position: GridPosition {
column,
row,
}
});
}
}
pub fn street_names(&self) -> Vec<InputStreetValue> {
self.fonts.clone()
}
}
pub fn number_to_alphabet_value(num: usize) -> String {
const ALPHABET_LEN: usize = 26;
let mut result = Vec::<char>::new();
let mut multiple_of_alphabet = num / ALPHABET_LEN;
while multiple_of_alphabet != 0 {
let remainder = (multiple_of_alphabet - 1) % ALPHABET_LEN;
result.push(u8_to_char(remainder as u8));
multiple_of_alphabet = (multiple_of_alphabet - 1) / ALPHABET_LEN;
}
let mut result = result.into_iter().rev().collect::<String>();
result.push(u8_to_char((num % ALPHABET_LEN) as u8));
result
}
fn u8_to_char(input: u8) -> char {
('A' as u8 + input) as char
}
#[test]
fn u8_to_char_test() {
assert_eq!(u8_to_char(0), 'A');
assert_eq!(u8_to_char(6), 'G');
assert_eq!(u8_to_char(25), 'Z');
}
#[test]
fn test_number_to_alphabet_value() {
assert_eq!(number_to_alphabet_value(0), String::from("A"));
assert_eq!(number_to_alphabet_value(1), String::from("B"));
assert_eq!(number_to_alphabet_value(6), String::from("G"));
assert_eq!(number_to_alphabet_value(26), String::from("AA"));
assert_eq!(number_to_alphabet_value(27), String::from("AB"));
assert_eq!(number_to_alphabet_value(225), String::from("HR"));
}