问题描述
我想用一些尺寸 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.
在构建立方体并用其他小立方体填充它之后,我想构建一个矩阵来表示大立方体中的这些小立方体,这是因为我希望能够访问每个小立方体,在那里我需要改变它的颜色.
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.
是否可以用这个矩阵来表示小立方体的结构?假设每个小立方体都用 -1 表示,我的意思是矩阵一行中的所有 -1 都是同一行的小立方体,而列中的所有 -1 实际上是同一列中的小立方体(矩阵内的邻居)大立方体必须是矩阵内的邻居).由于大立方体是 3D 形状,我希望这样的矩阵是具有行、列和深度度量的 3D 矩阵.深度可以代表我们拥有的不同小立方体的层,即在深度 1 我们有一组行和列代表第一个深度的小立方体.之后,我想遍历这个矩阵并将 -1 更改为代表某种颜色的其他数字.如何使用矩阵中的相应数字更改一些小立方体的颜色?例如,让索引 (1,1,1) 处的数字为 0 并让它代表黄色,如何将相应的立方体颜色更改为黄色?我在想patch
功能,但是如何将其应用到相应的立方体上?
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?
这是一个将小立方体放在大立方体内部的代码,但这将它放在中心,我试图按照提供的图片以有组织的方式用小立方体填充大立方体,但我不能弄清楚如何做到这一点.
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?
谢谢.
推荐答案
可以轻松地对您的代码进行一些细微的修改,以使用较小的立方体填充立方体.您已经有了在中心放置一个立方体的代码.你真正需要做的就是随机化基础小立方体的中心,用这个中心调整立方体的中心,然后把它放在更大的立方体中.您也可以随机化立方体的颜色.我们可以根据需要循环多次立方体,您可以为每个立方体生成随机中心位置以及随机颜色,并将它们放置到最终的立方体上.
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.
在代码末尾执行此操作:
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
现在将是一个 3D 矩阵,其中每个切片是一组代表一个立方体的 3D 坐标.类似地,colors
将表示一个 2D 矩阵,其中每一行是与您绘制的立方体相关联的颜色.
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
和 colors
来重建它,您可以这样做:
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 中构建一个空心立方体并用小立方体对其进行归档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!