Please help I need to rotate a rectangle

7 views (last 30 days)
This is what I have so far. I know how to draw a rectangle but I'm stuck on how to rotate it. Someone please help me out and show me the code I've been looking for a while. Also it needs to be done using psychtoolbox if possible. Thanks in advance.
screenPixWidth = 1280; %Screen Dimensions
screenPixHeight = 1024;
% Fixation variables
fixCrossLength = 25;
fixCrossWidth = 50;
fix1 = [screenPixWidth/2 - fixCrossWidth/2 , screenPixHeight/2 - fixCrossLength/2 , ...
screenPixWidth/2 + fixCrossWidth/2 , screenPixHeight/2 + fixCrossLength/2 ];
fix2 = [screenPixWidth/2 - fixCrossLength/2 , screenPixHeight/2 - fixCrossWidth/2, ...
screenPixWidth/2 + fixCrossLength/2 , screenPixHeight/2 + fixCrossWidth/2 ];
whichScreen = 0;
window = Screen(whichScreen, 'OpenWindow', [127 127 127]);
Screen('FillRect', window, greenCol, fix1);

Answers (3)

Mike Garrity
Mike Garrity on 8 Oct 2014
In R2014b, you can parent a rectangle object to an hgtransform object and apply a rotation:
g = hgtransform
r = rectangle('Parent',g)
g.Matrix = makehgtform('zrotate',pi/3)
Unfortunately this didn't work correctly in earlier releases of MATLAB. If you're using an earlier version, you need to compute the rotated coordinates yourself and then use the patch command. Something like this:
x = [0 1 1 0]
y = [0 0 1 1]
patch(cos(pi/3)*x - sin(pi/3)*y, ...
sin(pi/3)*x + cos(pi/3)*y, ...
zeros(1,4), ...
'FaceColor','none')

Star Strider
Star Strider on 8 Oct 2014
Just learning the new HG2 system in R2014b, so I kept with the previous calling syntax:
figure(1)
h = plot([0.5 2.1 2.1 0.5 0.5], [0.5 0.5 1.5 1.5 0.5]);
axis([0 4 0 2])
axis equal
for k1 = 1:24
rotate(h, [0 0 1], 0.3*k1)
refreshdata
drawnow
end
  1 Comment
Josyula Gopala Krishna
Josyula Gopala Krishna on 29 Jan 2019
This doesn't rotate about the center of the given polygon, Can you please help on that? Thanks.

Sign in to comment.


Chad Greene
Chad Greene on 8 Oct 2014
I've never used the psychtoolbox, but in plain old Matlab you could plot a rectangle, then rotate it.

Community Treasure Hunt

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

Start Hunting!