How to rotate a line drawn by two points by angle 0.1:0.1:1 using a for loop?
3 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 26 Feb 2022
Edited: Voss
on 26 Feb 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 Comments
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
