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

568 vues (au cours des 30 derniers jours)
Dimitris K le 3 Nov 2021
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;];
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Kelly Kearney le 4 Nov 2021
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);
##### 7 commentairesAfficher 5 commentaires plus anciensMasquer 5 commentaires plus anciens
Kelly Kearney le 5 Nov 2021
Modifié(e) : Kelly Kearney le 5 Nov 2021
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"
Simson Hutagalung le 24 Juin 2022
how if those coordinate data from excel?

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Surface and Mesh Plots dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by