How to change the visualization from lines to a patch

1 view (last 30 days)
I have a robot maze that is in 2d and im trying to make it 3d. Currently the walls are lines and I need to turn them to patches but i'm not sure how to do this?
function [maze_figure, maze_axes] = drawMaze3d(maze)
% prepare figure and axes
maze_figure = figure;
maze_axes = axes;
axis equal;
hold on;
title('this will become the 3d maze')
% set axis properties depending on maze size
axis([0 maze.number_of_columns 0 maze.number_of_rows+2]);
set(gca, 'xlim', [0 maze.number_of_columns], 'ylim', [2 maze.number_of_rows+2], 'xtick', [], 'ytick', []);
set(gca, 'xtick', [], 'ytick', [], 'ztick', []);
view(30, 30)
% ----------------------------------------------------------------------------------------------------------------------
% remove this line and specify camera properties of the maze_axes here for (b)
% ----------------------------------------------------------------------------------------------------------------------
% draw the grid
for i_col = 1:maze.number_of_columns
for i_row = 1:maze.number_of_rows
% draw the northern wall
if maze.hasWall(i_row, i_col, 1)
drawWallNorth(maze, i_row, i_col);
end
% draw the eastern wall
if maze.hasWall(i_row, i_col, 2)
drawWallEast(maze, i_row, i_col);
end
% draw the southern wall
if maze.hasWall(i_row, i_col, 3)
drawWallSouth(maze, i_row, i_col);
end
% draw the western wall
if maze.hasWall(i_row, i_col, 4)
drawWallWest(maze, i_row, i_col);
end
end
end
end
% nested functions
function drawWallNorth(maze, row, column)
x1 = column-1;
x2 = column;
y1 = maze.number_of_rows - row + 3;
y2 = maze.number_of_rows - row + 3;
plot([x1, x2], [y1, y2], 'k', 'linewidth', 4);
% ----------------------------------------------------------------------------------------------------------------------
% add a 3d patch here for (a)
% ----------------------------------------------------------------------------------------------------------------------
end
function drawWallEast(maze, row, column)
x1 = column;
x2 = column;
y1 = maze.number_of_rows - row + 3;
y2 = maze.number_of_rows - row + 2;
plot([x1, x2], [y1, y2], 'k', 'linewidth', 4);
% ----------------------------------------------------------------------------------------------------------------------
% add a 3d patch here for (a)
% ----------------------------------------------------------------------------------------------------------------------
end
function drawWallSouth(maze, row, column)
x1 = column-1;
x2 = column;
y1 = maze.number_of_rows - row + 2;
y2 = maze.number_of_rows - row + 2;
plot([x1, x2], [y1, y2], 'k', 'linewidth', 4);
% ----------------------------------------------------------------------------------------------------------------------
% add a 3d patch here for (a)
% ----------------------------------------------------------------------------------------------------------------------
end
function drawWallWest(maze, row, column)
x1 = column-1;
x2 = column-1;
y1 = maze.number_of_rows - row + 3;
y2 = maze.number_of_rows - row + 2;
plot([x1, x2], [y1, y2], 'k', 'linewidth', 4);
% ----------------------------------------------------------------------------------------------------------------------
% add a 3d patch here for (a)
% ----------------------------------------------------------------------------------------------------------------------
end

Accepted Answer

William Rose
William Rose on 2 Dec 2022
To help me create the maze, I made the image below. It could be helpful to future maze makers.
Then I wrote a main program to create a maze and call the plotting function.
%my3dMaze.m W. Rose 2022-12-01
%MY3DMAZE Script to test function drawMaze3d.m.
%
%maze.hasWall = NxMx4 array specifying N,E,S,W edges of each maze cell.
clear
% Define maze object
maze1.number_of_rows=3; maze1.number_of_columns=3;
maze1.hasWall=ones(maze1.number_of_rows,maze1.number_of_columns,4);
maze1.hasWall(:,:,1)=[1,1,1;0,1,0;0,0,0]; %north walls
maze1.hasWall(:,:,2)=[0,0,1;0,1,1;0,0,0]; %east walls
maze1.hasWall(:,:,3)=[0,0,0;0,1,0;1,1,1]; %south walls
maze1.hasWall(:,:,4)=[0,0,0;1,1,0;1,0,0]; %west walls
%Draw maze
drawMaze3d(maze1);
It makes the figure below.
The modified plotting function calls patch() instead of plot(). It is attached.
  3 Comments
William Rose
William Rose on 2 Dec 2022
The .m file I attached above is a modified version of your function. Compare it to your original function to see what I changed. They key change is to replace
plot([x1, x2], [y1, y2], 'k', 'linewidth', 4);
with
patch([x1,x2,x2,x1],[y1,y2,y2,y1],[0,0,1,1],'m');
inside the subfunctions drawWallNorth(), drawWallSouth(), etc.
The patch command above draws one wall panel: a rectangle whose top and bottom edges extend from (x1,y1) to (x2,y2), and whose top and bottom edges are at z=1 and z=0.
I also added a patch command to draw a gray floor at level z=0:
patch([0,maze.number_of_columns,maze.number_of_columns,0],...
[2,2,maze.number_of_columns+2,maze.number_of_columns+2],...
[0,0,0,0],[.7,.7,.7]);

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by