How i draw a cube with a Cut?
6 views (last 30 days)
Show older comments
Hi. I need Help for drawing a cube with a cut in the center.
With this coordinates i have a cube.Only a cube
X = [0 0 0 0 0 1;...
1 0 1 1 1 1;...
1 0 1 1 1 1;...
0 0 0 0 0 1];
Y = [0 0 0 0 1 0;...
0 1 0 0 1 1;...
0 1 1 1 1 1;...
0 0 1 1 1 0];
Z = [0 0 1 0 0 0;...
0 0 1 0 0 0;...
1 1 1 0 1 1;...
1 1 1 0 1 1];
What is the new coordenates for drwaing a cube with a cut in the middle, like the picture in anexo. And draw the cube with the command patch.
alpha=1
C= [0.5 0.2 0.3 0.5 0.1 0.5];
patch(X,Y,Z,C,'FaceAlpha',alpha)
Thaks for the time. Andre
0 Comments
Answers (1)
TED MOSBY
on 2 Jan 2025 at 8:19
Hi,
As you have not specified what kind cut you want, I assume you want a square cut in the centre of the top surface. Have a look at this example code to know how to use the patch command:
% Define the vertices of the cube
X = [0 1 1 0 0 1 1 0];
Y = [0 0 1 1 0 0 1 1];
Z = [0 0 0 0 1 1 1 1];
% Define the faces of the cube
faces = [
1 2 3 4; % Bottom face
5 6 7 8; % Top face
1 2 6 5; % Front face
2 3 7 6; % Right face
3 4 8 7; % Back face
4 1 5 8; % Left face
];
% Define vertices for the cut in the top face
cut_vertices = [
0.4 0.4 1; % Cut vertex 1
0.6 0.4 1; % Cut vertex 2
0.6 0.6 1; % Cut vertex 3
0.4 0.6 1 % Cut vertex 4
];
% Append the cut vertices to the existing vertices
X = [X cut_vertices(:,1)'];
Y = [Y cut_vertices(:,2)'];
Z = [Z cut_vertices(:,3)'];
% Define the new faces for the top face with the cut
faces_with_cut = [
5 9 10 6; % Top face part 1
10 11 7 6; % Top face part 2
11 12 8 7; % Top face part 3
9 12 8 5; % Top face part 4
9 10 11 12 % Cut face
];
% Combine the original faces with the new top face with the cut
faces = [
faces(1,:); % Bottom face
faces_with_cut; % Top face with cut
faces(3:end,:) % Side faces
];
% Color for each face
C = [0.5 0.2 0.3 0.5 0.1 0.5 0.7 0.7 0.7 0.7];
% Draw the cube with the cut using patch
figure;
patch('Vertices', [X' Y' Z'], 'Faces', faces, 'FaceVertexCData', C', 'FaceColor', 'flat', 'FaceAlpha', 1);
xlabel('X');
ylabel('Y');
zlabel('Z');
axis equal;
view(3);
Hope this helps!
0 Comments
See Also
Categories
Find more on Polygons 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!