Multiple surfaces in 3D with different colors

조회 수: 8 (최근 30일)
Eja  Eja
Eja Eja 2017년 7월 27일
답변: Vedant Shah 2025년 1월 31일
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

답변 (1개)

Vedant Shah
Vedant Shah 2025년 1월 31일
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]);

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by