Browse Source

Add naive space colonization plot

Getty Ritter 5 years ago
parent
commit
8e6016f18b
2 changed files with 61 additions and 1 deletions
  1. 5 1
      Cargo.toml
  2. 56 0
      src/space.rs

+ 5 - 1
Cargo.toml

@@ -18,4 +18,8 @@ path = "src/hexes.rs"
 
 [[bin]]
 name = "grid"
-path = "src/grid.rs"
+path = "src/grid.rs"
+
+[[bin]]
+name = "space"
+path = "src/space.rs"

+ 56 - 0
src/space.rs

@@ -0,0 +1,56 @@
+extern crate gunpowder_treason as gt;
+#[macro_use] extern crate itertools;
+extern crate rand;
+
+use std::collections::HashSet;
+use rand::Rng;
+
+fn main() {
+    let (w, h) = (11.0, 14.0);
+    let mut drawing = gt::svg(w, h);
+    drawing.add(gt::rect((0.0, 0.0), (11.0, 14.0)));
+
+    let mut rng = rand::thread_rng();
+
+    let mut points: HashSet<(usize, usize)> = iproduct!(2..40, 2..52).collect();
+    let mut src: Vec<(usize, usize)> = points.clone().into_iter().collect();
+    rng.shuffle(&mut src);
+
+    // for &(x, y) in points.iter() {
+    //     drawing.add(gt::line(x as f64 / 2.0, y as f64 / 2.0));
+    // }
+
+    while let Some((mut x, mut y)) = src.pop() {
+        if !points.remove(&(x, y)) { continue; }
+        let mut do_add = false;
+        let mut line = gt::line(x as f64 / 4.0, y as f64 / 4.0);
+        'find_next: loop {
+            let mut neighbors = vec![
+                (x - 1, y),
+                (x + 1, y),
+                (x, y - 1),
+                (x, y + 1),
+                (x, y - 1),
+                (x, y + 1),
+                (x, y - 1),
+                (x, y + 1),
+            ];
+            rng.shuffle(&mut neighbors);
+            'check_neighbors: for &(xn, yn) in neighbors.iter() {
+                if points.remove(&(xn, yn)) {
+                    do_add = true;
+                    line = line.to(xn as f64 / 4.0, yn as f64 / 4.0);
+                    x = xn;
+                    y = yn;
+                    continue 'find_next;
+                }
+            }
+            break 'find_next;
+        }
+        if do_add {
+            drawing.add(line);
+        }
+    }
+
+    drawing.to_stdout();
+}