mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-08-08 15:56:42 +02:00
performance improved
This commit is contained in:
@@ -1,9 +1,7 @@
|
|||||||
use <_mz_square_comm.scad>;
|
use <_mz_square_comm.scad>;
|
||||||
|
|
||||||
function eqPos(x, y, cell) = cell.x == x && cell.y == y;
|
|
||||||
|
|
||||||
// is (x, y) visited?
|
// is (x, y) visited?
|
||||||
function visited(x, y, cells, columns) = cells[y * columns + x][3];
|
function visited(x, y, cells, columns) = cells[y][x][3];
|
||||||
|
|
||||||
// is (x, y) visitable?
|
// is (x, y) visitable?
|
||||||
function visitable(x, y, cells, rows, columns) =
|
function visitable(x, y, cells, rows, columns) =
|
||||||
@@ -12,9 +10,12 @@ function visitable(x, y, cells, rows, columns) =
|
|||||||
!visited(x, y, cells, columns); // unvisited
|
!visited(x, y, cells, columns); // unvisited
|
||||||
|
|
||||||
// setting (x, y) as being visited
|
// setting (x, y) as being visited
|
||||||
function set_visited(x, y, cells) = [
|
function set_visited(x, y, cells) =
|
||||||
for(cell = cells)
|
let(rowY = [for(cell = cells[y]) if(cell.x == x) [x, y, get_type(cell), true] else cell])
|
||||||
eqPos(x, y, cell) ? [x, y, get_type(cell), true] : cell
|
[
|
||||||
|
for(r = [0:len(cells) - 1])
|
||||||
|
if(r == y) rowY
|
||||||
|
else cells[r]
|
||||||
];
|
];
|
||||||
|
|
||||||
// 0(right), 1(top), 2(left), 3(bottom)
|
// 0(right), 1(top), 2(left), 3(bottom)
|
||||||
@@ -61,34 +62,68 @@ function next_y(y, dir, rows, wrapping) =
|
|||||||
wrapping ? (ny < 0 ? ny + rows : ny % rows) : ny;
|
wrapping ? (ny < 0 ? ny + rows : ny % rows) : ny;
|
||||||
|
|
||||||
// go right and carve the right wall
|
// go right and carve the right wall
|
||||||
function carve_right(x, y, cells) = [
|
function carve_right(x, y, cells) =
|
||||||
for(cell = cells)
|
let(
|
||||||
!eqPos(x, y, cell) ? cell :
|
rowY = [
|
||||||
top_right_wall(cell) ? [x, y, 1, true] : [x, y, 0, true]
|
for(cell = cells[y])
|
||||||
|
if(cell.x != x) cell
|
||||||
|
else (top_right_wall(cell) ? [x, y, 1, true] : [x, y, 0, true])
|
||||||
|
]
|
||||||
|
)
|
||||||
|
[
|
||||||
|
for(r = [0:len(cells) - 1])
|
||||||
|
if(r == y) rowY
|
||||||
|
else cells[r]
|
||||||
];
|
];
|
||||||
|
|
||||||
// go up and carve the top wall
|
// go up and carve the top wall
|
||||||
function carve_top(x, y, cells) = [
|
function carve_top(x, y, cells) =
|
||||||
for(cell = cells)
|
let(
|
||||||
!eqPos(x, y, cell) ? cell :
|
rowY = [
|
||||||
top_right_wall(cell) ? [x, y, 2, true] : [x, y, 0, true]
|
for(cell = cells[y])
|
||||||
|
if(cell.x != x) cell
|
||||||
|
else (top_right_wall(cell) ? [x, y, 2, true] : [x, y, 0, true])
|
||||||
|
]
|
||||||
|
)
|
||||||
|
[
|
||||||
|
for(r = [0:len(cells) - 1])
|
||||||
|
if(r == y) rowY
|
||||||
|
else cells[r]
|
||||||
];
|
];
|
||||||
|
|
||||||
// go left and carve the right wall of the left cell
|
// go left and carve the right wall of the left cell
|
||||||
function carve_left(x, y, cells, columns) =
|
function carve_left(x, y, cells, columns) =
|
||||||
let(
|
let(
|
||||||
x_minus_one = x - 1,
|
x_minus_one = x - 1,
|
||||||
nx = x_minus_one < 0 ? x_minus_one + columns : x_minus_one
|
nx = x_minus_one < 0 ? x_minus_one + columns : x_minus_one,
|
||||||
|
rowY = [
|
||||||
|
for(cell = cells[y])
|
||||||
|
if(cell.x != nx) cell
|
||||||
|
else [nx, y, 1, false]
|
||||||
|
]
|
||||||
)
|
)
|
||||||
[for(cell = cells) eqPos(nx, y, cell) ? [nx, y, 1, 0] : cell];
|
[
|
||||||
|
for(r = [0:len(cells) - 1])
|
||||||
|
if(r == y) rowY
|
||||||
|
else cells[r]
|
||||||
|
];
|
||||||
|
|
||||||
// go down and carve the top wall of the bottom cell
|
// go down and carve the top wall of the bottom cell
|
||||||
function carve_bottom(x, y, cells, rows) =
|
function carve_bottom(x, y, cells, rows) =
|
||||||
let(
|
let(
|
||||||
y_minus_one = y - 1,
|
y_minus_one = y - 1,
|
||||||
ny = y_minus_one < 0 ? y_minus_one + rows : y_minus_one
|
ny = y_minus_one < 0 ? y_minus_one + rows : y_minus_one,
|
||||||
|
rowNY = [
|
||||||
|
for(cell = cells[ny])
|
||||||
|
if(cell.x != x) cell
|
||||||
|
else [x, ny, 2, false]
|
||||||
|
]
|
||||||
)
|
)
|
||||||
[for(cell = cells) eqPos(x, ny, cell) ? [x, ny, 2, 0] : cell];
|
[
|
||||||
|
for(r = [0:len(cells) - 1])
|
||||||
|
if(r == ny) rowNY
|
||||||
|
else cells[r]
|
||||||
|
];
|
||||||
|
|
||||||
// 0(right), 1(top), 2(left), 3(bottom)
|
// 0(right), 1(top), 2(left), 3(bottom)
|
||||||
function carve(dir, x, y, cells, rows, columns) =
|
function carve(dir, x, y, cells, rows, columns) =
|
||||||
@@ -97,7 +132,6 @@ function carve(dir, x, y, cells, rows, columns) =
|
|||||||
dir == 2 ? carve_left(x, y, cells, columns) :
|
dir == 2 ? carve_left(x, y, cells, columns) :
|
||||||
/*dir 3*/ carve_bottom(x, y, cells, rows);
|
/*dir 3*/ carve_bottom(x, y, cells, rows);
|
||||||
|
|
||||||
|
|
||||||
// find out visitable dirs from (x, y)
|
// find out visitable dirs from (x, y)
|
||||||
function visitable_dirs(r_dirs, x, y, cells, rows, columns, x_wrapping, y_wrapping) = [
|
function visitable_dirs(r_dirs, x, y, cells, rows, columns, x_wrapping, y_wrapping) = [
|
||||||
for(dir = r_dirs)
|
for(dir = r_dirs)
|
||||||
|
@@ -2,7 +2,9 @@ use <_mz_square_comm.scad>;
|
|||||||
|
|
||||||
// create a starting maze for being visited later.
|
// create a starting maze for being visited later.
|
||||||
function _rc_maze(rows, columns) = [
|
function _rc_maze(rows, columns) = [
|
||||||
for(y = [0:rows - 1], x = [0:columns - 1])
|
for(y = [0:rows - 1])
|
||||||
|
[
|
||||||
|
for(x = [0:columns - 1])
|
||||||
cell(
|
cell(
|
||||||
x, y,
|
x, y,
|
||||||
// all cells have top and right walls
|
// all cells have top and right walls
|
||||||
@@ -10,6 +12,7 @@ function _rc_maze(rows, columns) = [
|
|||||||
// unvisited
|
// unvisited
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
function _mz_mask(mask) =
|
function _mz_mask(mask) =
|
||||||
@@ -18,7 +21,8 @@ function _mz_mask(mask) =
|
|||||||
columns = len(mask[0])
|
columns = len(mask[0])
|
||||||
)
|
)
|
||||||
[
|
[
|
||||||
for(y = [0:rows - 1], x = [0:columns - 1])
|
for(y = [0:rows - 1]) [
|
||||||
|
for(x = [0:columns - 1])
|
||||||
mask[rows - y - 1][x] == 0 ?
|
mask[rows - y - 1][x] == 0 ?
|
||||||
cell(
|
cell(
|
||||||
x, y,
|
x, y,
|
||||||
@@ -32,4 +36,5 @@ function _mz_mask(mask) =
|
|||||||
3, // unvisited
|
3, // unvisited
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
]
|
||||||
];
|
];
|
@@ -16,10 +16,11 @@ function mz_square_cells(rows, columns, start = [0, 0], init_cells, x_wrapping =
|
|||||||
let(
|
let(
|
||||||
init_undef = is_undef(init_cells),
|
init_undef = is_undef(init_cells),
|
||||||
mz = init_undef ? mz_square_initialize(rows, columns) : init_cells,
|
mz = init_undef ? mz_square_initialize(rows, columns) : init_cells,
|
||||||
c = find_index(mz, function(cell) cell.y != 0),
|
r = len(mz),
|
||||||
r = len(mz) / c
|
c = len(mz[0]),
|
||||||
)
|
// c = find_index(mz, function(cell) cell.y != 0),
|
||||||
go_maze(
|
// r = len(mz) / c
|
||||||
|
generated = go_maze(
|
||||||
start.x,
|
start.x,
|
||||||
start.y,
|
start.y,
|
||||||
mz,
|
mz,
|
||||||
@@ -27,4 +28,6 @@ function mz_square_cells(rows, columns, start = [0, 0], init_cells, x_wrapping =
|
|||||||
x_wrapping,
|
x_wrapping,
|
||||||
y_wrapping,
|
y_wrapping,
|
||||||
seed
|
seed
|
||||||
);
|
)
|
||||||
|
)
|
||||||
|
[for(row = generated) each row];
|
||||||
|
Reference in New Issue
Block a user