%function [Lims,MovePtr] = checklims(Editor,Lims,Roots)
function out = checklims(Editor,Lims,Roots)
%CHECKLIMS  Zoom out to 200% if specified set of ROOTS are 
%           dragged near current axis borders.

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.4 $  $Date: 2000/06/14 20:21:48 $

% RE: Lims = [Xmin Xmax -Ymax Ymax]

% Trigger magnification when within 5% of border
TolOut = 0.05;  
TolX = TolOut * (Lims(2)-Lims(1));
TolY = TolOut * (Lims(4)-Lims(3));
Xmin = Lims(1) + TolX;
Xmax = Lims(2) - TolX;
Ymax = Lims(4) - TolY;

% Test if all ROOTS are inside (1+TOLOUT) x current rectangle
rp = real(Roots);
cp = abs(imag(Roots));
if any(rp<Xmin) | any(rp>Xmax) | any(cp>Ymax),
   % Dilate rectangle to 200% of current size
   HalfX = (Lims(2)-Lims(1))/2;
   HalfY = (Lims(4)-Lims(3))/2;
   Lims = Lims + [-HalfX,HalfX,-HalfY,HalfY];
   
   % Update limits
   HG = Editor.HG;
   set(HG.PlotAxes,'Xlim',Lims(1:2),'Ylim',Lims(3:4))
   set(HG.AxisLines(1),'Xdata',Lims(1:2));
   set(HG.AxisLines(2),'Ydata',Lims(3:4));
   MovePtr = 1;
else
   MovePtr = 0;
end

% REVISIT
out = {Lims,MovePtr};