建立一个中空立方体,并提交其与MATLAB小方块 [英] Building a hollow cube and filing it with small cubes in MATLAB

查看:311
本文介绍了建立一个中空立方体,并提交其与MATLAB小方块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个中空立方体某些尺寸的x,y和z,并填写其体积与许多小方块。下图是相似,我想做的事情,

I want to build a hollow cube with some dimensions x, y, and z, and fill its volume with many small cubes. The following image is similar to what I want to do,

不过,不是小球,我想用小方块。

But, instead of small spheres, I want to use small cubes.

建立多维数据集,并与其他小方块填充​​它之后,我想建立一个矩阵,再presents内的大单,这些小方块,这是因为我希望能够访问每个小多维数据集,在这里我需要改变它的颜色

After building the cube and filling it with other small cubes, I want to build a matrix that represents these small cubes inside the big one, this is because I want to be able to access every small cube, where I need to change its color.

是否有可能有这个矩阵再presenting的小立方体结构?比方说,每个小立方体再presented -1,我的意思是一排矩阵的所有-1s在同一排的小立方体,并在一列所有-1s实际上是在同一列小方块(里面的大立方体邻居必须是矩阵内的邻居)。由于大立方体是3D形状,我想到这样一个矩阵是一个三维矩阵的行,列和深度的措施。深度可能会重新present不同的小方块,我们有层,即在深度为1,我们有一组行和列重新presenting的小方块在第一深度。从那以后,我要遍历这个矩阵,改变-1至重新presents一些色彩的一些其他号码。如何更改使用矩阵中相应数量的一些小魔方的颜色?例如,让索引数(1,1,1)为0,让它重新present黄色的,我怎么能更改相应的立方体颜色为黄色?我想补丁的功能,但如何将其应用到相应的立方体?

Is it possible to have this matrix representing the structure of the small cubes? Let's say every small cube is represented by -1, I mean that all -1s in a row of the matrix are small cubes at the same row, and all -1s in a column are actually small cubes in the same column (neighbors inside the big cube must be neighbors inside the matrix). Since the big cube is a 3D shape, I expect such a matrix to be a 3D matrix with row, column, and depth measures. Depth may represent the layers of different small cubes we have, i.e at depth 1 we have a set of rows and columns representing the small cubes at the first depth. After that I want to iterate over this matrix and change the -1 to some other number that represents some color. How to change the color of some small cube using the corresponding number in the matrix? For example, let the number at index (1,1,1) be 0 and let it represent yellow, how can I change the corresponding cube color to yellow? I'm think about patch function, but how to apply it to the corresponding cube?

我怎么能填补小立方体魔方?而且,我怎么能建立上述矩阵?

How can I fill the cube with small cubes? And, how can I build the matrix described above?

下面是一个code放置一个小立方体里面最大的一个,但这个地方是在中心,我试图填补了大立方体的小立方体有组织地在所提供的图片,但我无法弄清楚如何做到这一点。

Here is a code for placing a small cube inside the big one, but this places it in the center, I tried to fill the big cube with small cubes in an organized way as in the provided picture, but I couldn't figure out how to do that.

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);

谁能告诉我如何填写立方体,构建矩阵?

Can anyone please tell me how to fill the cube and construct the matrix?

感谢你。

推荐答案

稍作修改你的code可以很容易地做到以用小的立方体魔方。你已经有了code到放置一个立方体的中心。你真正需要做的是随机化基小立方体的中心,以调整该中心的立方体的中心,并将其放置在较大的立方体。您也可以随机立方体的颜色为好。我们可以为循环多次,你想要的立方体,你可以生成随机中心位置为每个多维数据集,以及随机颜色,并将其放置到最终的立方体。

Some slight modifications to your code can easily be done to populate the cube with smaller cubes. You already have the code to place one cube in the centre. All you really have to do is randomize the centre of the base small cube, adjust the centre of the cube with this centre and place it in the larger cube. You can also randomize the colour of the cube as well. We can loop for as many times as you want cubes and you can generate random centre positions for each cube as well as random colours and place them onto the final cube.

在你的code到底要做这样的:

Do this at the end of your code:

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矩阵,这是相当简单的。只要有一个矩阵,并在每次迭代串联这些随机产生的坐标:

Now if you want to build a 3D matrix of these said coordinates, that's quite simple. Simply have a matrix and concatenate these randomly generated coordinates at each iteration:

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 现在将是一个三维矩阵,其中每片为一组三维坐标的重新presents一个多维数据集。同样,颜色将重新present一个二维矩阵,其中每行是你画立方体相关联的颜色。

coords will now be a 3D matrix where each slice is a set of 3D coordinates that represents one cube. Similarly, colors will represent a 2D matrix where each row is the colour associated with the cube you drew.

如果你想通过只使用重建这个 COORDS 颜色,你可以这样做:

If you want to reconstruct this by just using coords and colors, you can do this:

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);

这应该通过使用你想要保存矩阵再现相同的数字。

This should reproduce the same figure by using the matrices you wanted to save.

这篇关于建立一个中空立方体,并提交其与MATLAB小方块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆