How to plot concentric hexagons

9 views (last 30 days)
John
John on 3 May 2015
Answered: Steven Lord on 20 May 2020
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))

Accepted Answer

Image Analyst
Image Analyst 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');
  7 Comments
Image Analyst
Image Analyst 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 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.

Sign in to comment.

More Answers (4)

Chad Greene
Chad Greene 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).

daniel
daniel on 3 Jul 2015
Edited: daniel on 3 Jul 2015
% function [Point] = HexCorner(x,y,side,ii)
angle_deg = 60*ii + 30;
angle_rad = angle_deg*(pi/180);
Point = [x + side*cos(angle_rad),y + side*sin(angle_rad)];
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
amine ouamri 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.
  3 Comments
amine ouamri
amine ouamri on 3 Nov 2016
Good morning, Is having three sectors (tri-sectoral)
shivangi  mahajan
shivangi mahajan on 20 May 2020
hello mam/ sir,
i want to ask that i have made hexagonal so that how i will make sectros in that plss give me the idea about this.

Sign in to comment.


Steven Lord
Steven Lord on 20 May 2020
You can create a "stack" of concentric hexagons using polyshape.
clear X
for R = 6:-1:1
X(R) = nsidedpoly(6, 'Center', [1 2], 'Radius', R);
end
To visualize them, just plot the X vector.
h = plot(X);
You'll note that all the hexagons but the largest appear a bit muted. You can bring one of the hexagons to the "front" or "top" of the picture using uistack.
uistack(h(3), 'top')
Or if you want to see them from smallest to largest just bring each one, starting with the largest, to the top. [The second largest will be displayed "on top of" the largest, the third largest "on top of" the largest and second largest, etc.]
for k = 6:-1:1
uistack(h(k), 'top')
end

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!