quirk with OffsetRect.m

Ran into a little quirk with OffsetRect.m

If you pass it more than one rect, but less than 4, it got confused
and crashed. It was differentiating one rect input from multiple rects
input with a lenght(rect) call but that doesn't trigger if you sent 2
to 4 rects.

just switched it to always treat as multiple and address the correct
orientation. ...not sure if this is a speed faux pas, but it doesn't
crash now...

-Thad.

Here's the corrected version:


function newRect = OffsetRect(oldRect,x,y)
% newRect = OffsetRect(oldRect,x,y)
%
% Offset the passed rect matrix by the horizontal (x)
% and vertical (y) shift given.
% Also see PsychRects.

% 5/16/96 dhb Relented to Pelli's request to change calling order
% from v,h to x,y.
% dhb Index using RectTop etc.
% 7/10/96 dgp Wrote it.
% 8/5/96 dgp Check rect size.
% 5/18/08 mk Vectorized.

if nargin~=3
error('Usage: newRect = OffsetRect(oldRect,x,y)');
end

if size(oldRect, 1)==4
newRect(RectTop, :) = oldRect(RectTop, :) + y;
newRect(RectBottom, :) = oldRect(RectBottom, :) + y;
newRect(RectLeft, :) = oldRect(RectLeft, :) + x;
newRect(RectRight, :) = oldRect(RectRight, :) + x;
elseif size(oldRect, 2)==4
newRect(:, RectTop) = oldRect(:, RectTop) + y;
newRect(:, RectBottom) = oldRect(:, RectBottom) + y;
newRect(:, RectLeft) = oldRect(:, RectLeft) + x;
newRect(:, RectRight) = oldRect(:, RectRight) + x;
else
error('Wrong size rect argument. Usage: newRect =
OffsetRect(oldRect,x,y)');
end
Oops, solved in revision 1245.

...updatepsychtoolbox is your friend.

-Thad.

--- In psychtoolbox@yahoogroups.com, "Thad Czuba" <tbc.class@...> wrote:
>
> Ran into a little quirk with OffsetRect.m
>
> If you pass it more than one rect, but less than 4, it got confused
> and crashed. It was differentiating one rect input from multiple rects
> input with a lenght(rect) call but that doesn't trigger if you sent 2
> to 4 rects.
>
> just switched it to always treat as multiple and address the correct
> orientation. ...not sure if this is a speed faux pas, but it doesn't
> crash now...
>
> -Thad.
>
> Here's the corrected version:
>
>
> function newRect = OffsetRect(oldRect,x,y)
> % newRect = OffsetRect(oldRect,x,y)
> %
> % Offset the passed rect matrix by the horizontal (x)
> % and vertical (y) shift given.
> % Also see PsychRects.
>
> % 5/16/96 dhb Relented to Pelli's request to change calling order
> % from v,h to x,y.
> % dhb Index using RectTop etc.
> % 7/10/96 dgp Wrote it.
> % 8/5/96 dgp Check rect size.
> % 5/18/08 mk Vectorized.
>
> if nargin~=3
> error('Usage: newRect = OffsetRect(oldRect,x,y)');
> end
>
> if size(oldRect, 1)==4
> newRect(RectTop, :) = oldRect(RectTop, :) + y;
> newRect(RectBottom, :) = oldRect(RectBottom, :) + y;
> newRect(RectLeft, :) = oldRect(RectLeft, :) + x;
> newRect(RectRight, :) = oldRect(RectRight, :) + x;
> elseif size(oldRect, 2)==4
> newRect(:, RectTop) = oldRect(:, RectTop) + y;
> newRect(:, RectBottom) = oldRect(:, RectBottom) + y;
> newRect(:, RectLeft) = oldRect(:, RectLeft) + x;
> newRect(:, RectRight) = oldRect(:, RectRight) + x;
> else
> error('Wrong size rect argument. Usage: newRect =
> OffsetRect(oldRect,x,y)');
> end
>