space.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. extern crate gunpowder_treason as gt;
  2. #[macro_use] extern crate itertools;
  3. extern crate rand;
  4. use std::collections::HashSet;
  5. use rand::Rng;
  6. fn main() {
  7. let (w, h) = (11.0, 14.0);
  8. let mut drawing = gt::svg(w, h);
  9. drawing.add(gt::rect((0.0, 0.0), (11.0, 14.0)));
  10. let mut rng = rand::thread_rng();
  11. let mut points: HashSet<(usize, usize)> = iproduct!(2..40, 2..52).collect();
  12. let mut src: Vec<(usize, usize)> = points.clone().into_iter().collect();
  13. rng.shuffle(&mut src);
  14. // for &(x, y) in points.iter() {
  15. // drawing.add(gt::line(x as f64 / 2.0, y as f64 / 2.0));
  16. // }
  17. while let Some((mut x, mut y)) = src.pop() {
  18. if !points.remove(&(x, y)) { continue; }
  19. let mut do_add = false;
  20. let mut line = gt::line(x as f64 / 4.0, y as f64 / 4.0);
  21. 'find_next: loop {
  22. let mut neighbors = vec![
  23. (x - 1, y),
  24. (x + 1, y),
  25. (x, y - 1),
  26. (x, y + 1),
  27. (x, y - 1),
  28. (x, y + 1),
  29. (x, y - 1),
  30. (x, y + 1),
  31. ];
  32. rng.shuffle(&mut neighbors);
  33. 'check_neighbors: for &(xn, yn) in neighbors.iter() {
  34. if points.remove(&(xn, yn)) {
  35. do_add = true;
  36. line = line.to(xn as f64 / 4.0, yn as f64 / 4.0);
  37. x = xn;
  38. y = yn;
  39. continue 'find_next;
  40. }
  41. }
  42. break 'find_next;
  43. }
  44. if do_add {
  45. drawing.add(line);
  46. }
  47. }
  48. drawing.to_stdout();
  49. }