How to rotate rectangular with a an angle?
조회 수: 8 (최근 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!