How to change the visualization from lines to a patch
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
William Rose
2022-12-2
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 个评论
William Rose
2022-12-2
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]);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Labyrinth problems 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!