1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-01-18 06:38:14 +01:00
dotSCAD/examples/stereographic_hex_maze.scad

180 lines
4.3 KiB
OpenSCAD
Raw Normal View History

2019-09-01 11:58:44 +08:00
include <line2d.scad>;
include <polyline2d.scad>;
include <stereographic_extrude.scad>;
2019-09-01 18:20:41 +08:00
include <square_maze.scad>;
2019-09-01 11:58:44 +08:00
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(
2019-09-01 18:20:41 +08:00
[for(a = [60:60:120])
2019-09-01 11:58:44 +08:00
[cell_radius * cos(a), cell_radius * sin(a)]],
wall_thickness,
startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND"
);
}
module down_wall() {
polyline2d(
2019-09-01 18:20:41 +08:00
[for(a = [240:60:300])
2019-09-01 11:58:44 +08:00
[cell_radius * cos(a), cell_radius * sin(a)]],
wall_thickness,
startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND"
);
}
module up_left_wall() {
polyline2d(
2019-09-01 18:20:41 +08:00
[for(a = [120:60:180])
2019-09-01 11:58:44 +08:00
[cell_radius * cos(a), cell_radius * sin(a)]],
wall_thickness,
startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND"
2019-09-01 18:20:41 +08:00
);
2019-09-01 11:58:44 +08:00
}
module down_left_wall() {
polyline2d(
2019-09-01 18:20:41 +08:00
[for(a = [180:60:240])
2019-09-01 11:58:44 +08:00
[cell_radius * cos(a), cell_radius * sin(a)]],
wall_thickness,
startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND"
2019-09-01 18:20:41 +08:00
);
2019-09-01 11:58:44 +08:00
}
module up_right_wall() {
2019-09-01 18:20:41 +08:00
polyline2d(
[for(a = [0:60:60])
2019-09-01 11:58:44 +08:00
[cell_radius * cos(a), cell_radius * sin(a)]],
wall_thickness,
startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND"
2019-09-01 18:20:41 +08:00
);
2019-09-01 11:58:44 +08:00
}
module down_right_wall() {
2019-09-01 18:20:41 +08:00
polyline2d(
[for(a = [300:60:360])
2019-09-01 11:58:44 +08:00
[cell_radius * cos(a), cell_radius * sin(a)]],
wall_thickness,
startingStyle = "CAP_ROUND", endingStyle = "CAP_ROUND"
2019-09-01 18:20:41 +08:00
);
2019-09-01 11:58:44 +08:00
}
module right_walls() {
up_right_wall();
down_right_wall();
}
module cell_border_wall() {
if(x_cell == 0) {
up_left_wall();
down_left_wall();
}
2019-09-01 18:20:41 +08:00
if(x_cell == x_cells - 1 && y_cell == y_cells - 1) {
up_right_wall();
}
if(y_cell == 0) {
2019-09-01 11:58:44 +08:00
down_wall();
2019-09-01 18:20:41 +08:00
if(x_cell % 2 == 0) {
if(x_cell != 0) {
down_left_wall();
}
down_right_wall();
2019-09-01 11:58:44 +08:00
}
2019-09-01 18:20:41 +08:00
}
if(y_cell == y_cells - 1 && x_cell % 2 != 0) {
up_left_wall();
}
2019-09-01 11:58:44 +08:00
}
module cell_inner_wall() {
if(style == "upper") {
2019-09-01 18:20:41 +08:00
upper_wall();
if(x_cell % 2 != 0) {
up_right_wall();
}
2019-09-01 11:58:44 +08:00
} else if(style == "right") {
2019-09-01 18:20:41 +08:00
right_walls();
} else {
2019-09-01 11:58:44 +08:00
if(x_cell % 2 == 0) {
2019-09-01 18:20:41 +08:00
down_right_wall();
}
else {
2019-09-01 11:58:44 +08:00
up_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);
2019-09-01 20:03:51 +08:00
wall_type = cord[2];
2019-09-01 18:20:41 +08:00
2019-09-01 20:03:51 +08:00
if(wall_type == 1 || wall_type == 3) {
2019-09-01 11:58:44 +08:00
cell(x, y, "upper");
}
2019-09-01 20:03:51 +08:00
if(wall_type == 2 || wall_type == 3) {
2019-09-01 18:20:41 +08:00
cell(x, y, "right");
2019-09-01 11:58:44 +08:00
}
2019-09-01 18:20:41 +08:00
cell(x, y);
2019-09-01 11:58:44 +08:00
}
}
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
2019-09-01 18:20:41 +08:00
maze_vector = go_maze(1, 1, starting_maze(y_cells, x_cells), y_cells, x_cells);
2019-09-01 11:58:44 +08:00
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") {
2019-09-01 18:20:41 +08:00
color("black")
linear_extrude(wall_height)
2019-09-01 11:58:44 +08:00
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);