How to plot a 3D cube based if i have the coordinates of the 8 surrounding nodes?

Hello community,
I want to plot a 3d cube based on the coordinates of my geometry (8 nodes). The coordinates of my cube are:
coord=[0 0 0;
0.5 0 0;
0.5 0.5 0;
0 0.5 0;
0 0 0.5;
0.5 0 0.5;
0.5 0.5 0.5;
0 0.5 0.5;];
Thanks a lot in advance.

 Réponse acceptée

To expand on Star Strider's answer, in your example, you've specified a list of coordinates, but you haven't told Matlab how they should be connected. Based on your original example, the following array of row indices defines the faces of your cube:
coord = [...
0 0 0;
0.5 0 0;
0.5 0.5 0;
0 0.5 0;
0 0 0.5;
0.5 0 0.5;
0.5 0.5 0.5;
0 0.5 0.5;];
idx = [4 8 5 1 4; 1 5 6 2 1; 2 6 7 3 2; 3 7 8 4 3; 5 8 7 6 5; 1 4 3 2 1]';
To plot, substitute the coordinates:
xc = coord(:,1);
yc = coord(:,2);
zc = coord(:,3);
ax(1) = subplot(2,1,1);
patch(xc(idx), yc(idx), zc(idx), 'r', 'facealpha', 0.1);
view(3);
% Deformed
coord2 = coord + rand(size(coord))*0.1;
xc = coord2(:,1);
yc = coord2(:,2);
zc = coord2(:,3);
ax(2) = subplot(2,1,2);
patch(xc(idx), yc(idx), zc(idx), 'r', 'facealpha', 0.1);
view(3);

8 commentaires

That's amazing @Kelly Kearney !! Thanks a lot for your answer ! That is exactly what i was looking for!
You are absolutely right, i needed the connectivity matrix!
Could you please explain to me how you use patch?
Multi-faceted patches can be defined a few different ways. The most straightforward is by defining the x, y, and z coordinates as arrays where each column describes a different face (i.e. a polygon). That's the syntax I used above.
idx = [4 8 5 1 4; 1 5 6 2 1; 2 6 7 3 2; 3 7 8 4 3; 5 8 7 6 5; 1 4 3 2 1]'
idx = 5×6
4 1 2 3 5 1 8 5 6 7 8 4 5 6 7 8 7 3 1 2 3 4 6 2 4 1 2 3 5 1
idx says to create a polygon connecting vertices 4-8-5-1-4, then one connecting 1-5-6-2-1, etc., with the coordinates for those vertices defined by the rows of coord. By indexing into each of the columns of coord, you get a set of x/y/z coordinates for each face with the same ordering.
Alternatively, you can use face/vertex input, where you define the coordinates of the vertices and the connectivity of the faces separately. This allows you to skip the step of expanding the x/y/z coordinate matrices yourself. In this example, that would look like:
patch('vertices', coord, 'faces', idx', 'facecolor', 'r', 'facealpha', 0.1)
(For some reason, in face/vertex mode, the rows of the face input array define each face, as opposed to the columns in x/y/z input, hence the transposition of idx). I actually prefer this syntax because it's more compact, but it can be more confusing for new users.
Thank you so much for all your answers @Kelly Kearney !!! They were so helpful !!! Very good lesson on 3d plotting!!!
@Kelly Kearney Could you please help me with one more question related to the topic?
I plotted my cubes, original and deformed but as the deformations are tiny this is not visible with the existing script. Could you help make these difference of the shapes visible?
My coordinates are:
coord = [...
0 0 0;
0.5 0 0;
0.5 0.5 0;
0 0.5 0;
0 0 0.5;
0.5 0 0.5;
0.5 0.5 0.5;
0 0.5 0.5;];
and updated:
0 0 0
0.500001990189944 0 0
0.500001990189944 0.500001990189944 0
0 0.500001990189944 0
0 0 0.499993366033520
0.500001990189944 0 0.499993366033520
0.500001990189944 0.500001990189944 0.499993366033520
0 0.500001990189944 0.499993366033520
And lastly, can i also make the axes of the two subplots to be the same for comparison purposes?
Thanks a lot again @Kelly Kearney
Those numbers indicate that your deformations are about 6 orders of magnitude smaller than the cube edge lengths... so yeah, that won't be visible to the naked eye. If you just want to use this as a visual aid, then I suppose you could scale up the deformations, although then you lose the "real" aspect ratio of the cube. To sync the axes, just set the axis limits appropriately:
% Original cube
coord = [...
0 0 0;
0.5 0 0;
0.5 0.5 0;
0 0.5 0;
0 0 0.5;
0.5 0 0.5;
0.5 0.5 0.5;
0 0.5 0.5;];
idx = [4 8 5 1 4; 1 5 6 2 1; 2 6 7 3 2; 3 7 8 4 3; 5 8 7 6 5; 1 4 3 2 1]';
% Deformed cube
coord2 = [...
0 0 0
0.500001990189944 0 0
0.500001990189944 0.500001990189944 0
0 0.500001990189944 0
0 0 0.499993366033520
0.500001990189944 0 0.499993366033520
0.500001990189944 0.500001990189944 0.499993366033520
0 0.500001990189944 0.499993366033520];
% Deformation
dc = coord - coord2;
% Plot
ax(1) = subplot(1,2,1);
patch('vertices', coord, 'faces', idx', 'facecolor', 'r', 'facealpha', 0.1);
view(3);
ax(2) = subplot(1,2,2);
patch('vertices', coord+dc*1e5, 'faces', idx', 'facecolor', 'r', 'facealpha', 0.1);
view(3);
lim = [-0.1 0.6];
set(ax, 'xlim', [-0.1 0.6], ...
'ylim', [-0.1 0.6], ...
'zlim', [-0.1 1.2], ...
'dataaspectratio', [1 1 1]); % last one same as "axis equal"
how if those coordinate data from excel?
how to write idx? I am kind of new. please help me

Connectez-vous pour commenter.

Plus de réponses (1)

Another route using the 'surf' function:
x = 0.5*[ 1 1 1 1 1; 1 1 -1 -1 1;1 1 -1 -1 1;1 1 1 1 1];
y = 0.5*[ 1 -1 -1 1 1; 1 -1 -1 1 1;1 -1 -1 1 1;1 -1 -1 1 1];
z = 0.5*[-1 -1 -1 -1 -1;-1 -1 -1 -1 -1;1 1 1 1 1;1 1 1 1 1];
% Plow
fig = figure("Name","cube");
surf(x,y,z,'FaceColor','g')
set(fig.CurrentAxes,"XLim",[-1 1],"YLim",[-1 1],"ZLim",[-1 1]);
xlabel("x")
ylabel("y")
zlabel("z")

Community Treasure Hunt

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

Start Hunting!

Translated by