2

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

can anyone help me through this?

kh.omid
  • 33
  • 4

2 Answers2

1

Not recommended!

You can achieve your desired output using fill and by coordinating through the lattice correctly:

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

With this output:

enter image description here

Note that on my laptop for 100x50 it took 6 seconds to get the result. For 1000x1000 my computer crashed.

The second fill function in my code replaces the transparency with white color. If you are fine with having transparency instead of white filling, you can remove this part of the code and double the speed.

Erfan
  • 1,897
  • 13
  • 23
0

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:

enter image description here

Erfan
  • 1,897
  • 13
  • 23