MATLAB: How to plot concentric hexagons

hexagonplotspolygons

I tried to use the code below to plot 2 concentric hexagons but I am having problems with their width and more problems when I try to increase the hexagons to three. Any help will be appreciated.
clc
clear all
scale=4;
scale0=5;
L = linspace(0,2.*pi,7);
% N_sides = 6;
% L=(1/(N_sides*2):1/N_sides:1)';
% L=L*2*pi;
% L1=L;
xv = cos(L)'; xz = cos(L)';
yv = sin(L)'; yz = sin(L)';
xv=scale*[xv; xv(1)]; xz =scale0*[xz; xz(1)];
yv=scale*[yv; yv(1)]; yz =scale0*[yz; yz(1)];
% xv = [xv ; xv(1)]; yv = [yv ; yv(1)];
% xz = [xz ; xz(1)]; yz = [yz ; yz(1)];
x = rand(50); y = rand(50);
v = rand(20); w = rand(20);
in = inpolygon(x,y,xv,yv);
inz = inpolygon(v,w,xz,yz);
figure
plot(xv,yv,x(in),y(in),'r+',x(~in),y(~in),'bo')
hold
plot(xz,yz,v(inz),w(inz),'b+',v(~inz),w(~inz),'ro')
A=numel(y(in)), b=numel (x(~in))
C=numel(v(inz)), d=numel (w(~inz))

Best Answer

This is what I would do to identify the outermost polygon your random point is in. Have two for loops, one over random points and then one over polygons, where you work from inside out. Then, once you find the point is inside a particular hexagon, record that hexagon number and then break out of the inner loop. Here's a demo, where I color code each point according to what hexagon it is in:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
angles = linspace(0, 360, 7);
radii = [10, 20, 30, 40];
% First create and draw the hexagons.
numberOfHexagons = 4;
% Define x and y arrays. Each row is one hexagon.
% Columns are the vertices.
x = zeros(numberOfHexagons, 7);
y = zeros(numberOfHexagons, 7);
for h = 1 : numberOfHexagons
x(h, :) = radii(h) * cosd(angles); % Assign x coordinates
y(h, :) = radii(h) * sind(angles); % Assign y coordinates
plot(x(h, :), y(h, :), 'b-', 'LineWidth', 2);
hold on
end
axis equal
grid on;
% Set up the number of random points.
numberOfPoints = 200;
% Make random points
xp = 60 * rand(1, numberOfPoints)- 30;
yp = 60 * rand(1, numberOfPoints) - 30;
% scatter(xp, yp);
% Give each point a different color
cmap = jet(numberOfHexagons);
% Make an array to record what hexagon each point is in.
% Values will be 0 (if not in any hexagon) up to numberOfHexagons;
hexagonPointIsIn = zeros(1, numberOfPoints);
for p = 1 : numberOfPoints
% Scan hexagons from smallest to largest.
for h = 1 : numberOfHexagons
this_x = x(h, :); % Extract x coordinates
this_y = y(h, :); % Extract y coordinates
if inpolygon(xp(p), yp(p), this_x, this_y)
fprintf('Point #%d, (%.1f, %.1f), is in polygon #%d\n', ...
p, xp(p), yp(p), h);
% Plot it in it's own unique color
plot(xp(p), yp(p), '.', ...
'MarkerSize', 30, 'Color', cmap(h,:));
hold on;
% Log where this point was found.
hexagonPointIsIn(p) = h;
% We can skip the remaining hexagons.
break;
end
% If it's not in any hexagon, plot it in black.
if hexagonPointIsIn(p) == 0
plot(xp(p), yp(p), '.', ...
'MarkerSize', 30, 'Color', 'k');
end
end
end
fprintf('Done with demo!\n');