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

15 views (last 30 days)
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.

Accepted Answer

Voss
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
Ham Man
Ham Man on 24 Oct 2022
Edited: Ham Man on 24 Oct 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!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!