HexagonLatticePosit​ion(ring,separation​,center,key)

Version 1.0.0.0 (1.46 KB) by Long Wang
Generate the centers of the regular hexagonal lattice
169 Downloads
Updated 26 Jul 2015

View License

%HexagonLatticePosition(ring,separation,center,key) generates the x and y
%coordinates for a number rings of hexagon.
% Inputs:
% ring: the number of rings. Note, the center point is regarded as the
% first ring. Thus the number for ring has to equal or larger than 1.
% separation: the separation between each two adjacent points;
% center: the center coordinates for the hexgonal window
% key: to include the center point or not. 1: yes, 0: no.
% Detailed explanation goes here
h=sqrt(3)/2*separation; % the height for the unit triangle
row=2*ring-1; % how many rows of data
xdata=cell(1,row);
ydata=cell(1,row);
for ii=1:ring
vector_size=ring+ii-1;
if ii<ring
xx=[0:vector_size-1]*separation-(vector_size-1)*separation/2-center(1);
yy=(ring-ii)*h*ones(1,vector_size)-center(2);
xdata{ii}=xx;
xdata{row+1-ii}=xx;
ydata{ii}=yy;
ydata{row+1-ii}=-1*yy;
else
xx=[0:vector_size-1]*separation-(vector_size-1)*separation/2-center(1);
yy=(ring-ii)*h*ones(1,vector_size)-center(2);
xdata{ii}=xx;
ydata{ii}=yy;
end
end
xxx=[];
yyy=[];
for ii=1:row;
xxx=[xxx,xdata{ii}];
yyy=[yyy,ydata{ii}];
end
if key ==0
middle=(length(xxx)+1)/2;
x1=xxx(1:middle-1);
y1=yyy(1:middle-1);
x2=xxx(middle+1:end);
y2=yyy(middle+1:end);
xxx=[x1,x2];
yyy=[y1,y2];
end

Cite As

Long Wang (2024). HexagonLatticePosition(ring,separation,center,key) (https://www.mathworks.com/matlabcentral/fileexchange/52280-hexagonlatticeposition-ring-separation-center-key), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Condensed Matter & Materials Physics in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0.0