From 4b539c3b3ea40deb5b755a747fc606a54e3f2709 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Mon, 2 Sep 2019 08:38:43 +0800 Subject: [PATCH] refactor --- examples/pyramidal_maze.scad | 56 +++++++++--------------------------- examples/square_maze.scad | 36 ++++++++++++++++++++++- 2 files changed, 48 insertions(+), 44 deletions(-) diff --git a/examples/pyramidal_maze.scad b/examples/pyramidal_maze.scad index dfb8b99c..e9030673 100644 --- a/examples/pyramidal_maze.scad +++ b/examples/pyramidal_maze.scad @@ -5,41 +5,6 @@ maze_rows = 8; block_width = 10; stairs_width = 5; -module draw_block(wall_type, block_width, wall_thickness) { - if(wall_type == UPPER_WALL || wall_type == UPPER_RIGHT_WALL) { - // draw a upper wall - line2d( - [0, block_width], [block_width, block_width], wall_thickness - ); - } - - if(wall_type == RIGHT_WALL || wall_type == UPPER_RIGHT_WALL) { - // draw a right wall - line2d( - [block_width, block_width], [block_width, 0], wall_thickness - ); - } -} - -module draw_maze(rows, columns, blocks, block_width, wall_thickness) { - for(block = blocks) { - // move a block to a right position. - translate([get_x(block) - 1, get_y(block) - 1] * block_width) - draw_block( - get_wall_type(block), - block_width, - wall_thickness - ); - } - - // the lowermost wall - line2d([0, 0], [block_width * columns, 0], - wall_thickness); - // the leftmost wall - line2d([0, block_width], [0, block_width * rows], - wall_thickness); -} - module pyramid_with_stairs(base_width, stairs_width, rows) { height = base_width * sqrt(2) / 2; @@ -81,15 +46,20 @@ module pyramidal_staircase_maze(maze_rows, block_width, stairs_width) { square([block_width * maze_rows + stairs_width, block_width * maze_rows + stairs_width], center = true); translate([-(maze_rows * block_width) / 2, -(maze_rows * block_width) / 2, 0]) + difference() { + draw_maze( + maze_rows, + maze_rows, + maze_blocks, + block_width, + stairs_width + ); - - draw_maze( - maze_rows, - maze_rows, - maze_blocks, - block_width, - stairs_width - ); + translate([0, stairs_width]) + square(stairs_width, center = true); + translate([maze_rows * block_width, maze_rows * block_width - stairs_width]) + square(stairs_width, center = true); + } } } } diff --git a/examples/square_maze.scad b/examples/square_maze.scad index 29be020c..2a98225c 100644 --- a/examples/square_maze.scad +++ b/examples/square_maze.scad @@ -168,4 +168,38 @@ function try_routes_from(x, y, dir, maze, rows, columns) = rows, columns ) // road closed so return maze directly - : maze; \ No newline at end of file + : maze; + + +module draw_block(wall_type, block_width, wall_thickness) { + if(wall_type == UPPER_WALL || wall_type == UPPER_RIGHT_WALL) { + // draw a upper wall + line2d( + [0, block_width], [block_width, block_width], wall_thickness + ); + } + + if(wall_type == RIGHT_WALL || wall_type == UPPER_RIGHT_WALL) { + // draw a right wall + line2d( + [block_width, block_width], [block_width, 0], wall_thickness + ); + } +} + +module draw_maze(rows, columns, blocks, block_width, wall_thickness) { + for(block = blocks) { + // move a block to a right position. + translate([get_x(block) - 1, get_y(block) - 1] * block_width) + draw_block( + get_wall_type(block), + block_width, + wall_thickness + ); + } + + // the lowermost wall + line2d([0, 0], [block_width * columns, 0], wall_thickness); + // the leftmost wall + line2d([0, 0], [0, block_width * rows], wall_thickness); +} \ No newline at end of file