Centering a rectangle using FillRect

8 次查看(过去 30 天)
Steve
Steve 2014-10-10
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 个评论
Steve
Steve 2014-10-10
编辑:Image Analyst 2014-10-11
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 2014-10-11
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.

请先登录,再进行评论。

回答(1 个)

SK
SK 2014-10-10
编辑:SK 2014-10-10
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?

类别

Help CenterFile Exchange 中查找有关 Timing and presenting 2D and 3D stimuli 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by