Matlab 六边形晶格,随机着色为黑白色
我正在尝试绘制一个(10000 x 10000)六边形晶格,它是随机的半黑半白。我不知道如何将这个晶格的六边形随机填充为黑白。下面是代码(用matlab编写): 有人能帮我解决这个问题吗?不推荐! 通过正确协调晶格,您可以实现所需的输出:Matlab 六边形晶格,随机着色为黑白色,matlab,Matlab,我正在尝试绘制一个(10000 x 10000)六边形晶格,它是随机的半黑半白。我不知道如何将这个晶格的六边形随机填充为黑白。下面是代码(用matlab编写): 有人能帮我解决这个问题吗?不推荐! 通过正确协调晶格,您可以实现所需的输出: m = 100; % horizontal count n = 50; % vertical count blackratio = 0.5; % here you can choose the ratio of black hexagons % parame
m = 100; % horizontal count
n = 50; % vertical count
blackratio = 0.5; % here you can choose the ratio of black hexagons
% parametric definition of a hexagon
t = (1/12:1/6:1)'*2*pi;
x = cos(t);
y = sin(t);
blacks = rand(m, n) < blackratio;
d=sqrt(3)/2;
figure;
hold on
for ii = 1:m
for jj = 1:n
if blacks(ii, jj)
% draw a black hexagon
fill(x + d*(mod(2*ii+jj, 2*m)), y+1.5*jj, 'k', 'EdgeColor', 'None')
else
% draw a white hexagon
fill(x + d*(mod(2*ii+jj, 2*m)), y+1.5*jj, 'w', 'EdgeColor', 'None')
end
end
end
axis equal tight off
m=100;%水平计数
n=50;%垂直计数
黑度=0.5;%在这里你可以选择黑色六边形的比例
%六边形的参数定义
t=(1/12:1/6:1)“*2*pi;
x=cos(t);
y=sin(t);
黑度=兰特(m,n)<黑度比;
d=sqrt(3)/2;
图形
等等
对于ii=1:m
对于jj=1:n
如果黑人(ii,jj)
%画一个黑六边形
填充(x+d*(mod(2*ii+jj,2*m)),y+1.5*jj,'k','EdgeColor','None')
其他的
%画一个白色的六边形
填充(x+d*(模块(2*ii+jj,2*m)),y+1.5*jj,'w','EdgeColor','None')
结束
结束
结束
轴等紧
使用此输出:
请注意,在我的100x50笔记本电脑上,需要6秒钟才能得到结果。对于1000x1000,我的计算机崩溃了
我的代码中的第二个
fill
函数将透明度替换为白色。如果您对透明而不是白色填充满意,则可以删除这部分代码并将速度提高一倍。您可以使用绘制多个填充多边形。这种方法比在循环中逐个使用fill
绘制六边形要快得多
m = 100; % horizontal count
n = 50; % vertical count
blackratio = 0.5; % here you can choose the ratio of black hexagons
blacks = rand(m, n) > blackratio;
hexcount = sum(blacks(:));
whitecount = m * n - hexcount;
% parametric definition of a hexagon
t = (1/12:1/6:1)' * 2 * pi;
x = cos(t);
y = sin(t);
% coordinates of all black hexagons
Xb = zeros(6, hexcount);
Yb = zeros(6, hexcount);
% coordinates of all white hexagons
Xw = zeros(6, whitecount);
Yw = zeros(6, whitecount);
d=sqrt(3)/2;
bcount = 0;
wcount = 0;
for ii = 1:m
for jj = 1:n
if blacks(ii, jj)
bcount = bcount + 1;
Xb(:, bcount) = x + d * (mod(2 * ii + jj, 2 * m));
Yb(:, bcount) = y + 1.5 * jj;
else
wcount = wcount + 1;
Xw(:, wcount) = x + d * (mod(2 * ii + jj, 2 * m));
Yw(:, wcount) = y + 1.5 * jj;
end
end
end
figure; hold on
patch(Xb, Yb, 'k', 'EdgeColor', 'None')
patch(Xw, Yw, 'w', 'EdgeColor', 'None')
axis equal off
这将为您提供所需的输出:
请正确链接示例。请看一看MATLAB的
补丁
我做的正确,您现在就可以检查了。非常感谢您,erfan,这对我帮助很大。
m = 100; % horizontal count
n = 50; % vertical count
blackratio = 0.5; % here you can choose the ratio of black hexagons
blacks = rand(m, n) > blackratio;
hexcount = sum(blacks(:));
whitecount = m * n - hexcount;
% parametric definition of a hexagon
t = (1/12:1/6:1)' * 2 * pi;
x = cos(t);
y = sin(t);
% coordinates of all black hexagons
Xb = zeros(6, hexcount);
Yb = zeros(6, hexcount);
% coordinates of all white hexagons
Xw = zeros(6, whitecount);
Yw = zeros(6, whitecount);
d=sqrt(3)/2;
bcount = 0;
wcount = 0;
for ii = 1:m
for jj = 1:n
if blacks(ii, jj)
bcount = bcount + 1;
Xb(:, bcount) = x + d * (mod(2 * ii + jj, 2 * m));
Yb(:, bcount) = y + 1.5 * jj;
else
wcount = wcount + 1;
Xw(:, wcount) = x + d * (mod(2 * ii + jj, 2 * m));
Yw(:, wcount) = y + 1.5 * jj;
end
end
end
figure; hold on
patch(Xb, Yb, 'k', 'EdgeColor', 'None')
patch(Xw, Yw, 'w', 'EdgeColor', 'None')
axis equal off