space.rs 1.9 KB

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