From cd4dda3b39fc8261bffea308b38357e282cb3729 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sun, 26 Dec 2021 18:50:16 +0800 Subject: [PATCH] add mz_wang_tiles --- src/maze/_impl/_mz_wang_tiles_impl.scad | 33 ++++++++++++++++++ src/maze/mz_wang_tiles.scad | 46 +++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/maze/_impl/_mz_wang_tiles_impl.scad create mode 100644 src/maze/mz_wang_tiles.scad diff --git a/src/maze/_impl/_mz_wang_tiles_impl.scad b/src/maze/_impl/_mz_wang_tiles_impl.scad new file mode 100644 index 00000000..0801efd3 --- /dev/null +++ b/src/maze/_impl/_mz_wang_tiles_impl.scad @@ -0,0 +1,33 @@ +use <../../util/has.scad>; + +function _mz_wang_tiles_top(x, y) = + let( + nx = x * 2, + ny = y * 2 + ) + [[nx + 1, ny + 2]]; + +function _mz_wang_tiles_right(x, y) = + let( + nx = x * 2, + ny = y * 2 + ) + [[nx + 2, ny + 1]]; + +function _mz_wang_tiles_top_right(x, y) = + let( + nx = x * 2, + ny = y * 2 + ) + [[nx + 1, ny + 2], [nx + 2, ny + 1]]; + +function _mz_wang_tile_type(dots, x, y) = + let( + px = x * 2 + 1, + py = y * 2 + 1, + c1 = !has(dots, [px, py + 1], sorted = true) ? 1 : 0, + c2 = !has(dots, [px + 1, py], sorted = true) ? 2 : 0, + c3 = !has(dots, [px, py - 1], sorted = true) ? 4 : 0, + c4 = !has(dots, [px - 1, py], sorted = true) ? 8 : 0 + ) + c1 + c2 + c3 + c4; diff --git a/src/maze/mz_wang_tiles.scad b/src/maze/mz_wang_tiles.scad new file mode 100644 index 00000000..4b4d4b54 --- /dev/null +++ b/src/maze/mz_wang_tiles.scad @@ -0,0 +1,46 @@ +/** +* mz_wang_tiles.scad +* +* @copyright Justin Lin, 2020 +* @license https://opensource.org/licenses/lgpl-3.0.html +* +* @see https://openhome.cc/eGossip/OpenSCAD/lib3x-mz_wang_tiles.html +* +**/ + +use <_impl/_mz_wang_tiles_impl.scad>; +use ; +use ; +use <../util/sort.scad>; +use <../util/dedup.scad>; + +function mz_wang_tiles(rows, columns, start = [0, 0], init_cells, seed) = + let( + cells = mz_square_cells( + rows, columns, + init_cells = init_cells, + seed = seed + ), + all = concat( + [ + for(cell = cells) + let( + x = mz_square_get(cell, "x"), + y = mz_square_get(cell, "y"), + type = mz_square_get(cell, "t"), + pts = type == "TOP_WALL" ? _mz_wang_tiles_top(x, y) : + type == "RIGHT_WALL" ? _mz_wang_tiles_right(x, y) : + type == "TOP_RIGHT_WALL" || type == "MASK" ? _mz_wang_tiles_top_right(x, y) : [] + ) + each pts + ], + [for(x = [0:columns - 1]) [x * 2 + 1, 0]], + [for(y = [0:rows - 1]) [0, y * 2 + 1]] + ), + dot_pts = dedup(sort(all, by = "vt")) + ) + [ + for(y = [0:rows - 1]) + for(x = [0:columns - 1]) + [x, y, _mz_wang_tile_type(dot_pts, x, y)] + ]; \ No newline at end of file