Take slices of the cube

2 次查看(过去 30 天)
george korris
george korris 2022-4-2
Hi everyone the following code creates random cubes inside a big cube:
clc;
clear all;
%y z coordinates:
vert = [1 1 -1;
-1 1 -1;
-1 1 1;
1 1 1;
-1 -1 1;
1 -1 1;
1 -1 -1;
-1 -1 -1];
%These are the 6 faces of the cube, each is defined by connecting 4 of the
%available vertices:
fac = [1 2 3 4;
4 3 5 6;
6 7 8 5;
1 2 8 7;
6 7 1 4;
2 3 5 8];
% I defined a new cube whose length is 1 and centers at the origin.
vert2 = vert * .05;
fac2 = fac;
patch('Faces',fac,'Vertices',vert,'Facecolor', 'w'); % patch function for the first big cube.
axis([-1, 1, -1, 1, -1, 1]);
axis equal;
hold on;
rng(123); %// Set seed for reproducibility
num_squares = 100; %// Set total number of squares
%// New - to store the coordinates
coords = [];
%// For remembering the colours
colors = [];
%// For each square...
for idx = 1 : num_squares
%// Take the base cube and add an offset to each coordinate
%// Each coordinate will range from [-1,1]
vert_new = bsxfun(@plus, vert2, 2*rand(1,3)-1);
%// New - For the coordinates matrix
coords = cat(3, coords, vert_new);
%// Generate a random colour for each cube
color = rand(1,3);
%// New - Save the colour
colors = cat(1, colors, color);
%// Draw the cube
%patch('Faces', fac, 'Vertices', vert_new, 'FaceColor', color);
patch('Faces', fac, 'Vertices', vert_new);
axis off;
end
%// Post processing
material metal;
alpha('color');
alphamap('rampdown');
view(3);
and gives this :
does anyone know how can i plot slices of the cube ?
like this one:

回答(1 个)

Image Analyst
Image Analyst 2022-4-2
How about the slice() function?
  7 个评论
Image Analyst
Image Analyst 2022-4-3
I guess there would then be no black squares except along the outside perimeter of the image.
george korris
george korris 2022-4-3
But i have cubes randomly inside my big cube.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by