Home > utils > spiral_layout.m

spiral_layout

PURPOSE ^

Generates a spiral layout on a square grid given a list of arbitrary length

SYNOPSIS ^

function [x,y,idx,item] = spiral_layout(items)

DESCRIPTION ^

 Generates a spiral layout on a square grid given a list of arbitrary length
 Generated are x and y coordinates, assuming that the starting coordinate
 is 0,0 with integer steps in positive and negative directions.

 Currently, only a counterclockwise spiral is generated, with the first
 step to the right from center

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [x,y,idx,item] = spiral_layout(items)
0002 % Generates a spiral layout on a square grid given a list of arbitrary length
0003 % Generated are x and y coordinates, assuming that the starting coordinate
0004 % is 0,0 with integer steps in positive and negative directions.
0005 %
0006 % Currently, only a counterclockwise spiral is generated, with the first
0007 % step to the right from center
0008 
0009 
0010 % 19Nov2016 Petr Janata
0011 
0012 % Determine the number of elements
0013 items = items(:);
0014 nitems = length(items);
0015 
0016 % Generate a square containing the next large square of nitems
0017 dimlen = ceil(sqrt(nitems));
0018 
0019 % Make sure that dimlen is odd
0020 if ~mod(dimlen,2)
0021   dimlen = dimlen + 1;
0022 end
0023 
0024 npos = dimlen^2;
0025 
0026 % Generate x and y axes with the starting position in the center
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   % Assign the current value
0043   x(ipos) = curr_x;
0044   y(ipos) = curr_y;
0045   
0046   % Update curr_x
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   % Update curr_y
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   % Now adjust the direction based on the updated curr_x and curr_y
0061   % This set of if-else statements generates a counterclockwise spiral
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 % for ipos=1:npos
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 % function

Generated on Sun 24-Mar-2019 04:00:51 by m2html © 2003