![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/272447/image.png)
How to rotate a rectangle
89 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
回答 (3 件)
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/272447/image.png)
5 件のコメント
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
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
cui,xingxing
2022 年 3 月 5 日
編集済み: cui,xingxing
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Camera Views についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!