How to rotate rectangular with a an angle?
2 次查看(过去 30 天)
显示 更早的评论
Hey Everone,
I would like to rotate a rectangula with a specific angle.
rectangle('Position',[70 130 25 30]);
Any help
Thank you
0 个评论
回答(2 个)
Les Beckham
2025-1-9
编辑:Les Beckham
2025-1-9
Perhaps this is what you are trying to do. The hgtransform and makehgtransform functions are designed for transforming graphics (rotation, scaling, translation).
r = rectangle('Position',[70 130 25 30]);
axis equal
grid on
h = hgtransform;
set(r, 'parent', h)
phi = 25 * pi/180; % angle of rotation
R = makehgtform('zrotate', phi);
h.Matrix = R;
0 个评论
Adam Danz
2025-1-9
编辑:Adam Danz
2025-1-9
MATLAB's polyshape has a rotate function that makes this fairly easy. Instead of the [left, bottom, width, height] input used in rectangle, polyshape input is the coordinates of the vertices.
lbwh = [70 130 25 30]; % rectangle position [left, bottom, width, height]
deg = 45; % degrees
x = [lbwh(1), sum(lbwh([1,3]))]; % x vertices
y = [lbwh(2), sum(lbwh([2,4]))]; % y vertices
rect = polyshape(x([1 2 2 1]), y([1 1 2 2])); % rectangle
You can rotate it around (0,0) or around any coordinate. Here are two examples.
rectRot1 = rotate(rect, deg); % rotates with respect to (0,0)
[xCnt, yCnt] = centroid(rect); % center point
rectRot2 = rotate(rect, deg, [xCnt, yCnt]); % rotates about the centerpoint
Plot results
plot([rect, rectRot2],'FaceColor','none')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!