function new_pos = constrainPositionRect(pos, im_width, im_height)
%constrainPositionRect Constrain position rectangle to image area.
%   new_pos = constrainPositionRect(pos, im_width, im_height) constrains a
%   position vector, pos, to lie within the rectangle covered by a
%   im_width-by-im_height image.  The position vector has the form [x_min,
%   y_min, width, height].  The output argument NEW_POS is the constrained
%   position vector.
%
%   constrainPositionRect assumes the image has default spatial coordinates
%   and covers the range [0.5, im_width+0.5] along the x-axis and the range
%   [0.5, im_height+0.5] along the y-axis.  constrainPositionRect
%   modifies the position vector only by translating it (changing x_min
%   or y_min), so if pos is too wide or high to fit inside the image area
%   then new_pos will also be too wide or high.
%
%   constrainPositionRect assumes the input arguments are correctly
%   formed and does no error checking on them.

%   SLE
%   $Revision: 1.1.6.1 $  $Date: 2004/10/20 17:55:18 $
%   Copyright The MathWorks, Inc. 2004

x_min = pos(1);
y_min = pos(2);
w     = pos(3);
h     = pos(4);

x_min = min( im_width  + 0.5 - w, max(x_min, 0.5) );
y_min = min( im_height + 0.5 - h, max(y_min, 0.5) );

new_pos = [x_min y_min w h];
