Multiple surfaces in 3D with different colors
5 views (last 30 days)
Show older comments
Dear all,
I have a function for plot cubic elements in 3D. The shape is not clear when I use the same color for all surfaces. So, I want to make the top and bottom of each element in the same color like dark blue "[0,0.2,0.8]", while the side surfaces in a different color like light blue "[0.2,0.7,1]". How can I do that with my function? Thanks. My function is
function Plotxyz1(nely,nelx,nelz)
hx = 1; hy = 1; hz = 1;
face = [1 2 3 4; 2 6 7 3; 4 3 7 8; 1 5 8 4; 1 2 6 5; 5 6 7 8];
for k = 1:nelz
z = (k-1)*hz;
for i = 1:nelx
x = (i-1)*hx;
for j = 1:nely
y = nely*hy - (j-1)*hy;
vert = [x y z; x y-hx z; x+hx y-hx z; x+hx y z; x y z+hx;x y-hx z+hx; x+hx y-hx z+hx;x+hx y z+hx];
vert(:,[2 3]) = vert(:,[3 2]); vert(:,2,:) = -vert(:,2,:);
patch('Faces',face,'Vertices',vert,'FaceColor',[0.2,0.7,1]);
hold on;
end
end
end
axis equal; axis tight; axis off; box on; view([30,30]); pause(1e-6);
end
0 Comments
Answers (1)
Vedant Shah
on 31 Jan 2025
To make the top and bottom faces of the cube a different color compared to the other faces, we can use the “patch” function twice: first for the top and bottom faces, filling them with a dark blue color, and second for the side faces, filling them with a light blue color.
The following modifications can be made to the code:
Instead of assigning all the faces of the cube to a single “face” variable:
face = [1 2 3 4; 2 6 7 3; 4 3 7 8; 1 5 8 4; 1 2 6 5; 5 6 7 8];
We can create two separate variables: one for the top and bottom faces, and another for the side faces.
top_bottom_faces = [1 2 3 4;5 6 7 8];
side_faces = [2 6 7 3; 4 3 7 8; 1 5 8 4; 1 2 6 5];
Also, we need to add two different patch statements for the respective face variables:
% Plot top and bottom faces
patch('Faces', top_bottom_faces, 'Vertices', vert, 'FaceColor', [0, 0.2, 0.8]);
% Plot side faces
patch('Faces', side_faces, 'Vertices', vert, 'FaceColor', [0.2, 0.7, 1]);
0 Comments
See Also
Categories
Find more on Surface and Mesh Plots 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!