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

조회 수: 3 (최근 30일)
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.

채택된 답변

Voss
Voss 2022년 2월 26일
편집: Voss 2022년 2월 26일
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
Ham Man
Ham Man 2022년 10월 24일
편집: Ham Man 2022년 10월 24일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by