How to rotate rectangular with a an angle?
7 ビュー (過去 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 で Elementary Polygons についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!