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.
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

