
How to use fill3 to make a box
조회 수: 8 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Darshak
2025년 3월 6일
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
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 Center 및 File 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!