From 603611ab1631df91031d225f106a94af47471edc Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sun, 16 Feb 2020 16:47:11 +0800 Subject: [PATCH] refactor --- examples/maze/stereographic_hex_maze.scad | 110 +++++++++++++++++++++- 1 file changed, 108 insertions(+), 2 deletions(-) diff --git a/examples/maze/stereographic_hex_maze.scad b/examples/maze/stereographic_hex_maze.scad index 17176733..fa47356d 100644 --- a/examples/maze/stereographic_hex_maze.scad +++ b/examples/maze/stereographic_hex_maze.scad @@ -1,5 +1,6 @@ +use ; use ; -use ; +use ; x_cells = 10; cell_radius = 20; @@ -8,6 +9,108 @@ fn = 24; shadow = "YES"; // [YES, NO] wall_height = 1; +module build_hex_maze(y_cells, x_cells, maze_vector, cell_radius, wall_thickness, left_border = true, bottom_border = true) { + // NO_WALL = 0; + // UPPER_WALL = 1; + // RIGHT_WALL = 2; + // UPPER_RIGHT_WALL = 3; + + function no_wall(block) = get_wall_type(block) == 0; + function upper_wall(block) = get_wall_type(block) == 1; + function right_wall(block) = get_wall_type(block) == 2; + function upper_right_wall(block) = get_wall_type(block) == 3; + + function block(x, y, wall_type, visited) = [x, y, wall_type, visited]; + function get_x(block) = block[0]; + function get_y(block) = block[1]; + function get_wall_type(block) = block[2]; + + function cell_position(x_cell, y_cell) = + let( + grid_h = 2 * cell_radius * sin(60), + grid_w = cell_radius + cell_radius * cos(60) + ) + [grid_w * x_cell, grid_h * y_cell + (x_cell % 2 == 0 ? 0 : grid_h / 2), 0]; + + module hex_seg(begin, end) { + polyline2d( + [for(a = [begin:60:end]) + [cell_radius * cos(a), cell_radius * sin(a)]], + wall_thickness, + startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND" + ); + } + + module build_upper_right() { hex_seg(0, 60); } + module build_upper() { hex_seg(60, 120); } + module build_upper_left() { hex_seg(120, 180); } + module build_down_left() { hex_seg(180, 240); } + module build_down() { hex_seg(240, 300); } + module build_down_right() { hex_seg(300, 360); } + + module build_cell(block) { + module build_right_wall(x_cell) { + if(x_cell % 2 != 0) { + build_down_right(); + } + else { + build_upper_right(); + } + } + + module build_row_wall(x_cell, y_cell) { + if(x_cell % 2 != 0) { + build_upper_right(); + build_upper_left(); + } + else { + build_down_right(); + } + } + + x = get_x(block) - 1; + y = get_y(block) - 1; + + translate(cell_position(x, y)) { + build_row_wall(x, y); + + if(upper_wall(block) || upper_right_wall(block)) { + build_upper(); + } + if(right_wall(block) || upper_right_wall(block)) { + build_right_wall(x); + } + } + + } + + // create the wall of maze + for(block = maze_vector) { + build_cell(block); + } + + if(left_border) { + for(y = [0:y_cells - 1]) { + translate(cell_position(0, y)) { + build_upper_left(); + build_down_left(); + } + } + } + + if(bottom_border) { + for(x = [0:x_cells - 1]) { + translate(cell_position(x, 0)) { + build_down(); + if(x % 2 == 0) { + build_down_left(); + build_down_right(); + } + } + } + } +} + module hex_maze_stereographic_projection(x_cells, cell_radius, wall_thickness, fn, wall_height, shadow) { y_cells = round(0.866 * x_cells - 0.211); @@ -22,7 +125,10 @@ module hex_maze_stereographic_projection(x_cells, cell_radius, wall_thickness, f pyramid_height = square_w / sqrt(2); // create a maze - maze_vector = go_maze(1, 1, starting_maze(y_cells, x_cells), y_cells, x_cells); + maze_vector = mz_blocks( + [1, 1], + y_cells, x_cells + ); stereographic_extrude(square_w, $fn = fn) translate([grid_w - square_w / 2, grid_h - square_w / 2, 0])