function out = rectint(A, B)
%RECTINT Rectangle intersection area.
%  RECTINT(A, B) returns the area of intersection of the
%  rectangles specified by position vectors A and B.  If A 
%  is 1-by-4 and B is m-by-4, the output will be a 1-by-m
%  vector giving the intersection of A with all the
%  rectangles specified by B.  If A is n-by-4 and B is
%  m-by-4, the output will by n-by-m.

%  Steven L. Eddins, October 1994
%  Copyright (c) 1984-98 by The MathWorks, Inc.
%  $Revision: 1.10 $  $Date: 1997/11/21 23:41:01 $

leftA = A(:,1);
bottomA = A(:,2);
rightA = leftA + A(:,3);
topA = bottomA + A(:,4);

leftB = B(:,1)';
bottomB = B(:,2)';
rightB = leftB + B(:,3)';
topB = bottomB + B(:,4)';

numRectA = size(A,1);
numRectB = size(B,1);

leftA = repmat(leftA, 1, numRectB);
bottomA = repmat(bottomA, 1, numRectB);
rightA = repmat(rightA, 1, numRectB);
topA = repmat(topA, 1, numRectB);

leftB = repmat(leftB, numRectA, 1);
bottomB = repmat(bottomB, numRectA, 1);
rightB = repmat(rightB, numRectA, 1);
topB = repmat(topB, numRectA, 1);

out = (max(0, min(rightA, rightB) - max(leftA, leftB))) .* ...
    (max(0, min(topA, topB) - max(bottomA, bottomB)));
