space.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 per_inch = 5.0;
  12. let mut points: HashSet<(usize, usize)> = iproduct!(
  13. per_inch as usize .. per_inch as usize * 10,
  14. per_inch as usize .. per_inch as usize * 13
  15. ).collect();
  16. let mut src: Vec<(usize, usize)> = points.clone().into_iter().collect();
  17. rng.shuffle(&mut src);
  18. while let Some((ox, oy)) = src.pop() {
  19. let mut x = ox;
  20. let mut y = oy;
  21. if !points.remove(&(x, y)) { continue; }
  22. let mut total_points = 1usize;
  23. let mut line = gt::line(x as f64 / per_inch, y as f64 / per_inch);
  24. 'find_next: loop {
  25. let mut neighbors = vec![
  26. (x - 1, y),
  27. (x + 1, y),
  28. (x, y - 1),
  29. (x, y + 1),
  30. (x, y - 1),
  31. (x, y + 1),
  32. (x, y - 1),
  33. (x, y + 1),
  34. ];
  35. rng.shuffle(&mut neighbors);
  36. 'check_neighbors: for &(xn, yn) in neighbors.iter() {
  37. if points.remove(&(xn, yn)) {
  38. total_points += 1;
  39. line = line.to(xn as f64 / per_inch, yn as f64 / per_inch);
  40. x = xn;
  41. y = yn;
  42. continue 'find_next;
  43. }
  44. }
  45. break 'find_next;
  46. }
  47. if total_points > 3 {
  48. drawing.add(gt::circle((ox as f64 / per_inch, oy as f64 / per_inch), 0.02));
  49. drawing.add(gt::circle((x as f64 / per_inch, y as f64 / per_inch), 0.02));
  50. drawing.add(line);
  51. }
  52. }
  53. drawing.to_stdout();
  54. }