From a45434fa6e3cc4e52ddecaa70d1d0d0049d1c65c Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sun, 15 May 2022 16:11:21 +0800 Subject: [PATCH] optimization --- src/maze/_impl/_mz_hamiltonian_impl.scad | 49 +++++++++++------------- src/maze/mz_hamiltonian.scad | 8 ++-- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/maze/_impl/_mz_hamiltonian_impl.scad b/src/maze/_impl/_mz_hamiltonian_impl.scad index 2e894428..863960d5 100644 --- a/src/maze/_impl/_mz_hamiltonian_impl.scad +++ b/src/maze/_impl/_mz_hamiltonian_impl.scad @@ -11,55 +11,52 @@ function updated(x, y, dots) = else dots[r] ]; -function _mz_hamiltonian_top(x, y) = - let( - nx = x * 2, - ny = y * 2 - ) - [[nx, ny + 2], [nx + 1, ny + 2], [nx + 2, ny + 2]]; +function _top(x, y) = + let(nx = x * 2, ny_2 = y * 2 + 2) + [for(i = [0:2]) [nx + i, ny_2]]; -function _mz_hamiltonian_right(x, y) = - let( - nx = x * 2, - ny = y * 2 - ) - [[nx + 2, ny + 2], [nx + 2, ny + 1], [nx + 2, ny]]; +function _right(x, y) = + let(nx_2 = x * 2 + 2, ny = y * 2) + [for(i = [2, 1, 0]) [nx_2, ny + i]]; -function _mz_hamiltonian_top_right(x, y) = +function _top_right(x, y) = let( nx = x * 2, - ny = y * 2 + ny = y * 2, + nx_2 = nx + 2, + ny_2 = ny + 2 ) - [[nx, ny + 2], [nx + 1, ny + 2], [nx + 2, ny + 2], [nx + 2, ny + 1], [nx + 2, ny]]; + [[nx, ny_2], [nx + 1, ny_2], [nx_2, ny_2], [nx_2, ny + 1], [nx_2, ny]]; -function _mz_hamiltonian_corner_value(dotM, x, y) = +function _corner_value(dotM, x, y) = let( - c1 = dotM[y][x] ? 1 : 0, - c2 = dotM[y + 1][x] ? 2 : 0, - c3 = dotM[y + 1][x + 1] ? 4 : 0, - c4 = dotM[y][x + 1] ? 8 : 0 + dotMy = dotM[y], + dotMy1 = dotM[y + 1], + x_1 = x + 1, + c1 = dotMy[x] ? 1 : 0, + c2 = dotMy1[x] ? 2 : 0, + c3 = dotMy1[x_1] ? 4 : 0, + c4 = dotMy[x_1] ? 8 : 0 ) c1 + c2 + c3 + c4; -_mz_hamiltonian_dir_table = [ +_dir_table = [ [4, 0], [12, 0], [13, 0], // UP [1, 1], [3, 1], [7, 1], // DOWN [2, 2], [6, 2], [14, 2], // LEFT [8, 3], [9, 3], [11, 3] // RIGHT ]; -function _mz_hamiltonian_dir(cr_value) = - lookup(cr_value, _mz_hamiltonian_dir_table); -_mz_hamiltonian_nxt_offset = [ +_nxt_offset = [ [0, 1], // UP [0, -1], // DOWN [-1, 0], // LEFT [1, 0] // RIGHT ]; -function nxtp(dotM, p) = p + _mz_hamiltonian_nxt_offset[_mz_hamiltonian_dir(_mz_hamiltonian_corner_value(dotM, p.x, p.y))]; +function nxtp(dotM, p) = p + _nxt_offset[lookup(_corner_value(dotM, p.x, p.y), _dir_table)]; -function _mz_hamiltonian_travel(dotM, p, leng) = +function _travel(dotM, p, leng) = let( end = leng - 1, pts = [ diff --git a/src/maze/mz_hamiltonian.scad b/src/maze/mz_hamiltonian.scad index f62cde4d..91599603 100644 --- a/src/maze/mz_hamiltonian.scad +++ b/src/maze/mz_hamiltonian.scad @@ -32,9 +32,9 @@ function mz_hamiltonian(rows, columns, start = [0, 0], init_cells, seed) = [ for(row = cells, cell = row) let(type = mz_square_get(cell, "t")) - each if(type == "TOP_WALL") _mz_hamiltonian_top(cell.x, cell.y) else - if(type == "RIGHT_WALL") _mz_hamiltonian_right(cell.x, cell.y) else - if(type == "TOP_RIGHT_WALL" || type == "MASK") _mz_hamiltonian_top_right(cell.x, cell.y) + each if(type == "TOP_WALL") _top(cell.x, cell.y) else + if(type == "RIGHT_WALL") _right(cell.x, cell.y) else + if(type == "TOP_RIGHT_WALL" || type == "MASK") _top_right(cell.x, cell.y) ], [for(x = [0:c * 2 - 1]) [x, 0]], [for(y = [0:r * 2 - 1]) [0, y]] @@ -53,4 +53,4 @@ function mz_hamiltonian(rows, columns, start = [0, 0], init_cells, seed) = path_leng = init_cells_undef ? r * c : len([for(row = init_cells, cell = row) if(mz_square_get(cell, "t") != "MASK") undef]) ) - _mz_hamiltonian_travel(dotM, start * 2, path_leng * 4); \ No newline at end of file + _travel(dotM, start * 2, path_leng * 4); \ No newline at end of file