How to rotate a rectangle

조회 수: 62 (최근 30일)
Nathan Batta
Nathan Batta 2020년 2월 18일
편집: xingxingcui 2024년 4월 27일
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!

답변 (3개)

darova
darova 2020년 2월 18일
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
darova
darova 2020년 2월 19일
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 2020년 2월 19일
편집: darova 2020년 2월 19일
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

댓글을 달려면 로그인하십시오.


Nathan Batta
Nathan Batta 2020년 2월 19일
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!

xingxingcui
xingxingcui 2022년 3월 5일
편집: xingxingcui 2024년 4월 27일
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

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by