How to rotate rectangular with a an angle?

7 ビュー (過去 30 日間)
Riyadh
Riyadh 2025 年 1 月 9 日
編集済み: Adam Danz 2025 年 1 月 9 日
Hey Everone,
I would like to rotate a rectangula with a specific angle.
rectangle('Position',[70 130 25 30]);
Any help
Thank you

回答 (2 件)

Les Beckham
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;

Adam Danz
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')
  1 件のコメント
Riyadh
Riyadh 2025 年 1 月 9 日
Thank you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeElementary Polygons についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by