Ver código fonte

Close paths correctly

Johannes Hofmann 7 anos atrás
pai
commit
144553b16e
1 arquivos alterados com 31 adições e 5 exclusões
  1. 31
    5
      src/main.rs

+ 31
- 5
src/main.rs Ver arquivo

@@ -130,11 +130,37 @@ fn handle_event(
130 130
                     }
131 131
                     complete_ways.push(way_index);
132 132
                     // all nodes present
133
-                    if !way.is_empty() {
134
-                        map.add_path_element(PathElement::MoveTo((*query_state.way_nodes.get(&way[0]).unwrap()).into()));
135
-                        for node_id in way.iter().skip(1) {
136
-                            map.add_path_element(PathElement::LineTo((*query_state.way_nodes.get(node_id).unwrap()).into()));
137
-                        }
133
+                    match way.len() {
134
+                        0 => {},
135
+                        1 => {
136
+                            map.add_path_element(
137
+                                PathElement::MoveTo((*query_state.way_nodes.get(&way[0]).unwrap()).into())
138
+                            );
139
+                            map.add_path_element(PathElement::ClosePath);
140
+                        },
141
+                        2 => {
142
+                            map.add_path_element(
143
+                                PathElement::MoveTo((*query_state.way_nodes.get(&way[0]).unwrap()).into())
144
+                            );
145
+                            map.add_path_element(
146
+                                PathElement::LineTo((*query_state.way_nodes.get(&way[1]).unwrap()).into())
147
+                            );
148
+                        },
149
+                        len => {
150
+                            map.add_path_element(PathElement::MoveTo((*query_state.way_nodes.get(&way[0]).unwrap()).into()));
151
+
152
+                            if way.first() == way.last() {
153
+                                for node_id in way.iter().skip(1).take(len - 2) {
154
+                                    map.add_path_element(PathElement::LineTo((*query_state.way_nodes.get(node_id).unwrap()).into()));
155
+                                }
156
+                                map.add_path_element(PathElement::ClosePath);
157
+                            } else {
158
+                                for node_id in way.iter().skip(1) {
159
+                                    map.add_path_element(PathElement::LineTo((*query_state.way_nodes.get(node_id).unwrap()).into()));
160
+                                }
161
+                            }
162
+
163
+                        },
138 164
                     }
139 165
                 }
140 166
                 for way_index in complete_ways.into_iter().rev() {