Random sized hexagones in a hexagone grid

3 views (last 30 days)
JdC
JdC on 17 Jan 2022
Commented: _ on 17 Jan 2022
Greetings,
I am trying to make random sized hexagons (red and blue) in a two color (black and white) random hexagon grid with hexagons that are of lower size than the grid but have the same center as their hexagon tile.
For now, with my code, I already have the random white and black hexagon grid, but I have trouble making the lower sized hexagons inside. as you can see when I run this program, I obtain non-hexagons shape inside.
%Problem with this line --> To show where I think I made a mistake in the code.
Could you please help me with this problem?
Thank you in advance,
JdC
%Hexagon radii
r_rod = 9*2/3;
%Wavelength
lambda = 1.550;
% grid size
N_grid = 100;
N_rod=fix(N_grid/r_rod);
highratio = 1/2;
bounds = [1, r_rod]; %possible radii size for the red and blue hexagons
m = N_rod+1; % horizontal count
n = fix(N_rod*2/sqrt(3))+2; % vertical count
rng('shuffle')
highs = rand(m, n) > highratio; %random assignment of white and black color
hexcount = sum(highs(:));
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
Xh = zeros(6, hexcount);
Yh = zeros(6, hexcount);
Xhcore = zeros(6, hexcount);
Yhcore = zeros(6, hexcount);
% coordinates of all white hexagons
Xl = zeros(6, whitecount);
Yl = zeros(6, whitecount);
Xlcore = zeros(6, whitecount);
Ylcore = zeros(6, whitecount);
d=sqrt(3)/2;
hcount = 0;
lcount = 0;
Rcore = rand(size(Xh))*range(bounds)+bounds(1);
R1core = rand(size(Xl))*range(bounds)+bounds(1);
for ii = 1:m
for jj = 1:n
if highs(ii, jj)
hcount = hcount + 1;
Xh(:, hcount) = x + d * (mod(2 * ii + jj, 2 * m));
Yh(:, hcount) = y + 1.5 * jj;
Xhcore(:, hcount) = Xh(:, hcount)-Rcore(:, hcount).*x/5; %Problem with this line
Yhcore(:, hcount) = Yh(:, hcount)-Rcore(:, hcount).*y/5; %Problem with this line
else
lcount = lcount + 1;
Xl(:, lcount) = x + d * (mod(2 * ii + jj, 2 * m));
Yl(:, lcount) = y + 1.5 * jj;
Xlcore(:, lcount) = Xl(:, lcount)-R1core(:, lcount).*x/5; %Problem with this line
Ylcore(:, lcount) = Yl(:, lcount)-R1core(:, lcount).*y/5; %Problem with this line
end
end
end
%figure; hold on
fig_i= figure();
fig_i.Visible='on';
fig_i.Position=[1,500,500,500];
fig_i.PaperUnits='inches';
fig_i.PaperPositionMode='manual';
fig_i.PaperPosition=[0,0,N_grid/150,N_grid/150];
axes_i=axes();
axes_i.Position =[0,0,1,1];
axes_i.Units='inches';
patch(Xh, Yh, 'black', 'EdgeColor', 'None');
patch(Xl, Yl, [0.9999,0.9999,0.9999], 'EdgeColor', 'None');
patch(Xhcore, Yhcore, 'red', 'EdgeColor', 'None');
patch(Xlcore, Ylcore, 'blue', 'EdgeColor', 'None');
axis equal;
xlim([0,N_rod*sqrt(3)]);
ylim([1.5,N_rod*sqrt(3)+1.5]);
axis off;

Accepted Answer

_
_ on 17 Jan 2022
The problem is here:
Rcore = rand(size(Xh))*range(bounds)+bounds(1);
R1core = rand(size(Xl))*range(bounds)+bounds(1);
Those are random matrices of size 6-by-the number of (red/blue) hexagons. This gives each hexagon a (potentially) different radius for each vertex. To have a different radius for each hexagon but the same radius for each vertex in a given hexagon, do this:
Rcore = rand(1,size(Xh,2))*range(bounds)+bounds(1);
R1core = rand(1,size(Xl,2))*range(bounds)+bounds(1);
Then each of those variables is a row vector with one value per hexagon. Then when you use them, just refer to the one element at a time, rather than a column.
%Hexagon radii
r_rod = 9*2/3;
%Wavelength
lambda = 1.550;
% grid size
N_grid = 100;
N_rod=fix(N_grid/r_rod);
highratio = 1/2;
bounds = [1, r_rod]; %possible radii size for the red and blue hexagons
m = N_rod+1; % horizontal count
n = fix(N_rod*2/sqrt(3))+2; % vertical count
rng('shuffle')
highs = rand(m, n) > highratio; %random assignment of white and black color
hexcount = sum(highs(:));
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
Xh = zeros(6, hexcount);
Yh = zeros(6, hexcount);
Xhcore = zeros(6, hexcount);
Yhcore = zeros(6, hexcount);
% coordinates of all white hexagons
Xl = zeros(6, whitecount);
Yl = zeros(6, whitecount);
Xlcore = zeros(6, whitecount);
Ylcore = zeros(6, whitecount);
d=sqrt(3)/2;
hcount = 0;
lcount = 0;
% Rcore = rand(size(Xh))*range(bounds)+bounds(1);
% R1core = rand(size(Xl))*range(bounds)+bounds(1);
Rcore = rand(1,size(Xh,2))*range(bounds)+bounds(1);
R1core = rand(1,size(Xl,2))*range(bounds)+bounds(1);
for ii = 1:m
for jj = 1:n
if highs(ii, jj)
hcount = hcount + 1;
Xh(:, hcount) = x + d * (mod(2 * ii + jj, 2 * m));
Yh(:, hcount) = y + 1.5 * jj;
% Xhcore(:, hcount) = Xh(:, hcount)-Rcore(:, hcount).*x/5; %Problem with this line
% Yhcore(:, hcount) = Yh(:, hcount)-Rcore(:, hcount).*y/5; %Problem with this line
Xhcore(:, hcount) = Xh(:, hcount)-Rcore(hcount).*x/5;
Yhcore(:, hcount) = Yh(:, hcount)-Rcore(hcount).*y/5;
else
lcount = lcount + 1;
Xl(:, lcount) = x + d * (mod(2 * ii + jj, 2 * m));
Yl(:, lcount) = y + 1.5 * jj;
% Xlcore(:, lcount) = Xl(:, lcount)-R1core(:, lcount).*x/5; %Problem with this line
% Ylcore(:, lcount) = Yl(:, lcount)-R1core(:, lcount).*y/5; %Problem with this line
Xlcore(:, lcount) = Xl(:, lcount)-R1core(lcount).*x/5;
Ylcore(:, lcount) = Yl(:, lcount)-R1core(lcount).*y/5;
end
end
end
%figure; hold on
fig_i= figure();
fig_i.Visible='on';
fig_i.Position=[1,500,500,500];
fig_i.PaperUnits='inches';
fig_i.PaperPositionMode='manual';
fig_i.PaperPosition=[0,0,N_grid/150,N_grid/150];
axes_i=axes();
axes_i.Position =[0,0,1,1];
axes_i.Units='inches';
patch(Xh, Yh, 'black', 'EdgeColor', 'None');
patch(Xl, Yl, [0.9999,0.9999,0.9999], 'EdgeColor', 'None');
patch(Xhcore, Yhcore, 'red', 'EdgeColor', 'None');
patch(Xlcore, Ylcore, 'blue', 'EdgeColor', 'None');
axis equal;
xlim([0,N_rod*sqrt(3)]);
ylim([1.5,N_rod*sqrt(3)+1.5]);
axis off;

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!