%Copyright 2006, Adarsh Krishnamurthy (me.berkeley.edu/~adarsh) and Athulan Vijayaraghavan (me.berkeley.edu/~athulan) clc; clear all; %recursive backtracker - 3d tic %size of the maze - only square n = 5; n_cells = n*n; %listing all the cells with a uid in an array ctr = 1; for i = 1:n for j = 1:n cells_face_1(ctr, :) = [1 i j i-1 j-1 n i-1 j n i j n i j-1 n]; cells_face_2(ctr, :) = [2 i j i-1 0 j-1 i-1 0 j i 0 j i 0 j-1]; cells_face_3(ctr, :) = [3 i j i-1 n-j+1 0 i-1 n-j 0 i n-j 0 i n-j+1 0]; cells_face_4(ctr, :) = [4 i j i-1 n n-j+1 i-1 n n-j i n n-j i n n-j+1]; cells_face_5(ctr, :) = [5 i j 0 j-1 i-1 0 j-1 i 0 j i 0 j i-1]; cells_face_6(ctr, :) = [6 i j n j-1 n-i+1 n j-1 n-i n j n-i n j n-i+1]; ctr = ctr + 1; end end cells = [cells_face_1; cells_face_2; cells_face_3; cells_face_4; cells_face_5; cells_face_6]; %listing all the edges in an array edges = []; k = 1; for i = 1:n for j = 1:n+1 edges_face1(k, :) = [i-1 j-1 0 i j-1 0]; edges_face1(k+1, :) = [j-1 i-1 0 j-1 i 0]; edges_face2(k, :) = [i-1 j-1 n i j-1 n]; edges_face2(k+1, :) = [j-1 i-1 n j-1 i n]; edges_face3(k, :) = [0 i-1 j-1 0 i j-1]; edges_face3(k+1, :) = [0 j-1 i-1 0 j-1 i]; edges_face4(k, :) = [n i-1 j-1 n i j-1]; edges_face4(k+1, :) = [n j-1 i-1 n j-1 i]; edges_face5(k, :) = [i-1 0 j-1 i 0 j-1]; edges_face5(k+1, :) = [j-1 0 i-1 j-1 0 i]; edges_face6(k, :) = [i-1 n j-1 i n j-1]; edges_face6(k+1, :) = [j-1 n i-1 j-1 n i]; k = k + 2; end end edges = [edges_face1; edges_face2; edges_face3; edges_face4; edges_face5; edges_face6]; edges = unique(edges, 'rows'); %initialize the cell stack stack = []; %choose a random cell to begin with r = ceil(length(cells)*(rand)); stack(1, :) = cells(r, 1:3); visited = []; ctr = 1; while length(stack)>0 ctr; current_cell = stack(end, :); %set the current cell visited(end+1, :) = current_cell; %add current cell to the stack current_neighbors = neighbors3d(current_cell, n); %find the neighbors current_neighbors = setdiff(current_neighbors, visited, 'rows'); %remove those visited if size(current_neighbors, 1)==0 stack(end, :) = []; elseif size(current_neighbors, 1)>0 r = ceil(size(current_neighbors, 1)*(rand)); chosen_neighbor = current_neighbors(r, :); stack(end+1, :) = chosen_neighbor; end ctr = ctr + 1; end ctr for j = 1:length(visited)-1 j %pull up first cell [p i r] = intersect(cells(:, 1:3), visited(j, :) ,'rows'); pts_1 = [cells(i,4) cells(i,5) cells(i,6); cells(i,7) cells(i,8) cells(i,9); cells(i,10) cells(i,11) cells(i,12); cells(i,13) cells(i,14) cells(i,15)]; [p i r] = intersect(cells(:, 1:3), visited(j+1, :) ,'rows'); pts_2 = [cells(i,4) cells(i,5) cells(i,6); cells(i,7) cells(i,8) cells(i,9); cells(i,10) cells(i,11) cells(i,12); cells(i,13) cells(i,14) cells(i,15)]; common_edge = intersect(pts_1, pts_2, 'rows'); common_edgeA = [common_edge(1, :) common_edge(2, :)]; common_edgeB = [common_edge(2, :) common_edge(1, :)]; for k = 1:length(edges) if edges(k, :) == common_edgeA edges(k, :) = [0 0 0 0 0 0]; end if edges(k, :) == common_edgeB edges(k, :) = [0 0 0 0 0 0]; end end end figure(2); figure(2); axis square; hold on; for i = 1:length(edges) x = [edges(i, 1) edges(i, 4)]; y = [edges(i, 2) edges(i, 5)]; z = [edges(i, 3) edges(i, 6)]; plot3(x, y, z); h = plot3(x, y, z); set(h, 'LineWidth', 4); end fill3([0 0 n n], [0 n n 0], [0 0 0 0], 'y'); fill3([0 0 n n], [0 n n 0], [n n n n], 'y'); fill3([0 0 n n], [0 0 0 0], [0 n n 0], 'y'); fill3([0 0 n n], [n n n n], [0 n n 0], 'y'); fill3([0 0 0 0], [0 0 n n], [0 n n 0], 'y'); fill3([n n n n], [0 0 n n], [0 n n 0], 'y'); %show_maze(n, edges); toc