MATLAB Answers

How to plot a triangular prism ?

44 views (last 30 days)
Marian  Jose
Marian Jose on 23 Oct 2016
Commented: Marian Jose on 25 Oct 2016
Hi, Guys I just started using Matlab and I would like to how to plot 3d figure for co-ordinates shown in the image which happens to be a triangular prism. The co-ordinates of point c are (-1,3,2). I tried using PATCH command and got two funny triangles. I have mentioned the code I used below. I am really new to Matlab so if you could please explain your answer for a noobie it would much be appreciated. thanks in advance
X = [1 -1 1 -1 1 -1]
Y = [3 3 5 5 3 3]
Z = [0 0 2 2 2 2]
  1 Comment
Chaya N
Chaya N on 23 Oct 2016
I think your figure is drawn wrong! Please check points E and F (the z co-ordinates in particular).

Sign in to comment.

Accepted Answer

Chaya N
Chaya N on 23 Oct 2016
Hello Marian, this is how your prism should look
And now for the procedure:
The patch function that you are using considers the Z co-ordinates as color data. This is why you get the two triangles. A better way to achieve your goal would be to instead provide your inputs in the form of Vertices and Faces.
To get vertices, you just rearrange data from X, Y and Z
v = [1 3 0; -1 3 0; 1 5 2; -1 5 2; 1 3 2; -1 3 2]; % Points A, B, E, F, D and C respectively
To get the faces, consider them as being defined by their vertices. This prism has 5 faces, 2 of them defined by 3 vertices each (ADE and BFC) and the remaining 3 defined by 4 vertices each (ABFE, EFCD and ABCD). You have to choose from these two sets such that all faces have equal number of vertices and they include all of the edges among them. This can now be done by choosing the set of three four-sided faces. When defining these in code, it is important to maintain the order of the vertices or else you would get a very funky looking graph. The first vertex in vector v is point 1, second is point 2 and so on and so forth.
f = [1 2 4 3; 3 4 6 5; 1 2 6 5]; % For ABFE, EFCD and ABCD respectively
With these two arrays decided, you just need a few more details.
1. Your prism is going to have to be transparent or you will not see the edges and general shape / details. For this, we define a patch property called FaceAlpha and set it to 0 for transparent.
2. In Matlab, the axes for the graph are set to particular directions and we need to modify them to reflect those of your figure. We also modify the limits on the axes for visual clarity. To do this, we call up axes properties and set the limits (XLim, YLim, ZLim) and directions (XDir, YDir) on the axes.
figure; % Call up an empty figure window
axes('XLim',[-2 2],'YLim',[0 6],'ZLim',[0 3],'XDir','reverse','YDir','reverse'); % Set axes properties
view(3); % For a 3D view of your graph
patch('Faces',f,'Vertices',v,'FaceAlpha',0); % plotting the prism with face and vertex data
grid on; % draw a grid
This will produce your required 3D plot like the one above.
This is about as thorough an explanation as I am able to provide. If I have missed anything or if you require any clarifications, please post as a comment.
Good Luck!

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by