How to plot concentric hexagons

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

 Respuesta aceptada

Image Analyst
Image Analyst el 3 de Mayo de 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 comentarios

John
John el 5 de Mayo de 2015
You understand what I need, I feel like you are stepping into the code in my head. Thanks a lot. Please, do you know if I can integrate this plot into another GUIDE plot?
I got a beta version of the Mind Reading Toolbox last week, which unfortunately expired May 3 so now I'm back to being a mere mortal. It was nice while it lasted. I hope the official release will be soon.
Yes you can do all that plotting onto another axes in GUIDE. Just activate the other plot with the axes() function
axes(handles.axesPlot);
then do all this stuff with the hexagons. It will appear on that other plot.
John
John el 9 de Mayo de 2015
I can't seem to find my way please. After activating the GUIDE axes, the plot was only stripped of colors but was plotted on the same old axes.There was no difference in the GUIDE plot.
Image Analyst
Image Analyst el 9 de Mayo de 2015
That doesn't make sense. Once you use axes() to change the active axes, the stuff must plot on the axes you changed to. How could it not? Why would it plot to the first axes after you switched away from it? You didn't explicitly specify the axes in the plot() function did you? Otherwise, attach your fig and m files if you can.
John
John el 9 de Mayo de 2015
Editada: John el 9 de Mayo de 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
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 el 12 de Jun. de 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.

Iniciar sesión para comentar.

Más respuestas (4)

Chad Greene
Chad Greene el 3 de Mayo de 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 el 3 de Jul. de 2015
Editada: daniel el 3 de Jul. de 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 el 30 de Oct. de 2016

0 votos

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

3 comentarios

Image Analyst
Image Analyst el 30 de Oct. de 2016
What does "unscrew" mean in this context?
amine ouamri
amine ouamri el 3 de Nov. de 2016
Good morning, Is having three sectors (tri-sectoral)
shivangi  mahajan
shivangi mahajan el 20 de Mayo de 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.

Iniciar sesión para comentar.

Steven Lord
Steven Lord el 20 de Mayo de 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

Categorías

Más información sobre Line Plots en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Mayo de 2015

Respondida:

el 20 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by