function color = tiling(fct,par)

% function color = tiling(fct,par)
% tiling determined by a generating function 
% fct: inline function of two variables 
%      should grow at infinity
% par.window: size of display window
% par.shift: maximum size of integer shifts used
%            should be large for good accuracy
% color: distiguishes different tiles

% set defaults
if nargin < 2; 
   par.window = 1.25;
   par.shift = 8;
   if nargin < 1; 
      fct = inline('(2*x+y).*(x+2*y)','x','y');
   end;
end;

% define grid and initialize height and color of tile
xy = linspace(-par.window, par.window, 256);
[x0,y0] = meshgrid(xy); 
value = inf*ones(size(x0));
color = zeros(size(x0));

% minimal shifted function determines color of tile
% central tile: f(x,y) <= f(x+j,y+k) for all j,k 
for j=-par.shift:par.shift;
   for k=-par.shift:par.shift;
      x = x0+j; y = y0+k;
      vjk = abs(feval(fct,x,y));
      color(vjk <= value) = fix(par.shift*rand(1));
      value = min(value,vjk);
   end;
end;

% plotting tiles with different colors
contourf(x0,y0,color,[-1/2:1:par.shift]);
colormap(hsv); axis equal; axis off; 

