function [varargout] = newfigure();
% This function is a wrapper around the function figure. It creates a
% new figure in a tiled fashion : top left, top right, bottom left,
% bottom right and then looping the same way upto 8 figures. Any more
% figures are then just generated using the default figure command. It
% returns the handle of the figure it creates, if an output argument is
% asked for. 
% 
% Vivek Nadkarni
% Trimble Navigation Limited
% created on November 23, 1998
% List of Modifications
% January 14, 1999
% Bug in default figure command fixed. Modified to accept 1280x1024
% screens as well as the original 1600x1200 screens. 

root = 0;
screensize = get(root,'screensize');

if (screensize == [1 1 1600 1200])

  figsize = [672 504];

  topleft = [10 655];
  topright = [800 655];
  bottomleft = [10 55];
  bottomright = [800 55];
  
  % Note topleft2 = topleft + [50 -40]. So later we may want to set
  % figures modulo 4 by adding the corresponding numbers to the initial
  % coordinates. This can continue beyond 8 figures, until we have gotton
  % down so far that the TOP of the lower figures is below the
  % screen. For now leave it like this. 
  topleft2 = [60 615];
  topright2 = [850 615];
  bottomleft2 = [60 15];
  bottomright2 = [850 15];

elseif (screensize == [1 1 1280 1024])

  figsize = [560 420];

  topleft = [20 560];
  topright = [640 560];
  bottomleft = [20 30];
  bottomright = [640 30];
  
  % Note topleft2 = topleft + [50 -40]. So later we may want to set
  % figures modulo 4 by adding the corresponding numbers to the initial
  % coordinates. This can continue beyond 8 figures, until we have gotton
  % down so far that the TOP of the lower figures is below the
  % screen. For now leave it like this. 
  
  shift = [25 -40];
  
  topleft2 = topleft+shift;
  topright2 = topright+shift;
  bottomleft2 = bottomleft+shift;
  bottomright2 = bottomright+shift;

else

  % If screensize is not either 1200x1600 or 1280x1024, then just generate
  % figures in default location.
  handle = figure;
  return;
  
end;



num_figures = size(get(root,'children'),1);
position = num_figures + 1;

if (position>8) 
  position = mod(position,4);
end


switch position
  case 1
    handle = figure('Position', [topleft figsize]);    
  case 2
    handle = figure('Position', [topright figsize]);    
  case 3
    handle = figure('Position', [bottomleft figsize]);    
  case {0,4}
    handle = figure('Position', [bottomright figsize]);    
  case 5
    handle = figure('Position', [topleft2 figsize]);    
  case 6
    handle = figure('Position', [topright2 figsize]);    
  case 7
    handle = figure('Position', [bottomleft2 figsize]);    
  case 8
    handle = figure('Position', [bottomright2 figsize]);    
  otherwise
    handle = figure;
end

zoom on;

if (nargout)
  varargout(1) = {handle};
end;
  