在 MATLAB 中构建一个空心立方体并用小立方体填充它
Building a hollow cube and filing it with small cubes in MATLAB
我想构建一个具有 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);
谁能告诉我如何填充立方体和构造矩阵?
谢谢。
可以轻松地对您的代码进行一些细微的修改,以使用更小的多维数据集填充多维数据集。您已经有了将一个立方体放在中心的代码。您真正需要做的就是随机化基本小立方体的中心,用这个中心调整立方体的中心并将其放置在较大的立方体中。您也可以随机化立方体的颜色。我们可以根据需要循环多次立方体,您可以为每个立方体生成随机中心位置以及随机颜色并将它们放置到最终立方体上。
在代码末尾执行此操作:
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
将表示一个二维矩阵,其中每一行都是与您绘制的立方体相关联的颜色。
如果你想只用coords
和colors
重建这个,你可以这样做:
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);
这应该使用您要保存的矩阵重现相同的图形。
我想构建一个具有 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);
谁能告诉我如何填充立方体和构造矩阵?
谢谢。
可以轻松地对您的代码进行一些细微的修改,以使用更小的多维数据集填充多维数据集。您已经有了将一个立方体放在中心的代码。您真正需要做的就是随机化基本小立方体的中心,用这个中心调整立方体的中心并将其放置在较大的立方体中。您也可以随机化立方体的颜色。我们可以根据需要循环多次立方体,您可以为每个立方体生成随机中心位置以及随机颜色并将它们放置到最终立方体上。
在代码末尾执行此操作:
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
将表示一个二维矩阵,其中每一行都是与您绘制的立方体相关联的颜色。
如果你想只用coords
和colors
重建这个,你可以这样做:
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);
这应该使用您要保存的矩阵重现相同的图形。