How to rotate a rectangle

186 visualizaciones (últimos 30 días)
Nathan Batta
Nathan Batta el 18 de Feb. de 2020
Respondida: cui,xingxing el 5 de Mzo. de 2022
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!

Respuestas (3)

darova
darova el 18 de Feb. de 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 comentarios
darova
darova el 19 de Feb. de 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 el 19 de Feb. de 2020
Editada: darova el 19 de Feb. de 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

Iniciar sesión para comentar.


Nathan Batta
Nathan Batta el 19 de Feb. de 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 el 5 de Mzo. de 2022
I recommend you to use 'polyshape' for rotating rectangles, there are many object functions that can meet your requirements.

Categorías

Más información sobre Camera Views en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by