90° clockwise rotation of 2D coordinates

26 visualizaciones (últimos 30 días)
Alberto Acri
Alberto Acri el 16 de Dic. de 2022
Editada: Matt J el 17 de Dic. de 2022
Hi. I cannot understand why the 90° clockwise rotation of the attached 2D coordinates (matrix.txt) is not done.
I have tried two different ways but I do not get the desired result.
1st way (using "rot90"):
matrix = importdata('matrix.txt');
Rmatrix = rot90(matrix,3); % I have to rotate 3 times 90° counterclockwise
figure
plot(Rmatrix(1,:), Rmatrix(2,:), 'k.')
axis equal
xlim([0 512]);
ylim([0 512]);
2nd way (using rotation matrix, more convenient):
matrix = importdata('matrix.txt');
theta = 90;
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
Rmatrix = R*matrix';
figure
plot(Rmatrix(1,:), Rmatrix(2,:), 'k.')
axis equal
xlim([0 512]);
ylim([0 512]);
  2 comentarios
Voss
Voss el 16 de Dic. de 2022
Editada: Voss el 16 de Dic. de 2022
rot90 is not for this. Use the rotation matrix approach.
Remove the xlim and ylim lines of code to see the result, and go from there.
Adam Danz
Adam Danz el 16 de Dic. de 2022
As @Voss mentioned, rot90 rotates the array of data. Look how this 4x3 matrix turns into a 3x4 matrix and the top row moves to the first column and it gets fliped as if you moved your monitor 90deg counter clockwise.
x = randi(9,4,3)
x = 4×3
3 5 8 2 2 5 1 4 3 5 6 2
rot90(x)
ans = 3×4
8 5 3 2 5 2 4 6 3 2 1 5
What is the problem with your second method using the rotation matrix?

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 17 de Dic. de 2022
Editada: Matt J el 17 de Dic. de 2022
2nd way (using rotation matrix, more convenient):
Your implementation of this has several problems:
  1. You try to rotate by 90 degrees once, instead of 3 times
  2. Your rotation angle theta is expressed in degrees instead of radians
  3. You are rotating about the origin, which means the rotated coordinates cannot lie in the range [0,512].
Here's what you might have intended:
matrix = importdata('matrix.txt');
theta = 90*3;
R = [cosd(theta) -sind(theta); sind(theta) cosd(theta)];
c=mean(matrix,1);
Rmatrix = (matrix-c)*R'+c;
plot(matrix(:,1), matrix(:,2), 'k.'); hold on
plot(Rmatrix(:,1), Rmatrix(:,2), 'r.'); hold off
axis equal

Más respuestas (1)

Matt J
Matt J el 17 de Dic. de 2022
Editada: Matt J el 17 de Dic. de 2022
matrix = importdata('matrix.txt');
p=polyshape(matrix(:,1), matrix(:,2),'Simplify',1);
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
[cx,cy]=centroid(p);
prot=rotate(p,90*3,[cx,cy]);
plot([p,prot]); axis equal

Categorías

Más información sobre Interactions, Camera Views, and Lighting en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by