【问题标题】:Building a hollow cube and filing it with small cubes in MATLAB在 MATLAB 中构建一个空心立方体并用小立方体填充它
【发布时间】:2015-11-01 00:03:16
【问题描述】:

我想构建一个具有 x、y 和 z 维度的空心立方体,并用许多小立方体填充它的体积。下图与我想做的类似,

但是,我想用小立方体代替小球体。

在构建立方体并用其他小立方体填充它之后,我想构建一个矩阵来表示大立方体中的这些小立方体,这是因为我希望能够访问每个小立方体,其中我需要改变它的颜色

是否可以用这个矩阵来表示小立方体的结构?假设每个小立方体都用-1表示,我的意思是矩阵的一行中的所有-1都是同一行中的小立方体,而一列中的所有-1实际上是同一列中的小立方体(相邻大立方体必须是矩阵内的邻居)。由于大立方体是 3D 形状,我希望这样的矩阵是具有行、列和深度度量的 3D 矩阵。深度可能代表我们拥有的不同小立方体的层,即在深度 1 处,我们有一组行和列代表第一深度的小立方体。之后,我想遍历这个矩阵并将 -1 更改为代表某种颜色的其他数字。如何使用矩阵中的相应数字改变一些小立方体的颜色?比如让索引(1,1,1)处的数字为0,让它代表黄色,如何将对应的立方体颜色改为黄色?我在想patch 函数,但是如何将它应用到对应的立方体上呢?

如何用小方块填充方块?还有,如何构建上述矩阵?

这是一个将一个小立方体放在大立方体内的代码,但这会将它放在中心,我尝试按照提供的图片以有组织的方式用小立方体填充大立方体,但我做不到弄清楚如何做到这一点。

clf;
figure(1);
format compact 
h(1) = axes('Position',[0.2 0.2 0.6 0.6]);

%These are the different 8 vertices of the cube, each is defined by its 3 x
%y z coordinates:
vert = [1 1 -1; 
        -1 1 -1; 
        -1 1 1; 
        1 1 1; 
        -1 -1 1;
        1 -1 1; 
        1 -1 -1;
        -1 -1 -1];
%These are the 6 faces of the cube, each is defined by connecting 4 of the
%available vertices:
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];

% I defined a new cube whose length is 1 and centers at the origin.
vert2 = vert * .05;  
fac2 = fac;


patch('Faces',fac,'Vertices',vert,'Facecolor', 'w');  % patch function for the first big cube. 
axis([-1, 1, -1, 1, -1, 1]);
axis equal;

hold on;

patch('Faces', fac2, 'Vertices', vert2, 'FaceColor', 'r');
material metal;
alpha('color');
alphamap('rampdown');
view(3);

谁能告诉我如何填充立方体并构造矩阵?

谢谢。

【问题讨论】:

    标签: matlab image-processing matrix graphics 3d


    【解决方案1】:

    您可以轻松地对您的代码进行一些细微的修改,以便用较小的立方体填充立方体。您已经拥有将一个立方体放置在中心的代码。您真正需要做的就是随机化基础小立方体的中心,用这个中心调整立方体的中心,然后将其放入较大的立方体中。您也可以随机化立方体的颜色。我们可以根据需要循环多次,您可以为每个立方体生成随机中心位置以及随机颜色并将它们放置到最终的立方体上。

    在代码末尾执行此操作:

    hold on;
    rng(123); %// Set seed for reproducibility
    num_squares = 1000; %// Set total number of squares
    
    %// For each square...
    for idx = 1 : num_squares
    
        %// Take the base cube and add an offset to each coordinate
        %// Each coordinate will range from [-1,1]
        vert_new = bsxfun(@plus, vert2, 2*rand(1,3)-1);
    
        %// Generate a random colour for each cube
        color = rand(1,3);
    
        %// Draw the cube
        patch('Faces', fac, 'Vertices', vert_new, 'FaceColor', color);
    end
    
    %// Post processing
    material metal;
    alpha('color');
    alphamap('rampdown');
    view(3);
    

    我们得到这张图片:

    现在,如果您想构建这些坐标的 3D 矩阵,那非常简单。只需有一个矩阵并在每次迭代时连接这些随机生成的坐标:

    hold on;
    rng(123); %// Set seed for reproducibility
    num_squares = 1000; %// Set total number of squares
    
    %// New - to store the coordinates
    coords = [];
    
    %// For remembering the colours
    colors = [];
    
    %// For each square...
    for idx = 1 : num_squares
    
        %// Take the base cube and add an offset to each coordinate
        %// Each coordinate will range from [-1,1]
        vert_new = bsxfun(@plus, vert2, 2*rand(1,3)-1);
    
        %// New - For the coordinates matrix
        coords = cat(3, coords, vert_new);
    
        %// Generate a random colour for each cube
        color = rand(1,3);
    
        %// New - Save the colour
        colors = cat(1, colors, color);
    
        %// Draw the cube
        patch('Faces', fac, 'Vertices', vert_new, 'FaceColor', color);
    end
    
    %// Post processing
    material metal;
    alpha('color');
    alphamap('rampdown');
    view(3);
    

    coords 现在将是一个 3D 矩阵,其中每个切片是一组代表一个立方体的 3D 坐标。同样,colors 将表示一个 2D 矩阵,其中每一行是与您绘制的立方体相关联的颜色。

    如果你想只使用coordscolors 来重构它,你可以这样做:

    close all;
    clf;
    figure(1);
    format compact 
    h(1) = axes('Position',[0.2 0.2 0.6 0.6]);
    
    patch('Faces',fac,'Vertices',vert,'Facecolor', 'w');  % patch function for the first big cube. 
    axis([-1, 1, -1, 1, -1, 1]);
    axis equal;
    
    vert = [1 1 -1; 
            -1 1 -1; 
            -1 1 1; 
            1 1 1; 
            -1 -1 1;
            1 -1 1; 
            1 -1 -1;
            -1 -1 -1];
    
    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];
    
    vert2 = vert * .05;  
    
    %// For each square...
    for idx = 1 : num_squares
    
        %// Take the base cube and add an offset to each coordinate
        %// Each coordinate will range from [-1,1]
        vert_new = coords(:,:,idx);
    
        %// Generate a random colour for each cube
        color = colors(idx,:);
    
        %// Draw the cube
        patch('Faces', fac, 'Vertices', vert_new, 'FaceColor', color);
    end
    
    %// Post processing
    material metal;
    alpha('color');
    alphamap('rampdown');
    view(3);
    

    这应该使用您要保存的矩阵重现相同的图形。

    【讨论】:

    • 非常感谢rayryeng,这是一个很好的答案,它很清楚并且工作完美。我只是有一些问题,我明白这一行,vert_new = coords(:,:,idx);遍历 coords 数组,我们将内部立方体的数量 idx 作为第三个参数,这是否意味着 idx 也可以作为立方体的 z 轴?另外,如果我在某个立方体坐标处并且我想移动到它右侧的立方体,我该如何使用坐标来做到这一点?是通过将列增加一(第二个参数)吗?非常感谢。
    • @Dania - 嗨。抱歉耽搁了。 (1) 不,它没有。 idx 为您提供定义立方体的 6 个坐标,因此 idx 将为您提供由该索引定义的立方体 #idx。我不太明白你所说的 3D 矩阵是什么意思,所以这就是我解释你的问题的方式。如果这不是你想要的,请告诉我。 (2) 每列是x(第一)、y(第二)或z(第三)坐标。如果您想将立方体“向右”移动,那么您可以按照您的说明在第二列中添加一个偏移量。如果您还有其他问题,请告诉我。
    • 非常感谢@rayryeng 的回答。坐标的第一个第二个和第三个参数代表什么?实际上,我想用许多小立方体填充立方体,然后通过 (x,y,z) 位置访问每个人,这就是为什么我想制作一个代表它们的 3D 矩阵。我的目标是,如果我站在某个位置的某个小立方体上,我必须能够找到它的邻居(任何位于其右侧、左侧、顶部或底部的立方体 - 实际上我想通过立方体)。有没有更简单的方法来做到这一点?例如,我们可以使用您为每个立方体定义的中心来执行此操作吗?谢谢
    • (1) 3D 矩阵的每一行都是一个坐标。 3D 矩阵的每一列都是坐标的一维。每个切片是 6 个坐标的集合,用于可视化一个立方体。执行coords(:,:,idx) 将为您提供一个切片的 6 个坐标,在此切片中,每一行为您提供此立方体上一个点的 (x,y,z) 坐标。现在,如果您真的想找到每个立方体的中心,您可以通过执行以下操作分别找到每列的平均值:centre_pos = mean(coords, 2);。这将为您提供一个包含这些中心位置的每个切片 1 行的 3D 矩阵。
    • (2) “左、右、上、下”在 3D 中是不明确的。您可以在单个立方体附近有许多立方体,那么您将遍历哪个立方体?如果您查看每个立方体的中心可能会更好,因此使用前面注释中的上述代码行计算中心会有所帮助。如果您有任何疑问,我认为您应该提出另一个问题。
    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多