function y = findZoomMag(dir,x)
%findZoomMag Find magnification for zooming in or out.
%    Y = findZoomMag(DIR,X)
%    
%    DIR is 'in' or 'out' for zooming in or zooming out, respectively.
%    
%    Round X up/down to the next element in the sequence:
%    ..., 1/4, 1/3, 1/2, 2/3, 1, 2, 4, 8, ...
%    
%    X and Y are in natural units. Multiply by 100 to convert to
%    percentage. 
%    
%    For example, if X = 2, that's equivalent to doubling the magnification,
%    also described as 200% magnification. And, if X = .6, that's equivalent
%    to reducing the magnification by 60%.

%   Copyright 1993-2004 The MathWorks, Inc.  
%   $Revision: 1.1.8.1 $  $Date: 2004/08/10 01:50:07 $

zoomingIn = strcmp(dir,'in');

log2_x = log(x) / log(2.);

% find next integer powers of 2 even if you're on an integer power of 2 
power_of_2_larger  = 2^(floor(log2_x) + 1);
power_of_2_smaller = 2^(ceil( log2_x) - 1);

if (x > 1) 

    % Stay on integer power of 2    

    if zoomingIn
        y = power_of_2_larger;
    else
        y = power_of_2_smaller;    
    end

elseif (x < 1)

    % Be finer grained than integer powers of 2 (1, 1/2, 1/4, ...). 
    %
    % So "in between" each pair of power of two mags, we find another mag.
    
    if zoomingIn
        in_between_mag = power_of_2_larger * 2/3;
    
        if (x < in_between_mag)
            y = in_between_mag;
        else
            y = power_of_2_larger;
        end
    else
        in_between_mag = power_of_2_smaller * 4/3;        

        if (x > in_between_mag)
            y = in_between_mag;
        else
            y = power_of_2_smaller;
        end
    end
        
else % (x == 1)
    
    if zoomingIn
        y = 2;
    else
        y = 2/3;
    end
    
end

