## How to plot concentric hexagons

### John (view profile)

on 3 May 2015
Latest activity Commented on by amine ouamri

on 3 Nov 2016

### Image Analyst (view profile)

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))

### Image Analyst (view profile)

on 3 May 2015

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');

John

### John (view profile)

on 9 May 2015
Here are the files. But at run-time, there are some other m-files that these depend on to run, do I send them too? I placed the polygon codes between Line 349 ans 404 Thanks
Image Analyst

### Image Analyst (view profile)

on 9 May 2015
It doesn't look like you took my suggestion. All of your plotting is done in the axes called handles.map_axes and you never called axes() like I recommended
axes(handles.axesPlot); % Switch current axes to axesPlot.
Why not? You pass the axes handle in to scatter and you never seem to set it to any other axes than handles.map_axes so of course it always plots in the same axes. If you decide to take my suggestion, then don't pass the axes into scatter(). Or else you don't have to call axes() explictly but if you do it that way, you've got to make sure you change the axes handle you pass in to scatter().
John

### John (view profile)

on 12 Jun 2015
Thank you very much IMAGE ANALYST. I had examinations, so I have been away. I have retried your suggestion and I got it to work. Thank you very much.

on 3 May 2015

If the problem is with the aspect ratio, try ending with
axis equal
If making the hexagons is the issue, you can use circles like this:
circles(1,0,1:10,'vertices',6,'facecolor','none')
which places 10 concentric 6-point 'circles' centered at (1,0).

on 3 Jul 2015
Edited by daniel

### daniel (view profile)

on 3 Jul 2015

% function [Point] = HexCorner(x,y,side,ii)
angle_deg = 60*ii + 30;
end
x = 0;
y = 0;
side = [2:2:12];
for ii = 1:6
points1(ii,:)= HexCorner(x,y,side(1),ii);
points2(ii,:)= HexCorner(x,y,side(2),ii);
points3(ii,:)= HexCorner(x,y,side(3),ii);
points4(ii,:)= HexCorner(x,y,side(4),ii);
points5(ii,:)= HexCorner(x,y,side(5),ii);
points6(ii,:)= HexCorner(x,y,side(6),ii);
end
hold on
grid on
box on
set(gca,'linewidth',3)
patch(points6(:,1),points6(:,2),'b')
patch(points5(:,1),points5(:,2),'g')
patch(points4(:,1),points4(:,2),'y')
patch(points3(:,1),points3(:,2),'r')
patch(points2(:,1),points2(:,2),'m')
patch(points1(:,1),points1(:,2),'w')

### amine ouamri (view profile)

on 30 Oct 2016

I could draw one hexagon, but I can not unscrew the hexagon has three sectors of 120 ° (degrees). Any help would be appreciated.

Image Analyst

### Image Analyst (view profile)

on 30 Oct 2016
What does "unscrew" mean in this context?
amine ouamri

### amine ouamri (view profile)

on 3 Nov 2016
Good morning, Is having three sectors (tri-sectoral)