From 2cf3ac67817a4c1558a5009518fdb03f027449d2 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 11 Aug 2022 09:33:14 +0800 Subject: [PATCH] refactor --- src/maze/_impl/_mz_tiles_impl.scad | 36 ----------------- src/maze/mz_tiles.scad | 63 +++++++++++++++--------------- 2 files changed, 32 insertions(+), 67 deletions(-) delete mode 100644 src/maze/_impl/_mz_tiles_impl.scad diff --git a/src/maze/_impl/_mz_tiles_impl.scad b/src/maze/_impl/_mz_tiles_impl.scad deleted file mode 100644 index 4d3fe4fe..00000000 --- a/src/maze/_impl/_mz_tiles_impl.scad +++ /dev/null @@ -1,36 +0,0 @@ -use <../../util/set/hashset_has.scad> - -include <../../__comm__/_pt2_hash.scad> - -function _mz_tiles_top(x, y) = - let( - nx = x * 2, - ny = y * 2 - ) - [[nx + 1, ny + 2]]; - -function _mz_tiles_right(x, y) = - let( - nx = x * 2, - ny = y * 2 - ) - [[nx + 2, ny + 1]]; - -function _mz_tiles_top_right(x, y) = - let( - nx = x * 2, - ny = y * 2 - ) - [[nx + 1, ny + 2], [nx + 2, ny + 1]]; - -hash = function(p) _pt2_hash(p); -function _mz_tile_type(dots, x, y) = - let( - px = x * 2 + 1, - py = y * 2 + 1, - c1 = !hashset_has(dots, [px, py + 1], hash = hash) ? 1 : 0, - c2 = !hashset_has(dots, [px + 1, py], hash = hash) ? 2 : 0, - c3 = !hashset_has(dots, [px, py - 1], hash = hash) ? 4 : 0, - c4 = !hashset_has(dots, [px - 1, py], hash = hash) ? 8 : 0 - ) - c1 + c2 + c3 + c4; diff --git a/src/maze/mz_tiles.scad b/src/maze/mz_tiles.scad index 9195e570..ddca9b61 100644 --- a/src/maze/mz_tiles.scad +++ b/src/maze/mz_tiles.scad @@ -1,52 +1,53 @@ /** * mz_tiles.scad * -* @copyright Justin Lin, 2020 +* @copyright Justin Lin, 2022 * @license https://opensource.org/licenses/lgpl-3.0.html * * @see https://openhome.cc/eGossip/OpenSCAD/lib3x-mz_tiles.html * **/ -use <_impl/_mz_tiles_impl.scad> use -use <../util/set/hashset.scad> - -include <../__comm__/_pt2_hash.scad> function mz_tiles(cells, left_border = true, bottom_border = true) = let( rows = len(cells), columns = len(cells[0]), - top_cells = cells[rows - 1], - right_cells = [for(r = [0:rows - 1]) cells[r][columns - 1]], - all = concat( + edges = [ + for(y = 0; y <= rows; y = y + 1) [ - for(row = cells, cell = row) - let( - x = cell.x, - y = cell.y, - type = mz_square_get(cell, "t"), - pts = type == "TOP_WALL" ? _mz_tiles_top(x, y) : - type == "RIGHT_WALL" ? _mz_tiles_right(x, y) : - type == "TOP_RIGHT_WALL" || type == "MASK" ? _mz_tiles_top_right(x, y) : [] - ) - each pts - ], - bottom_border ? [for(x = [0:columns - 1]) [x * 2 + 1, 0]] : [ - for(x = [0:columns - 1]) - let(type = mz_square_get(top_cells[x], "t")) - [x * 2 + 1, type == "RIGHT_WALL" || type == "NO_WALL" ? 1 : 0] - ], - left_border ? [for(y = [0:rows - 1]) [0, y * 2 + 1]] : [ - for(y = [0:rows - 1]) - let(type = mz_square_get(right_cells[y], "t")) - [type == "TOP_WALL" || type == "NO_WALL" ? 1 : 0, y * 2 + 1] + for(x = 0; x <= columns; x = x + 1) + if(y == 0 && x == 0) + [0, 0] + else if(y == 0) + let(type = mz_square_get(cells[rows - 1][x - 1], "t")) + [ + bottom_border ? 0 : type == "NO_WALL" || type == "RIGHT_WALL" ? 1 : 0, + 0 + ] + else if(x == 0) + let(type = mz_square_get(cells[y - 1][columns - 1], "t")) + [ + 0, + left_border ? 0 : type == "NO_WALL" || type == "TOP_WALL" ? 1 : 0 + ] + else + let(type = mz_square_get(cells[y - 1][x - 1], "t")) + [ + type == "NO_WALL" || type == "RIGHT_WALL" ? 1 : 0, + type == "NO_WALL" || type == "TOP_WALL" ? 1 : 0 + ] ] - ), - dot_pts = hashset(all, hash = function(p) _pt2_hash(p)) + ], + _mz_tile_type = function(edges, cx, cy) + let(x = cx + 1, y = cy + 1) + (edges[y][x][0] == 1 ? 1 : 0) + + (edges[y][x][1] == 1 ? 2 : 0) + + (edges[y - 1][x][0] == 1 ? 4 : 0) + + (edges[y][x - 1][1] == 1 ? 8 : 0) ) [ for(y = [0:rows - 1], x = [0:columns - 1]) - [x, y, _mz_tile_type(dot_pts, x, y)] + [x, y, _mz_tile_type(edges, x, y)] ]; \ No newline at end of file