Please help I need to rotate a rectangle

7 次查看(过去 30 天)
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);

回答(3 个)

Mike Garrity
Mike Garrity 2014-10-8
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 2014-10-8
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 个评论
Josyula Gopala Krishna
This doesn't rotate about the center of the given polygon, Can you please help on that? Thanks.

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Image display and manipulation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by