30 views (last 30 days)

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

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

Sign in to comment.

Chad Greene
on 3 May 2015

If the problem is with the aspect ratio, try ending with

axis equal

circles(1,0,1:10,'vertices',6,'facecolor','none')

which places 10 concentric 6-point 'circles' centered at (1,0).

Sign in to comment.

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

Sign in to comment.

amine ouamri
on 30 Oct 2016

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.