Centering a rectangle using FillRect

8 views (last 30 days)
Steve
Steve on 10 Oct 2014
Commented: Image Analyst on 11 Oct 2014
I need to center the rectangle drawn but it is in the top left corner. Where do I put the coordinates to make the rectangle in the center?
% Displays a horizontal or vertical bar at random and asks for the
% orientation of the bar
% Color variables
blackCol = [127 127 127]; % background screen
greenCol = [0 300 0];
whiteCol = [500 500 500];
screenPixWidth = 1280; %Screen Dimensions
screenPixHeight = 1024;
monitorHeight = 19.5; % inches
monitorWidth = 14.3;
visualAngle= 1; %desired visual angle
distance= 24; %distance from the screen subject is in inches
% solve for height of the rectangle given the visual angle and dist. from
% screen
theta= (visualAngle/180)*pi;
h = 2*distance*tan(theta/2);
heightppi = screenPixHeight/monitorHeight; %calculate ppi
widthppi = screenPixWidth/monitorWidth;
rectangleHeight = heightppi*h;
rectangleWidth = widthppi * (h/2);
height = ceil(rectangleHeight);
width = ceil(rectangleWidth);
recta = [0 0 width height];
whichScreen = 0;
window = Screen(whichScreen, 'OpenWindow', blackCol);
Screen('FillRect', window, greenCol, recta);
Screen('Flip',window,0,0);
WaitSecs(1);
Screen('CloseAll');
  3 Comments
Steve
Steve on 10 Oct 2014
Edited: Image Analyst on 11 Oct 2014
I am using psychtoolbox and sorry for the bad format I will use it next time. I am new to all of this.
Image Analyst
Image Analyst on 11 Oct 2014
OK Steve, I thought I'd fix it for you this time, so I formatted your code for you. I also found out that psychtoolbox is not a MATLAB product but a third party product, and I put a link for it in in your message. No one else who posted any questions on it put links to it either, that I could find after looking at a bunch so I tracked it down myself and put it in there in case anyone else wants to check it out. Basically you can put an angle bracket, then the URL, then some words you want the link to say, then a closing angle bracket, like < URL bluewords text > and that will create the link.

Sign in to comment.

Answers (1)

SK
SK on 10 Oct 2014
Edited: SK on 10 Oct 2014
You know the screen pixel dimensions. Using this change recta appropriately - its a simple calculation. Right now you have the top-left of recta set to 0,0.
By the way Screen() does not come with any official Matlab product. Are you aware that you are using psychtoolbox, a third party library?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!