# 迷宮的牆

``````上牆, 上牆. 上牆, 上牆

``````

# 區塊資料結構

``````NO_WALL = 0;       // 無牆
UP_WALL = 1;       // 上牆
RIGHT_WALL = 2;    // 右牆
UP_RIGHT_WALL = 3; // 都有
``````

``````// [x, y, wall_type]
maze_blocks =  [
[1, 4, 1], [2, 4, 1], [3, 4, 1], [4, 4, 1],
[1, 3, 2], [2, 3, 3], [3, 3, 1], [4, 3, 2],
[1, 2, 0], [2, 2, 2], [3, 2, 2], [4, 2, 2],
[1, 1, 1], [2, 1, 1], [3, 1, 2], [4, 1, 2]
];
``````

``````maze_blocks =  [
[1, 1, 1, 1],
[2, 3, 1, 2],
[0, 2, 2, 2],
[1, 1, 2, 2]
];
``````

``````maze_blocks =  [b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16];
``````

``````function block_data(x, y, wall_type) = [x, y, wall_type];
function get_x(block_data) = block_data[0];
function get_y(block_data) = block_data[1];
function get_wall_type(block_data) = block_data[2];
``````

# 繪製迷宮

``````module draw_block(wall_type, block_width, wall_thickness) {
if(wall_type == UP_WALL || wall_type == UP_RIGHT_WALL) {
// 畫上牆
polyline(
[[0, block_width], [block_width, block_width]], wall_thickness
);
}

if(wall_type == RIGHT_WALL || wall_type == UP_RIGHT_WALL) {
// 畫右牆
polyline(
[[block_width, block_width], [block_width, 0]], wall_thickness
);
}
}
``````

``````module draw_maze(rows, columns, blocks, block_width, wall_thickness) {
for(block = blocks) {
// 將每個畫好的區塊移至對應的位置
translate([get_x(block) - 1, get_y(block) - 1] * block_width)
draw_block(
get_wall_type(block),
block_width,
wall_thickness
);
}

// 最底牆
polyline(
[[0, 0], [block_width * columns, 0]],
wall_thickness);
// 最左牆
polyline(
[[0, block_width], [0, block_width * rows]],
wall_thickness);
}
``````

``````module line(point1, point2, width = 1, cap_round = true) {
angle = 90 - atan((point2[1] - point1[1]) / (point2[0] - point1[0]));
offset_x = 0.5 * width * cos(angle);
offset_y = 0.5 * width * sin(angle);

offset1 = [-offset_x, offset_y];
offset2 = [offset_x, -offset_y];

if(cap_round) {
translate(point1) circle(d = width, \$fn = 24);
translate(point2) circle(d = width, \$fn = 24);
}

polygon(points=[
point1 + offset1, point2 + offset1,
point2 + offset2, point1 + offset2
]);
}

module polyline(points, width = 1) {
module polyline_inner(points, index) {
if(index < len(points)) {
line(points[index - 1], points[index], width);
polyline_inner(points, index + 1);
}
}

polyline_inner(points, 1);
}

// 牆面常數

NO_WALL = 0;       // 無牆
UP_WALL = 1;       // 上牆
RIGHT_WALL = 2;    // 右牆
UP_RIGHT_WALL = 3; // 都有

function block_data(x, y, wall_type) = [x, y, wall_type];
function get_x(block_data) = block_data[0];
function get_y(block_data) = block_data[1];
function get_wall_type(block_data) = block_data[2];

module draw_block(wall_type, block_width, wall_thickness) {
if(wall_type == UP_WALL || wall_type == UP_RIGHT_WALL) {
// 畫上牆
polyline(
[[0, block_width], [block_width, block_width]], wall_thickness
);
}

if(wall_type == RIGHT_WALL || wall_type == UP_RIGHT_WALL) {
// 畫右牆
polyline(
[[block_width, block_width], [block_width, 0]], wall_thickness
);
}
}

module draw_maze(rows, columns, blocks, block_width, wall_thickness) {
for(block = blocks) {
// 將每個畫好的區塊移至對應的位置
translate([get_x(block) - 1, get_y(block) - 1] * block_width)
draw_block(
get_wall_type(block),
block_width,
wall_thickness
);
}

// 最底牆
polyline(
[[0, 0], [block_width * columns, 0]],
wall_thickness);
// 最左牆
polyline(
[[0, block_width], [0, block_width * rows]],
wall_thickness);
}

block_width = 3;
wall_thickness = 1;
maze_rows = 4;
maze_columns = 4;

// [x, y, wall_type]
maze_blocks =  [
[1, 4, 1], [2, 4, 1], [3, 4, 1], [4, 4, 1],
[1, 3, 2], [2, 3, 3], [3, 3, 1], [4, 3, 2],
[1, 2, 0], [2, 2, 2], [3, 2, 2], [4, 2, 2],
[1, 1, 1], [2, 1, 1], [3, 1, 2], [4, 1, 2]
];

draw_maze(
maze_rows,
maze_columns,
maze_blocks,
block_width,
wall_thickness
);
``````