0001 function [x,y,idx,item] = spiral_layout(items)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 items = items(:);
0014 nitems = length(items);
0015
0016
0017 dimlen = ceil(sqrt(nitems));
0018
0019
0020 if ~mod(dimlen,2)
0021 dimlen = dimlen + 1;
0022 end
0023
0024 npos = dimlen^2;
0025
0026
0027 xpos = -(dimlen-1)/2:(dimlen-1)/2;
0028 ypos = xpos;
0029
0030 max_x = 0;
0031 max_y = 0;
0032 min_x = 0;
0033 min_y = 0;
0034 curr_x = 0;
0035 curr_y = 0;
0036 xdir = 'inc';
0037 ydir = 'same';
0038 x = nan(npos,1);
0039 y = nan(npos,1);
0040
0041 for ipos = 1:npos
0042
0043 x(ipos) = curr_x;
0044 y(ipos) = curr_y;
0045
0046
0047 if strcmp(xdir,'inc')
0048 curr_x = curr_x + 1;
0049 elseif strcmp(xdir, 'dec')
0050 curr_x = curr_x - 1;
0051 end
0052
0053
0054 if strcmp(ydir, 'inc')
0055 curr_y = curr_y + 1;
0056 elseif strcmp(ydir, 'dec')
0057 curr_y = curr_y - 1;
0058 end
0059
0060
0061
0062 if strcmp(xdir, 'inc') && curr_x > max_x
0063 xdir = 'same';
0064 ydir = 'inc';
0065 max_x = curr_x;
0066 elseif strcmp(ydir, 'inc') && curr_y > max_y
0067 ydir = 'same';
0068 xdir = 'dec';
0069 max_y = curr_y;
0070 elseif strcmp(xdir, 'dec') && curr_x < min_x
0071 xdir = 'same';
0072 ydir = 'dec';
0073 min_x = curr_x;
0074 elseif strcmp(ydir, 'dec') && curr_y < min_y
0075 xdir = 'inc';
0076 ydir = 'same';
0077 min_y = curr_y;
0078 end
0079
0080 end
0081
0082 idx = (1:npos)';
0083
0084 item = items;
0085
0086 if isnumeric(item)
0087 item(end+1:npos) = NaN;
0088 elseif iscell(item)
0089 item(end+1:npos) = {''};
0090 end
0091
0092 end