You are on page 1of 4

http://www.iterativesolutions.com/download.

htm
http://www.iterativesolutions.com/download.htm
http://www.iterativesolutions.com/download.htm
http://www.iterativesolutions.com/download.htm

I assume you mean that 1's are to lie both on and in the interior of a
hexagon. If so, let (x0,y0) be the center of the regular hexagon and d
the distance from the center to the midpoint of each of its sides. Let
the image matrix be n x m in size. Then do this:
[x,y] = meshgrid([1:m],[1:n]);
A = (abs(y-y0) <= d) & ...
(abs(sqrt(3)/2*(x-x0)+1/2*(y-y0)) <= d) & ...
(abs(sqrt(3)/2*(x-x0)-1/2*(y-y0)) <= d);
Malek
Here is the code I have been using:
% Define the unit hexagon using angles
t=(0:1/6:1)*2*pi;
% Define the center of the hexagons
alpha=[0 2*pi/3 4*pi/3];
x=cos(alpha);
y=sin(alpha);
% Values to plot on each hexagon
v=[100 200 1000];
% Define the colormap (114 is just an example)
c=colormap(jet(114));
% Map the colors to the range of values contained in the kpi vector
xmin=min(v);
xmax=max(v);
slope=113/(xmax-xmin);
offset=114-slope*xmax;
indexes=max(1,floor(offset+slope*v));
fig=figure;
set(fig,'NumberTitle','off','Name','Network Area by MBC');
hold on
grid on
box on
for i=1:3
fill(x(i)+cos(t),y(i)+sin(t),c(indexes(i),:));
end
xlabel('\bfX [m]')
ylabel('\bfY [m]')
title('\bfMap of the KPI');
colorbar(c)
hold off;
% axis([xmin xmax])

% create a hexagonal grid (radius <r>)


clf;
r=2;
c=r*sqrt(3);
c2=c/2;
v=30:60:390;
cv=r*cosd(v);
sv=r*sind(v);
line(0*c+cv,0*c+sv,'tag','h');
for f=[1,2] % we test <f>!
for a=0:60:360
line(f*c*cosd(a)+...
cv,f*c*sind(a)+sv);
end
if f==2
for a=30:60:360
line(c2*c*cosd(a)+...
cv,c2*c*sind(a)+sv);
end
end
end
axis equal;
xl=get(gca,'xlim');
set(gca,'xlim',...
[xl(1)-.1*range(xl),xl(2)+.1*range(xl)]);
% compute hull from plotted hexagon vertices
lh=findall(gca,'type','line');
x=get(lh,'xdata');
x=cat(1,x{:});
x=x(:);
y=get(lh,'ydata');
y=cat(1,y{:});
y=y(:);
xy=unique([x,y],'rows');
x=xy(:,1);
y=xy(:,2);
xx=[];
yy=[];
for i=1:3
p=convhull(x,y);
xx=[xx;x(p)]; % quick and dirty!

yy=[yy;y(p)];
x(p)=[];
y(p)=[];
end
at=atan2(xx,yy);
[ax,ax]=sort(at);
xx=xx(ax);
yy=yy(ax);
line(xx,yy,...
'marker','s',...
'color',[0,0,0]);
% test some data points
px=20*rand(500,1)-10;
py=20*rand(500,1)-10;
line(px,py,...
'marker','.',...
'markersize',4,...
'linestyle','none');
ix=inpolygon(px,py,xx,yy);
line(px(ix),py(ix),...
'marker','.',...
'linestyle','none',...
'color',[1,0,0]);

function heglat(nr,nc,r)
% help
% error checking
% preliminaries
v=30:60:390;
cv=r*cosd(v);
sv=r*sind(v);
% ...the center points
[x,y]=meshgrid(1:nc,1:nr);
cx=r*sqrt(3);
x=cx*x;
x(2:2:end,:)=.5*cx+x(2:2:end,:);
y=y*(r+.5*r);
for i=1:nc
for j=1:nr
line(x(j,i)+cv,y(j,i)+sv);
end
end

% axis equal;

http://www.mathworks.com/matlabcentral/newsreader/view_thread/138624

You might also like