Clear Filters
Clear Filters

Take slices of the cube

6 views (last 30 days)
george korris
george korris on 2 Apr 2022
Commented: george korris on 3 Apr 2022
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:

Answers (1)

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

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!