| 309 | } |
| 310 | |
| 311 | fn ragged_grid(width: usize, height: usize, size_x: f64, size_y: f64) -> GeometryGraph<Undirected> { |
| 312 | let nodes = (width + 1) * (height + 1); |
| 313 | let edges = 2 * width * height - width - height; |
| 314 | let mut graph = GeometryGraph::<Undirected>::with_capacity(nodes, edges); |
| 315 | |
| 316 | // Add the nodes |
| 317 | for j in 0..=height { |
| 318 | for i in 0..=(width + 1) { |
| 319 | let x = quad_i2x(i, size_x, 0.0); |
| 320 | let y = quad_j2y(j, size_y, 0.0); |
| 321 | let point = Point::new(x, y); |
| 322 | let index = graph.add_node(point); |
| 323 | let node_index = (width + 2) * j + i; |
| 324 | tracing::trace!("id={} node={point:?}", index.index()); |
| 325 | debug_assert_eq!(index.index(), node_index); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // Add the edges |
| 330 | for j in 0..=height { |
| 331 | // Because of the ragged edges, you need one more index to get another column of cells |
| 332 | for i in 0..=(width + 1) { |
| 333 | let current_index = (width + 2) * j + i; |
| 334 | tracing::trace!("adding neighbors for id={current_index}"); |
| 335 | |
| 336 | // Add the four neighbors, starting at the left and working clockwise |
| 337 | // |
| 338 | // 0-1-2-3 |
| 339 | // / / / |
| 340 | // 4-5-6-7 |
| 341 | // / / / |
| 342 | // 8-9-0-1 |
| 343 | if i > 0 { |
| 344 | let left = current_index - 1; |
| 345 | tracing::trace!("added left id={left}"); |
| 346 | graph.update_edge(current_index.into(), left.into(), ()); |
| 347 | } |
| 348 | if j > 0 { |
| 349 | let upper = current_index - (width + 1); |
| 350 | tracing::trace!("added upper id={upper}"); |
| 351 | graph.update_edge(current_index.into(), upper.into(), ()); |
| 352 | } |
| 353 | if i < (width + 1) { |
| 354 | let right = current_index + 1; |
| 355 | tracing::trace!("added right id={right}"); |
| 356 | graph.update_edge(current_index.into(), right.into(), ()); |
| 357 | } |
| 358 | if j < height { |
| 359 | let lower = current_index + width + 1; |
| 360 | tracing::trace!("added lower id={lower}"); |
| 361 | graph.update_edge(current_index.into(), lower.into(), ()); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | graph |
| 366 | } |
| 367 | |
| 368 | fn hex_grid(width: usize, height: usize, size_x: f64, _size_y: f64) -> GeometryGraph<Undirected> { |