这种图在matlab中怎么得到

img

先用plot3画长方体轮廓,再用fill3绘制正方形。

a = 20;
b = 20;
c = 20;

A1 = [0 0 0
     a 0 0
     a b 0
     0 b 0
     0 0 0];
A2 = [0 0 c
     a 0 c
     a b c
     0 b c
     0 0 c]; 
A3 = [0 0 0
     0 0 c
     0 b c
     0 b 0
     0 0 0]; 
A4 = [a 0 0
     a 0 c
     a b c
     a b 0
     a 0 0];  
% 绘图
figure
hold on
plot3(A1(:,1),A1(:,2),A1(:,3),'k');
plot3(A2(:,1),A2(:,2),A2(:,3),'k');
plot3(A3(:,1),A3(:,2),A3(:,3),'k');
plot3(A4(:,1),A4(:,2),A4(:,3),'k');
view(3)


XYZ0 = [0 0 0
        1 0 0
        1 0 1
        0 0 1];
for ii = 1:100
    p = [rand*(a-2) rand*(b-2) rand*(c-2)]+1;    % 球心
    X2=  p(1) + XYZ0(:,1);
    Y2 = p(2) + XYZ0(:,2);
    Z2 = p(3) + XYZ0(:,3);
    ColorSpec = rand(1,3);
    fill3( X2,Y2,Z2,ColorSpec )
end

结果:

img

颜色片的中心。若有帮助,麻烦点击采纳。