/ examples / maze / island_maze.scad
island_maze.scad
 1  use <maze/mz_square.scad>
 2  use <maze/mz_squarewalls.scad>
 3  use <polyline2d.scad>
 4  use <noise/nz_perlin2.scad>
 5  use <util/choose.scad>
 6  
 7  rows = 30;
 8  columns = 30;
 9  cell_width = 3;
10  wall_thickness = 1.5;
11  moutain_height = 30;
12  base_height = 3;
13  seed = 35;
14  smoothing  = 25;
15  
16  island_maze();
17  
18  module island_maze() {
19      dirs = function(x, y, cells, seed) 
20             let(
21                 nz = nz_perlin2(x * cell_width / smoothing, y * cell_width / smoothing, seed),
22                 sd = y * len(cells[0]) + x + seed,
23                 h = choose([[0, 2], [2, 0]], seed = sd),
24                 v = choose([[1, 3], [3, 1]], seed = sd)
25             )
26             nz > 0 ? concat(h, v) : concat(v, h);
27  
28      cells = mz_square(rows, columns, seed = seed, directions = dirs);
29      walls = mz_squarewalls(cells, cell_width);
30  
31      color("green")
32      for(wall = walls) {
33          for(i = [0:len(wall) - 2]) {
34              p1 = wall[i];
35              p2 = wall[i + 1];
36              h1 = nz_perlin2(p1.x / smoothing , p1.y / smoothing , seed);
37              h2 = nz_perlin2(p2.x / smoothing , p2.y / smoothing , seed);
38              hull() {
39                 linear_extrude(h1 > 0 ? h1 * moutain_height + base_height : base_height)
40                  translate(p1)
41                      square(wall_thickness);
42                  
43                 linear_extrude(h2 > 0 ? h2 * moutain_height + base_height : base_height)
44                  translate(p2)
45                      square(wall_thickness);
46              }
47          }
48      }
49  
50      if($preview) {
51          color("blue")
52          translate([wall_thickness, wall_thickness] / 2)
53          for(wall = walls) {
54              linear_extrude(base_height)
55              polyline2d(wall, wall_thickness, joinStyle = "JOIN_MITER");
56          }
57      }
58  }