From 7689ac9c552434fd7e91a41f0256bb00a412d1f0 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 18 Aug 2022 11:46:47 +0800 Subject: [PATCH] add giant maze --- examples/maze/giant_maze.scad | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 examples/maze/giant_maze.scad diff --git a/examples/maze/giant_maze.scad b/examples/maze/giant_maze.scad new file mode 100644 index 00000000..5c533ef9 --- /dev/null +++ b/examples/maze/giant_maze.scad @@ -0,0 +1,86 @@ +use +use +use +use +use + +rows = 5; +columns = 5; +height = 5; +height_smooth = 5; + +giant_maze(); + +module giant_maze() { + bitmaps = [ + [[0, 0, 0], [0, 0, 0], [0, 0, 0]], + [[0, 0, 0], [0, 1, 0], [0, 1, 0]], + [[0, 0, 0], [0, 1, 1], [0, 0, 0]], + [[0, 0, 0], [0, 1, 1], [0, 1, 0]], + [[0, 1, 0], [0, 1, 0], [0, 0, 0]], + [[0, 1, 0], [0, 1, 0], [0, 1, 0]], + [[0, 1, 0], [0, 1, 1], [0, 0, 0]], + [[0, 1, 0], [0, 1, 1], [0, 1, 0]], + [[0, 0, 0], [1, 1, 0], [0, 0, 0]], + [[0, 0, 0], [1, 1, 0], [0, 1, 0]], + [[0, 0, 0], [1, 1, 1], [0, 0, 0]], + [[0, 0, 0], [1, 1, 1], [0, 1, 0]], + [[0, 1, 0], [1, 1, 0], [0, 0, 0]], + [[0, 1, 0], [1, 1, 0], [0, 1, 0]], + [[0, 1, 0], [1, 1, 1], [0, 0, 0]], + [[0, 1, 0], [1, 1, 1], [0, 1, 0]] + ]; + + function tiles2bits(tiles) = + [ + for(tile = tiles) + let( + bitmap = bitmaps[tile[2]], + sx = tile.x * 3, + sy = tile.y * 3 + ) + each [for(y = [0:2], x = [0:2]) [[sx + x, sy + y], bitmap[y][x]]] + ]; + + bits = tiles2bits(mz_tiles(mz_square(rows, columns))); + + m = [ + for(r = [0:rows * 3 - 1]) + [ + for(c = [0:columns * 3 - 1]) + let(i = find_index(bits, function(elem) elem[0] == [c, r])) + bits[i][1] + ] + ]; + + cells = vrn2_cells_space([len(m[0]), len(m)], 1); + seed = rands(0, 1000, 1)[0]; + for(cell = cells) { + cell_pt = cell[0]; + cell_poly = cell[1]; + + noise = 2 * nz_perlin2(cell_pt.x / height_smooth, cell_pt.y / height_smooth, seed) + height; + + b = m[cell_pt[1]][cell_pt[0]]; + if(!is_undef(b) && b == 1) { + color("LightGrey") + translate(cell_pt) + linear_extrude(noise, scale = 0.9) + translate(-cell_pt) + polygon(cell_poly); + } + else { + color("gray") + translate(cell_pt) + linear_extrude(height / 2, scale = 0.75) + translate(-cell_pt) + polygon(cell_poly); + } + } + + color("DimGray") + linear_extrude(height / 5) + for(cell = cells) { + polygon(cell[1]); + } +} \ No newline at end of file