Browse Source

Add Sunburst and Planets programs

Getty Ritter 5 years ago
parent
commit
b7d2ac97d2
4 changed files with 111 additions and 37 deletions
  1. 9 1
      Cargo.toml
  2. 40 36
      src/hexes.rs
  3. 38 0
      src/planets.rs
  4. 24 0
      src/sunburst.rs

+ 9 - 1
Cargo.toml

@@ -18,4 +18,12 @@ path = "src/hexes.rs"
 
 [[bin]]
 name = "grid"
-path = "src/grid.rs"
+path = "src/grid.rs"
+
+[[bin]]
+name = "sunburst"
+path = "src/sunburst.rs"
+
+[[bin]]
+name = "planets"
+path = "src/planets.rs"

+ 40 - 36
src/hexes.rs

@@ -1,61 +1,65 @@
 extern crate gunpowder_treason as gt;
-#[macro_use] extern crate itertools;
+extern crate itertools;
 
 use std::f64::consts::PI;
 
 fn main() {
-    let mut drawing = gt::svg(11.0, 14.0);
-    drawing.add(gt::rect((0.0, 0.0), (11.0, 14.0)));
+    let cent = 0.393701f64;
+    let mut drawing = gt::svg(23.0 * cent, 30.0 * cent);
+    drawing.add(gt::rect((0.0, 0.0), (23.0 * cent, 30.0 * cent)));
     const N: f64 = 0.2;
     const THETA: f64 = PI / 3.0;
-    const COLS: u32 = 16;
-    const ROWS: u32 = 36;
+    const COLS: u32 = 13;
+    const ROWS: u32 = 30;
 
     const XO: f64 = 0.8;
     const YO: f64 = 0.8;
 
-    for (y, x) in iproduct!(0..ROWS, 0..COLS) {
-        let d = 2.0 * (N + THETA.cos() * N);
-        let a = THETA.cos() * N;
-        let h = THETA.sin() * N;
-        let h2 = THETA.sin() * N * 2.0;
-
+    let a = THETA.cos() * N;
+    let h = THETA.sin() * N;
+    let h2 = THETA.sin() * N * 2.0;
+    let d = 2.0 * (N + THETA.cos() * N);
+    for y in 0..ROWS {
         let by = YO + y as f64 * h2;
+        for x in 0..COLS {
 
-        drawing.add(
-            gt::line(XO + d * x as f64, by)
-                .to(XO + d * x as f64 - a, by + h)
-        );
-        drawing.add(
-            gt::line(XO + d * x as f64, by)
-                .to(XO + d * x as f64 + N, by)
-        );
-        drawing.add(
-            gt::line(XO + d * x as f64 + N, by)
-                .to(XO + d * x as f64 + N + a, by + h)
-        );
-        if x < COLS - 1 {
             drawing.add(
-                gt::line(XO + d * x as f64 + N + a, by + h)
-                    .to(XO + d * x as f64 + N * 2.0 + a, by + h)
+                gt::line(XO + d * x as f64 - a, by + h)
+                    .to(XO + d * x as f64, by)
+            );
+            drawing.add(
+                gt::line(XO + d * x as f64, by)
+                    .to(XO + d * x as f64 + N, by)
+            );
+            drawing.add(
+                gt::line(XO + d * x as f64 + N, by)
+                    .to(XO + d * x as f64 + N + a, by + h)
             );
+            if x < COLS - 1 {
+                drawing.add(
+                    gt::line(XO + d * x as f64 + N + a, by + h)
+                        .to(XO + d * x as f64 + N * 2.0 + a, by + h)
+                );
+            }
         }
 
-        drawing.add(
-            gt::line(XO + d * x as f64 - a, by + h)
-                .to(XO + d * x as f64, by + h * 2.0)
-        );
-        if y == ROWS - 1 {
+        for x in 0..COLS {
+
             drawing.add(
                 gt::line(XO + d * x as f64, by + h * 2.0)
+                    .to(XO + d * x as f64 - a, by + h)
+            );
+            if y == ROWS - 1 {
+                drawing.add(
+                    gt::line(XO + d * x as f64, by + h * 2.0)
+                        .to(XO + d * x as f64 + N, by + h * 2.0)
+                );
+            }
+            drawing.add(
+                gt::line(XO + d * x as f64 + N + a, by + h)
                     .to(XO + d * x as f64 + N, by + h * 2.0)
             );
         }
-        drawing.add(
-            gt::line(XO + d * x as f64 + N + a, by + h)
-                .to(XO + d * x as f64 + N, by + h * 2.0)
-        );
-
     }
     drawing.to_stdout();
 }

+ 38 - 0
src/planets.rs

@@ -0,0 +1,38 @@
+extern crate gunpowder_treason as gt;
+extern crate rand;
+
+use rand::random;
+
+fn main() {
+    let cent = 0.393701;
+    let mut drawing = gt::svg(9.0, 12.0);
+
+    let xs: Vec<Vec<f64>> = (1..9).map(|_: u32| {
+        (0..(random::<u32>() % 8) + 4).map(|_: u32| {
+            (random::<f64>() * 0.3 + 0.3) * 0.2
+        }).collect()
+    }).collect();
+
+    for (x, rs) in xs.iter().enumerate() {
+        let max = rs.len();
+        let tot = (max - 1) as f64;
+        let dy = 10.0 / tot;
+        let mut ry = 1.0;
+        let r2 = rs.clone();
+        for (y, c) in rs.iter().enumerate() {
+            let rx = 1.0 + x as f64;
+            eprintln!("{}, {}, {}, {}", x, y, c, ry);
+            drawing.add(gt::circle(
+                (rx, 1.0 + dy * y as f64),
+                *c,
+            ));
+            if y != max - 1 {
+                let cn = r2[y + 1];
+                drawing.add(gt::line(rx, ry + c).to(rx, ry + dy - cn));
+            }
+            ry += dy;
+        }
+    }
+
+    drawing.to_stdout();
+}

+ 24 - 0
src/sunburst.rs

@@ -0,0 +1,24 @@
+extern crate gunpowder_treason as gt;
+extern crate rand;
+
+fn main() {
+    let (w, h) = (8.5, 11.0);
+    let mut drawing = gt::svg(w, h);
+
+    let rs = (0u64..100)
+        .scan((0u64, 5.5f64), |(_, s), x: u64| {
+            Some((x, *s + (rand::random::<f64>() * 0.5) - 0.25))
+    });
+
+    for (x, r) in rs {
+        let xn = 0.8 + (x as f64 * (8.5 - 1.6)) / 100.0;
+        drawing.add(
+            gt::line(xn, 0.8).to(xn, r - 0.8)
+        );
+        drawing.add(
+            gt::line(xn, 10.2).to(xn, r + 0.8)
+        );
+    }
+
+    drawing.to_stdout();
+}