How to rotate a line drawn by two points by angle 0.1:0.1:1 using a for loop?

3 visualizaciones (últimos 30 días)
the points are (x(1) y(1)) and (x(2),y(2)). i dont have any code. I am new to coding in general and i am not sure how to rotate the plotted line using a for loop. I am not sure how to change the values in both vectors by that angle 0.1:0.1:1. Any help would be appreciated thanks.

Respuesta aceptada

Voss
Voss el 26 de Feb. de 2022
Editada: Voss el 26 de Feb. de 2022
Math reference: Rotation Matrix.
Rotation about point A:
a = [3 1];
b = [10 3];
angle = 0.1:0.1:1;
figure();
plot([a(1) b(1)],[a(2) b(2)],'LineWidth',2);
hold on
axis equal
text(a(1),a(2),'A','HorizontalAlignment','right');
text(b(1),b(2),'B','HorizontalAlignment','left');
for ii = 1:numel(angle)
cos_angle = cos(angle(ii));
sin_angle = sin(angle(ii));
new_b = a(:)+[cos_angle -sin_angle; sin_angle cos_angle]*(b-a).';
plot([a(1) new_b(1)],[a(2) new_b(2)],'k--');
text(new_b(1),new_b(2),sprintf(' %.1f',angle(ii)));
end
ylim([0 9]);
title('Rotating AB about point A by different angles (in radians)');
Rotation about the origin:
a = [3 1];
b = [10 3];
angle = 0.1:0.1:1;
figure();
plot([a(1) b(1)],[a(2) b(2)],'LineWidth',2);
hold on
axis equal
text(a(1),a(2),'A','HorizontalAlignment','right');
text(b(1),b(2),'B','HorizontalAlignment','left');
for ii = 1:numel(angle)
cos_angle = cos(angle(ii));
sin_angle = sin(angle(ii));
R = [cos_angle -sin_angle; sin_angle cos_angle];
new_a = R*a(:);
new_b = R*b(:);
plot([new_a(1) new_b(1)],[new_a(2) new_b(2)],'k--');
text(new_b(1),new_b(2),sprintf(' %.1f',angle(ii)));
end
ylim([0 11]);
title('Rotating AB about the Origin by different angles (in radians)');
  3 comentarios
Ham Man
Ham Man el 24 de Oct. de 2022
Editada: Ham Man el 24 de Oct. de 2022
Voss I have the same problem and in my case a and b have 128 elements. How this works for my case?should I define a new matrix R?
I need all new points in new line.
Many thanks in advance!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Annotations en Help Center y File Exchange.

Etiquetas

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