File: AlignRect.m

package info (click to toggle)
psychtoolbox-3 3.0.14.20170103%2Bgit6-g605ff5c.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 103,044 kB
  • ctags: 69,483
  • sloc: ansic: 167,371; cpp: 11,232; objc: 4,708; sh: 1,875; python: 383; php: 344; makefile: 207; java: 113
file content (48 lines) | stat: -rwxr-xr-x 1,622 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function rect=AlignRect(rect,fixedRect,side1,side2)
% rect=AlignRect(rect,fixedRect,side1,[side2])
%
% Moves rect to align its center/top/bottom/left/right with the
% corresponding edge(s)/point of fixedRect. Does "side1" and then "side2".
% The legal values for side1 and side2 are 'center', 'left', 'right',
% 'top', and 'bottom'. 
%      r=AlignRect(r,screenRect,'center','top');
% For backward compatibility, also accepts 1,2,3,4,5. You may use the
% pre-defined constants RectLeft,RectRight,RectTop,RectBottom, but there is
% no named constant for centering.
% Also see PsychRects.

% Denis Pelli 5/27/96, 7/10/96, 8/5/96, 11/8/06

if nargin<3
    error('Usage:  rect=AlignRect(rect,fixedRect,side1,[side2])');
end
if size(rect,2)~=4 || size(fixedRect,2)~=4
    error('Wrong size rect arguments. Usage:  rect=AlignRect(rect,fixedRect,side1,[side2])');
end
side{1}=side1;
if nargin>3
    side{2}=side2;
end
for i=1:length(side)
    if isnumeric(side{i})
        if ~ismember(side{i},1:5)
            error('Illegal side1 or side2 value.');
        end
    elseif ischar(side{i})
        [ok,n]=ismember(side{i},{'left','top','right','bottom','center'});
        if ~ok
            error('Illegal side1 or side2 value.');
        end
        side{i}=n;
    else
        error('Illegal side1 or side2 value.');
    end
    switch side{i}
        case 5
            rect=CenterRect(rect,fixedRect);
        case {RectLeft,RectRight},
            rect=OffsetRect(rect,fixedRect(side{i})-rect(side{i}),0);
        case {RectTop,RectBottom},
            rect=OffsetRect(rect,0,fixedRect(side{i})-rect(side{i}));
    end
end