本文介绍了六边形格子,随机用黑色和白色上色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试绘制一个(10,000 x 10,000)六边形的格子,该格子随机是一半黑与一半白色.我不知道如何将这个格子的六边形随机填充为黑白.(这是我从此代码中真正想要的示例,但我无法实现.).代码(用matlab编写):
I am trying to draw a (10,000 x 10,000) hexagonal lattice which is randomly half black and half white.I don't know how to fill hexagons of this lattice randomly to black and white.(this is a sample of what i really want from this code but I couldn't make it.).here is the code(written in matlab):
clc
x=input('enter the value of x: ');
y=input('enter the value of y: ');
r=input('enter the value of R: ');
n=input('enter the value of N: ');
d=sqrt(3*n)*r
axis([0 x 0 y ])
c=r;
v=30:60:390;
cv=r*cosd(v);
sv=r*sind(v);
for y=0:2:y
for w=0:2:x
line(w*sqrt(3)/2*c+cv,y*1.5*c+sv,'tag','h');
end
end
for m=1:2:y
for k=1:2:x
line(k*sqrt(3)/2*c+cv,m*1.5*c+sv,'tag','h');
end
end
有人可以帮我吗?
推荐答案
您可以使用 patch
.这种方法比循环绘制fill
六边形的六边形要快得多.
You can plot multiple filled polygons using patch
. This approach is extremely faster than drawing hexagons with fill
one by one in a loop.
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
这将为您提供所需的输出:
This gives you the desired output:
这篇关于六边形格子,随机用黑色和白色上色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!