【问题标题】:Simple cubic lattice using three-dimensional array使用三维数组的简单三次点阵
【发布时间】:2014-12-26 00:24:50
【问题描述】:

我想使用 MATLAB 绘制一个简单的立方晶格。

我已经阅读了How to plot 3D grid (cube) in Matlab,但是,我想为每个小立方体着色。

我在MATLAB中有一个三维数组,比如,

cube(:,:,1) = [1 0 1
               0 1 1
               1 1 0]
cube(:,:,2) = [0 0 1
               1 1 1
               0 1 0]
cube(:,:,3) = [1 1 1
               0 1 1
               1 0 1]

如何使用这个数组绘制一个简单的立方格子,其中cube(:,:,1)表示立方格子的一楼,cube(:,:,2)表示二楼,cube(:,:,3)表示三楼。

0 表示白色小立方体,1 表示黑色小立方体。

想要的结果是这样的:http://www.instructables.com/id/Puzzle-Cube/

【问题讨论】:

    标签: arrays image matlab plot 3d


    【解决方案1】:

    我找不到更简单的东西,所以就是这样!

    C = randi(2,[3 3 3])-1;
    colorC = char(repmat('k',[3 3 3]));
    colorC(C == 0) = 'y';
    figure(1);
    for x = 0 : 2
    for y = 0 : 2
    for z = 0 : 2
         vert = [1 1 0; 
                 0 1 0; 
                 0 1 1; 
                 1 1 1; 
                 0 0 1;
                 1 0 1; 
                 1 0 0;
                 0 0 0];
         vert(:,1) = vert(:,1) + x;
         vert(:,2) = vert(:,2) + y;
         vert(:,3) = vert(:,3) + z;
         fac = [1 2 3 4; 
                4 3 5 6; 
                6 7 8 5; 
                1 2 8 7; 
                6 7 1 4; 
                2 3 5 8];
         patch('Faces',fac,'Vertices',vert,'FaceColor',colorC(x + 1, y + 1, z + 1)); 
         axis([0, 3, 0, 3, 0, 3]);
         alpha('color');
         alphamap('rampdown');
         axis equal
         hold on
    end
    end
    end
    

    给你这个,

    如果你删除alpha('color');alphamap('rampdown');并使用axis off,你会得到,

    【讨论】:

    • 我无法通过你得到的代码重新出现上图。不过,我找到了最好的答案,请看emuch.net/bbs/viewthread.php?tid=8090152&viewexp=yes
    • @Kongling,加油!只需复制并粘贴代码。我的代码中的C 是你的cube
    • 对不起,按照你说的我已经试过了。但是,我只拿到了斧头。
    • 感谢您的耐心回复,我已经通过复制粘贴您写的代码生成了图片。
    • @Kongling,感谢上帝。我很高兴它终于奏效了。是你想要的吗?
    猜你喜欢
    • 1970-01-01
    • 2020-03-08
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多