How to rotate rectangular with a an angle?

3 visualizaciones (últimos 30 días)
Riyadh
Riyadh el 9 de En. de 2025
Editada: Adam Danz el 9 de En. de 2025
Hey Everone,
I would like to rotate a rectangula with a specific angle.
rectangle('Position',[70 130 25 30]);
Any help
Thank you

Respuestas (2)

Les Beckham
Les Beckham el 9 de En. de 2025
Editada: Les Beckham el 9 de En. de 2025
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 el 9 de En. de 2025
Editada: Adam Danz el 9 de En. de 2025
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')

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by