From 4708f039fcff5ec72f1b7d0a9f26dbacc8f97a98 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sun, 1 Sep 2019 11:58:44 +0800 Subject: [PATCH] hex_maze_stereographic_projection --- .../hex_maze_stereographic_projection.scad | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 examples/hex_maze_stereographic_projection.scad diff --git a/examples/hex_maze_stereographic_projection.scad b/examples/hex_maze_stereographic_projection.scad new file mode 100644 index 00000000..811c5e58 --- /dev/null +++ b/examples/hex_maze_stereographic_projection.scad @@ -0,0 +1,173 @@ +include ; +include ; +include ; +include ; + +x_cells = 5; +cell_radius = 20; +wall_thickness = 12; +fn = 24; +shadow = "YES"; // [YES, NO] +wall_height = 1; + +// create a maze +module hex_maze(y_cells, x_cells, maze_vector, cell_radius, wall_thickness) { + + // style : upper/rights/right + module cell(x_cell, y_cell, style) { + module upper_wall() { + polyline2d( + [for(a = [240:60:300]) + [cell_radius * cos(a), cell_radius * sin(a)]], + wall_thickness, + startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND" + ); + } + + module down_wall() { + polyline2d( + [for(a = [60:60:120]) + [cell_radius * cos(a), cell_radius * sin(a)]], + wall_thickness, + startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND" + ); + } + + module up_left_wall() { + polyline2d( + [for(a = [180:60:270]) + [cell_radius * cos(a), cell_radius * sin(a)]], + wall_thickness, + startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND" + ); + } + + module down_left_wall() { + polyline2d( + [for(a = [120:60:180]) + [cell_radius * cos(a), cell_radius * sin(a)]], + wall_thickness, + startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND" + ); + } + + module up_right_wall() { + polyline2d( + [for(a = [300:60:360]) + [cell_radius * cos(a), cell_radius * sin(a)]], + wall_thickness, + startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND" + ); + } + + module down_right_wall() { + polyline2d( + [for(a = [0:60:60]) + [cell_radius * cos(a), cell_radius * sin(a)]], + wall_thickness, + startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND" + ); + } + + module right_walls() { + up_right_wall(); + down_right_wall(); + } + + module cell_border_wall() { + if(y_cell == 0 && x_cell % 2 == 0) { + if(x_cell != 0) { + up_left_wall(); + } + up_right_wall(); + } + + if(x_cell == 0) { + up_left_wall(); + down_left_wall(); + } + + if(y_cell == (y_cells - 1)) { + down_wall(); + if(x_cell % 2 != 0) { + down_left_wall(); + } + } + } + + module cell_inner_wall() { + if(style == "upper") { + upper_wall(); + } else if(style == "rights") { + right_walls(); + } else if(style == "right") { + if(x_cell % 2 == 0) { + up_right_wall(); + } else { + down_right_wall(); + } + } + } + + module cell_wall() { + cell_inner_wall(); + cell_border_wall(); + } + + grid_h = 2 * cell_radius * sin(60); + grid_w = cell_radius + cell_radius * cos(60); + + translate([grid_w * x_cell, grid_h * y_cell + (x_cell % 2 == 0 ? 0 : grid_h / 2), 0]) + cell_wall(); + } + + // create the wall of maze + + for(i = [0:len(maze_vector) - 1]) { + cord = maze_vector[i]; + x = (cord[0] - 1) ; + y = (cord[1] - 1); + v = cord[3]; + + if(v == 1 || v == 3) { + cell(x, y, "upper"); + + } + if(v == 2 || v == 3) { + cell(x, y, "rights"); + } + if(v == 0 || v == 1) { + cell(x, y, "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); + + 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_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 = go_maze(1, 1, y_cells, x_cells, init_maze(y_cells, x_cells)); + + stereographic_extrude(square_w, $fn = fn) + translate([grid_w - square_w / 2, grid_h - square_w / 2, 0]) + hex_maze(y_cells, x_cells, maze_vector, cell_radius, wall_thickness); + + if(shadow == "YES") { + color("black") linear_extrude(wall_height) + translate([grid_w - square_w / 2, grid_h - square_w / 2, 0]) + hex_maze(y_cells, x_cells, maze_vector, cell_radius, wall_thickness); + } + +} + +hex_maze_stereographic_projection(x_cells, cell_radius, wall_thickness, fn, wall_height, shadow); \ No newline at end of file