www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/+mbcgui/+util/clipRect.m

    function rect = clipRect(rect, clip)
%CLIPRECT Clip a position rectangle to allowable bounds
%
%  RECT = CLIPRECT(RECT, CLIP) clips the standard Matlab position rectangle
%  RECT so that it is within the rectangle specified in CLIP and has a
%  height and width of >=1.
%
%  RECT = CLIPRECT(RECT) just ensures that the specified position
%  rectangle has a height and width >=1.

%  Copyright 2000-2010 The MathWorks, Inc. and Ford Global Technologies, Inc.


if nargin==1
    if rect(3)<1
        rect(3) = 1;
    end
    if rect(4)<1
        rect(4) = 1;
    end
else
    % This scalar routine is JITted  to be faster than a vectorized method
    % using max and min
    xMax = clip(1) + clip(3);
    yMax = clip(2) + clip(4);
    if rect(1) < clip(1)
        rect(1) = clip(1);
    elseif rect(1) >= xMax
        rect(1) = xMax - 1;
    end
    
    if rect(2) < clip(2)
        rect(2) = clip(2);
    elseif rect(2) >= yMax
        rect(2) = yMax - 1;
    end
    
    if rect(3) < 1
        rect(3) = 1;
    elseif (rect(1) + rect(3)) > xMax
        rect(3) = xMax - rect(1);
    end
    
    if rect(4) < 1
        rect(4) = 1;
    elseif (rect(2) + rect(4)) > yMax
        rect(4) = yMax - rect(2);
    end
end