Created
October 8, 2014 13:43
-
-
Save smithdanielle/e297ef9452f93d6aea24 to your computer and use it in GitHub Desktop.
A MATLAB script to generate hexmaps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [h,xy]=hextile(rect,size) | |
% hextile(rect,size) | |
% rect - [x y w h] | |
% size - distance between hexes (default 1) | |
% returns | |
% h - plot handle | |
% xy - centers of all hexagons drawn | |
[x,y,w,h] = deal(rect(1),rect(2),rect(3),rect(4)); | |
if nargin < 2, size = 1; end | |
[c,s]=deal(cos(pi/6),sin(pi/6)); | |
xlist = 0:size*(2+2*s):w; % anticipated x centers | |
ylist = 0:size*(c):h; % anticipated y centers | |
center_x = repmat(xlist,length(ylist),1)'; | |
center_y = repmat(ylist,length(xlist),1); | |
center_x(:,2:2:end) = ... | |
center_x(:,2:2:end)+size*(1+s); % Shift odd rows | |
f = find(center_x(:) <= w); % find outlying hexes | |
center_x = center_x(f); % remove outlying hexes | |
center_y = center_y(f); % remove outlying hexes | |
% Define corners of a single hexagon | |
x0=size*(cumsum([0,s,1,s, -s,-1,-s,NaN])-s-0.5); | |
y0=size*[0,c,c,0,-c,-c,0,NaN]; % full hex | |
% Replicate to the centers | |
xplot=log(exp(x0')*exp(center_x)'); | |
yplot=log(exp(y0')*exp(center_y)'); | |
h=plot(x+xplot(:),y+yplot(:)); | |
if nargout >= 2 | |
xy = [x+center_x(:),y+center_y(:)]; | |
end | |
axis equal | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment