How to rotate a rectangle

130 views (last 30 days)
Nathan Batta
Nathan Batta on 18 Feb 2020
Edited: cui,xingxing on 27 Apr 2024
Hello!
I am working on modeling a dynamic suspension system and I am trying to create an animation for it. I have the translation components completed and working fine but I need to rotate a rectangle and for some reason it is not working.
I used the below code to create the rectangle and rotate it 45 degrees about the z axis:
rotate(rectangle('Position',[x_left,xc-height/2+2*mass_height_diff,w_chassis+1,height],'EdgeColor','black'),[0 0 1],45)
However, running the code shows the rectangle moving up and down as it should but not rotating at all. Does anyone know what the issue is here? Thank you!

Answers (3)

darova
darova on 18 Feb 2020
If you open (Ctrl+D) rotate function you will find these lines at the end:
if strcmp(t,'surface') || strcmp(t,'line')
set(h(i),'xdata',newx,'ydata',newy,'zdata',newz);
elseif strcmp(t,'patch')
set(h(i),'Vertices',[newx,newy,newz]);
elseif strcmp(t,'text')
set(h(i),'position',[newx newy newz])
elseif strcmp(t,'image')
set(h(i),'xdata',newx,'ydata',newy)
end
When you draw a rectangle
h = rectangle('Position',[x_left,xc-height/2+2*mass_height_diff,w_chassis+1,height],'EdgeColor','red');
get(h,'type')
ans =
rectangle
As you can see there is no case for rectangle. Maybe that is way rotate didn't work
So i used simple plot
x_left = 3;
xc = 1;
height = 2;
mass_height_diff = 5;
w_chassis = 1;
height = 5;
x = [x_left
w_chassis+1];
y = [xc-height/2+2*mass_height_diff
height];
h = plot([x(1) x(2) x(2) x(1) x(1)],...
[y(1) y(1) y(2) y(2) y(1)]);
rotate(h,[0 0 1],45)
  5 Comments
darova
darova on 19 Feb 2020
What happens if you try
h = plot([x(1) x(2) x(2) x(1) x(1)],...
[y(1) y(1) y(2) y(2) y(1)]);
h1 = get(h,'children');
rotate(h1,[0 0 1],45)
darova
darova on 19 Feb 2020
Edited: darova on 19 Feb 2020
Maybe rotate manually?
x = [x_left
w_chassis+1];
y = [xc-height/2+2*mass_height_diff
height];
x = x([1 2 2 1 1]);
y = y([1 1 2 2 1]);
a = 15;
R = [cosd(a) -sind(a);sind(a) cosd(a)]; % rotation matrix
v = R*[x(:)-mean(x) y(:)-mean(y)]'; % center and rotate rectangle
x = v(1,:)+mean(x); % restore original position
y = v(2,:)+mean(y);
plot(x,y,'k');
EDITED: rotation matrix

Sign in to comment.


Nathan Batta
Nathan Batta on 19 Feb 2020
Update:
I was able to get it to work using the following code:
g=hgtransform;
r=rectangle('Parent',g,'Position',[x_left,xc+2*mass_height_diff,w_chassis+1,height],'EdgeColor','black','LineWidth',.75);
g.Matrix=makehgtform('zrotate',theta);
However, is there a way to get it to rotate about the center of the rectangle? Right now it is rotating about the bottom left corner I believe. Thank you!

cui,xingxing
cui,xingxing on 5 Mar 2022
Edited: cui,xingxing on 27 Apr 2024
I recommend you to use 'polyshape' for rotating rectangles, there are many object functions that can meet your requirements.
-------------------------Off-topic interlude, 2024-------------------------------
I am currently looking for a job in the field of CV algorithm development, based in Shenzhen, Guangdong, China,or a remote support position. I would be very grateful if anyone is willing to offer me a job or make a recommendation. My preliminary resume can be found at: https://cuixing158.github.io/about/ . Thank you!
Email: cuixingxing150@gmail.com

Categories

Find more on Bounding Regions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!