diff --git a/README.md b/README.md index b311fe20..578d3e96 100644 --- a/README.md +++ b/README.md @@ -247,7 +247,7 @@ These examples incubate dotSCAD and dotSCAD refactors these examples. See [examp ### Maze -- mz_square_blocks +- [mz_square_blocks](https://openhome.cc/eGossip/OpenSCAD/lib2x-mz_square_blocks.html) - mz_square_get - mz_square_initialize - mz_square_walls diff --git a/docs/images/lib2x-mz_square_cells-1.JPG b/docs/images/lib2x-mz_square_cells-1.JPG new file mode 100644 index 00000000..ecc16bcc Binary files /dev/null and b/docs/images/lib2x-mz_square_cells-1.JPG differ diff --git a/docs/lib2x-mz_square_cells.md b/docs/lib2x-mz_square_cells.md new file mode 100644 index 00000000..cefadedc --- /dev/null +++ b/docs/lib2x-mz_square_cells.md @@ -0,0 +1,63 @@ +# mz_square_cells + +This function returns cell data of a square-based maze. The data is a list of cells. A cell has the data structure `[x, y, type, visited]`. `x` and `y` are 0-based. `x` means x-th column and `y` means y-th row for a cell. The value of `type` can be `0`, `1`, ``2`, `3` or `4`. Setting them to constants is convenient. + + NO_WALL = 0; + TOP_WALL = 1; + RIGHT_WALL = 2; + TOP_RIGHT_WALL = 3; + MASK = 4; + +The cell data is seperated from views. You can use cell data to construct [different types of mazes](https://www.thingiverse.com/justinsdk/collections/maze-generator). + +**Since:** 2.5 + +## Parameters + +- `rows` : The number of rows. +- `columns` : The number of columns. +- `start` : The start point to travel the maze. Default to `[0, 0]`. +- `init_cells` : You can define your own initial cell data, a list of `[x, y, type, visited]`s. `visited` means the cell is visited or not. A visited cell won't be visited when traveling the maze. If you don't provide `init_cells`, `mz_square_cells` will generate one automatically. +- `x_wrapping` : Default to `false`. If you want to wrap the maze in the x direction, set it to `true`. The last column of cells will be adjacent to the first column of cells. +- `y_wrapping` : Default to `false`. If you want to wrap the maze in the y direction, set it to `true`. The last row of cells will be adjacent to the first row of cells. +- `seed` : The maze is traveling randomly. Use `seed` to initialize the pseudorandom number generator. + +## Examples + + use ; + use ; + + rows = 10; + columns = 10; + cell_width = 5; + wall_thickness = 2; + + NO_WALL = 0; + TOP_WALL = 1; + RIGHT_WALL = 2; + TOP_RIGHT_WALL = 3; + MASK = 4; + + cells = mz_square_cells(rows, columns); + + for(cell = cells) { + x = cell[0]; + y = cell[1]; + type = cell[2]; + + translate([x, y] * cell_width) { + if(type == TOP_WALL || type == TOP_RIGHT_WALL) { + line2d([0, cell_width], [cell_width, cell_width], wall_thickness); + } + + if(type == RIGHT_WALL || type == TOP_RIGHT_WALL) { + line2d([cell_width, cell_width], [cell_width, 0], wall_thickness); + } + } + } + + line2d([0, 0], [cell_width * rows, 0], wall_thickness); + line2d([0, 0], [0, cell_width * columns], wall_thickness); + +![mz_square_cells](images/lib2x-mz_square_cells-1.JPG) + diff --git a/src/maze/mz_square_cells.scad b/src/maze/mz_square_cells.scad index 4bd8e466..7eeef700 100644 --- a/src/maze/mz_square_cells.scad +++ b/src/maze/mz_square_cells.scad @@ -1,9 +1,19 @@ +/** +* mz_square_cells.scad +* +* @copyright Justin Lin, 2020 +* @license https://opensource.org/licenses/lgpl-3.0.html +* +* @see https://openhome.cc/eGossip/OpenSCAD/lib2x-mz_square_cells.html +* +**/ + use <_impl/_mz_cells_impl.scad>; use ; function mz_square_cells(rows, columns, start = [0, 0], init_cells, x_wrapping = false, y_wrapping = false, seed) = go_maze( - start[0], start[1], // starting point + start[0], start[1], is_undef(init_cells) ? mz_square_initialize(rows, columns) : init_cells, rows, columns, x_wrapping, y_wrapping, seed );