How to use fill3 to make a box

8 次查看(过去 30 天)
axel rose
axel rose 2021-1-31
评论: Voss 2025-3-29
X= [1 26 26 1]
Y= [0.5 .5 25.5 25.5]
Z= [0 0 0 0]
fill3 (X, Y, Z, 'blue')
X1= [1 26 26 1]
Y1= [0.5 .5 25.5 25.5]
Z1= [25 25 25 25]
fill3 (X1, Y1, Z1, 'red')
I wanna make a box with fill3 but when i typed in the 2nd fill3 code (the red one), it does not produce a surface. How can i make multiple surfaces with fill3

回答(1 个)

Darshak
Darshak 2025-3-6
Hello @axel rose,
The issue with the second surface not being produced using the “fill3” function arises from the vertices not being correctly defined for a 3D face. To achieve the desired box, it is essential to use the “hold on” and “hold off” commands, these commands are used to plot the surfaces on the same axis. Below is the revised code to accomplish this objective:
X = [1 26 26 1];
Y = [0.5 0.5 25.5 25.5];
Z_bottom = [0 0 0 0];
Z_top = [25 25 25 25];
fill3(X, Y, Z_bottom, 'blue');
hold on;
fill3(X, Y, Z_top, 'red');
X_sides = [1 1; 26 26; 26 26; 1 1];
Y_sides = [0.5 0.5; 0.5 0.5; 25.5 25.5; 25.5 25.5];
Z_sides = [0 25; 0 25; 0 25; 0 25];
for i = 1:4
fill3(X_sides(i, :), Y_sides(i, :), Z_sides(i, :), 'green');
end
axis equal;
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
grid on;
hold off;
This is the output we get using the given code.
The “fill3” function can be explored on this documentation page - https://www.mathworks.com/help/releases/R2021a/matlab/ref/fill3.html
The “hold” command can be explored on this documentation page - https://www.mathworks.com/help/releases/R2021a/matlab/ref/hold.html
  1 个评论
Voss
Voss 2025-3-29

Note that the green patches (not surfaces, FYI) don't show up on the plot. This is because their coordinates describe the four lateral edges instead of the four lateral faces, i.e., they each have two points instead of four.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Guidance, Navigation, and Control (GNC) 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by