Hi all,
I was wondering how I can plot a 3D-shape of a cube. I have tried https://in.mathworks.com/matlabcentral/fileexchange/15161-plotcube but this give me a 2D-array, and I cannot seem to get a 3D-array out of it. I have the following code:
array_width=101;%must be odd
outer_cube=create_cube(array_width);
figure; cube=isosurface(outer_cube,0);
patch(cube,'FaceColor',[0 0 .7],'EdgeColor',[0 0 1]);
view(45,45); axis equal; title('Simulation of solid cube');
function cube=create_cube(array_width)
a=(array_width-1)/2;
a=linspace(-a,a,array_width);
[X,Y,Z]=meshgrid(a);
cube=(X.*Y.*Z);
end
But, this give me the following figure:
Can somebody help me out please? Thanks!

Réponses (1)

Cris LaPierre
Cris LaPierre le 13 Jan 2022

0 votes

4 commentaires

array_width=101;%must be odd
[cube, faces]=create_cube(array_width);
figure
patch('Vertices',cube,'Faces',faces,'FaceColor',[0 0 .7],'EdgeColor',[0 0 1]);
view(3)
axis equal
title('Simulation of solid cube')
function [cube,faces]=create_cube(array_width)
a=(array_width-1)/2;
cube = unique(nchoosek([-a a -a a -a a],3),"rows");
faces = [1 2 4 3;
5 6 8 7;
1 2 6 5;
3 4 8 7;
1 3 7 5;
2 4 8 6];
end
Lieke Pullen
Lieke Pullen le 14 Jan 2022
Hi Cris, thank you for your ellaboration. But, this gives me a 8x3 cube, which is a 2D array, and I would like have a 3D-array. Any idea how to implement that?
I would look into slice. To work as I want, I had to adjust your function to return the X,Y,and Z data, too.
array_width=101;%must be odd
[X,Y,Z,outer_cube]=create_cube(array_width);
figure
s = slice(X,Y,Z,outer_cube,[-50 50],[-50 50],[-50 50]);
set(s,'EdgeColor','none')
function [X,Y,Z,cube]=create_cube(array_width)
a=(array_width-1)/2;
a=linspace(-a,a,array_width);
[X,Y,Z]=meshgrid(a);
cube=(X.*Y.*Z);
end
Here's another approach
array_width=101;%must be odd
[X,Y,Z,outer_cube]=create_cube(array_width);
figure
k=boundary([X(:),Y(:),Z(:)]);
trisurf(k,X(:),Y(:),Z(:),outer_cube,'EdgeColor','none')
function [X,Y,Z,cube]=create_cube(array_width)
a=(array_width-1)/2;
a=linspace(-a,a,array_width);
[X,Y,Z]=meshgrid(a);
cube=(X.*Y.*Z);
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Line Plots dans Centre d'aide et File Exchange

Produits

Version

R2020a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by