From 3c29270a8fd645543f304497de016f6f24c1a258 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sat, 3 Oct 2020 21:17:23 +0800 Subject: [PATCH] refactor --- examples/maze/stereographic_hex_maze.scad | 132 ++++------------------ 1 file changed, 25 insertions(+), 107 deletions(-) diff --git a/examples/maze/stereographic_hex_maze.scad b/examples/maze/stereographic_hex_maze.scad index 13f7c19b..65b5c798 100644 --- a/examples/maze/stereographic_hex_maze.scad +++ b/examples/maze/stereographic_hex_maze.scad @@ -1,140 +1,58 @@ use ; use ; use ; -use ; +use ; -x_cells = 10; +columns = 10; cell_radius = 20; wall_thickness = 12; 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) { - function no_wall(block) = get_wall_type(block) == "NO_WALL"; - function upper_wall(block) = get_wall_type(block) == "UPPER_WALL"; - function right_wall(block) = get_wall_type(block) == "RIGHT_WALL"; - function upper_right_wall(block) = get_wall_type(block) == "UPPER_RIGHT_WALL"; - - function get_x(block) = mz_get(block, "x"); - function get_y(block) = mz_get(block, "y"); - function get_wall_type(block) = mz_get(block, "w"); - - 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); +module hex_maze_stereographic_projection(columns, cell_radius, wall_thickness, fn, wall_height, shadow) { + rows = round(0.866 * columns - 0.211); grid_h = 2 * cell_radius * sin(60); grid_w = cell_radius + cell_radius * cos(60); - square_w = grid_w * (x_cells - 1) + cell_radius * 2 + wall_thickness * 2; - square_h = grid_h * y_cells + grid_h / 2 + wall_thickness * 2; + square_w = grid_w * (columns - 1) + cell_radius * 2 + wall_thickness * 2; + square_h = grid_h * rows + grid_h / 2 + wall_thickness * 2; square_offset_x = square_w / 2 -cell_radius - wall_thickness; square_offset_y = square_h / 2 -grid_h / 2 - wall_thickness; pyramid_height = square_w / sqrt(2); // create a maze - maze_vector = mz_blocks( + blocks = mz_blocks( [1, 1], - y_cells, x_cells + rows, columns ); + walls = mz_hex_walls(blocks, rows, columns, cell_radius, wall_thickness); + stereographic_extrude(square_w, $fn = fn) translate([grid_w - square_w / 2, grid_h - square_w / 2, 0]) - build_hex_maze(y_cells, x_cells, maze_vector, cell_radius, wall_thickness); + for(wall = walls) { + polyline2d( + wall, + wall_thickness, + startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND" + ); + } if(shadow == "YES") { color("black") linear_extrude(wall_height) translate([grid_w - square_w / 2, grid_h - square_w / 2, 0]) - build_hex_maze(y_cells, x_cells, maze_vector, cell_radius, wall_thickness); + for(wall = walls) { + polyline2d( + wall, + wall_thickness, + startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND" + ); + } } } -hex_maze_stereographic_projection(x_cells, cell_radius, wall_thickness, fn, wall_height, shadow); \ No newline at end of file +hex_maze_stereographic_projection(columns, cell_radius, wall_thickness, fn, wall_height, shadow); \ No newline at end of file