0001 function varargout = hexagonalGrid(bounds, origin, size, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 size = size(1);
0022 dx = 3*size;
0023 dy = size*sqrt(3);
0024
0025
0026 pts1 = squareGrid(bounds, origin + [0 0], [dx dy], varargin{:});
0027 pts2 = squareGrid(bounds, origin + [dx/3 0], [dx dy], varargin{:});
0028 pts3 = squareGrid(bounds, origin + [dx/2 dy/2], [dx dy], varargin{:});
0029 pts4 = squareGrid(bounds, origin + [-dx/6 dy/2], [dx dy], varargin{:});
0030
0031
0032 pts = [pts1;pts2;pts3;pts4];
0033
0034
0035
0036
0037 if nargout > 1
0038 edges = zeros([0 4]);
0039 x0 = origin(1);
0040 y0 = origin(2);
0041
0042
0043 x1 = bounds(1) + mod(x0-bounds(1), dx);
0044 x2 = bounds(3) - mod(bounds(3)-x0, dx);
0045 lx = (x1:dx:x2)';
0046
0047
0048 y1 = bounds(2) + mod(y0-bounds(2), dy);
0049 y2 = bounds(4) - mod(bounds(4)-y0, dy);
0050 ly = (y1:dy:y2)';
0051
0052
0053 ny = length(ly);
0054 nx = length(lx);
0055
0056 if bounds(1)-x1+dx < size
0057 disp('intersect bounding box');
0058 end
0059
0060 if bounds(3)-x2 < size
0061 disp('intersect 2');
0062 edges = [edges;repmat(x2, [ny 1]) ly repmat(bounds(3), [ny 1]) ly];
0063 x2 = x2-dx;
0064 lx = (x1:dx:x2)';
0065 nx = length(lx);
0066 end
0067
0068 for i = 1:length(ly)
0069 ind = (1:nx)';
0070 tmpEdges = zeros(length(ind), 4);
0071 tmpEdges(ind, 1) = lx;
0072 tmpEdges(ind, 2) = ly(i);
0073 tmpEdges(ind, 3) = lx+size;
0074 tmpEdges(ind, 4) = ly(i);
0075 edges = [edges; tmpEdges];
0076 end
0077
0078 end
0079
0080
0081 if nargout > 0
0082 varargout{1} = pts;
0083
0084 if nargout > 1
0085 varargout{2} = edges;
0086 end
0087 end