mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-08-12 09:44:16 +02:00
refactor
This commit is contained in:
@@ -1,26 +1,20 @@
|
|||||||
use <_mz_square_comm.scad>;
|
use <_mz_square_comm.scad>;
|
||||||
|
|
||||||
// find out the index of a cell with the position (x, y)
|
function eqPos(x, y, cell) = get_x(cell) == x && get_y(cell) == y;
|
||||||
function indexOf(x, y, cells, i = 0) =
|
|
||||||
i == len(cells) ? -1 : (
|
|
||||||
[get_x(cells[i]), get_y(cells[i])] == [x, y] ? i :
|
|
||||||
indexOf(x, y, cells, i + 1)
|
|
||||||
);
|
|
||||||
|
|
||||||
// is (x, y) visited?
|
// is (x, y) visited?
|
||||||
function visited(x, y, cells) = cells[indexOf(x, y, cells)][3];
|
function visited(x, y, cells, rows) = cells[y * rows + x][3];
|
||||||
|
|
||||||
// is (x, y) visitable?
|
// is (x, y) visitable?
|
||||||
function visitable(x, y, cells, rows, columns) =
|
function visitable(x, y, cells, rows, columns) =
|
||||||
y >= 0 && y < rows && // y bound
|
y >= 0 && y < rows && // y bound
|
||||||
x >= 0 && x < columns && // x bound
|
x >= 0 && x < columns && // x bound
|
||||||
!visited(x, y, cells); // unvisited
|
!visited(x, y, cells, rows); // 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)
|
for(cell = cells)
|
||||||
[x, y] == [get_x(cell), get_y(cell)] ?
|
eqPos(x, y, cell) ? [x, y, get_type(cell), true] : cell
|
||||||
[x, y, get_type(cell), true] : cell
|
|
||||||
];
|
];
|
||||||
|
|
||||||
// 0(right), 1(top), 2(left), 3(bottom)
|
// 0(right), 1(top), 2(left), 3(bottom)
|
||||||
@@ -68,16 +62,16 @@ function next_y(y, dir, rows, wrapping) =
|
|||||||
|
|
||||||
// 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) [get_x(cell), get_y(cell)] == [x, y] ? (
|
for(cell = cells)
|
||||||
top_right_wall(cell) ? [x, y, 1, 1] : [x, y, 0, 1]
|
!eqPos(x, y, cell) ? cell :
|
||||||
) : cell
|
top_right_wall(cell) ? [x, y, 1, true] : [x, y, 0, true]
|
||||||
];
|
];
|
||||||
|
|
||||||
// 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) [get_x(cell), get_y(cell)] == [x, y] ? (
|
for(cell = cells)
|
||||||
top_right_wall(cell) ? [x, y, 2, 1] : [x, y, 0, 1]
|
!eqPos(x, y, cell) ? cell :
|
||||||
) : cell
|
top_right_wall(cell) ? [x, y, 2, true] : [x, y, 0, true]
|
||||||
];
|
];
|
||||||
|
|
||||||
// go left and carve the right wall of the left cell
|
// go left and carve the right wall of the left cell
|
||||||
@@ -94,7 +88,7 @@ function carve_bottom(x, y, cells, rows) =
|
|||||||
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
|
||||||
)
|
)
|
||||||
[for(cell = cells) [get_x(cell), get_y(cell)] == [x, ny] ? [x, ny, 2, 0] : cell];
|
[for(cell = cells) eqPos(x, ny, cell) ? [x, ny, 2, 0] : cell];
|
||||||
|
|
||||||
// 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) =
|
||||||
@@ -107,8 +101,8 @@ function carve(dir, x, y, cells, rows, columns) =
|
|||||||
// 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)
|
||||||
if(visitable(next_x(x, dir, columns, x_wrapping), next_y(y, dir, rows, y_wrapping), cells, rows, columns))
|
if(visitable(next_x(x, dir, columns, x_wrapping), next_y(y, dir, rows, y_wrapping), cells, rows, columns))
|
||||||
dir
|
dir
|
||||||
];
|
];
|
||||||
|
|
||||||
// go maze from (x, y)
|
// go maze from (x, y)
|
||||||
|
Reference in New Issue
Block a user