How to make lines at given angles?

98 views (last 30 days)
Hi all,
Im trying to make 5 lines at the following angles: 90, 180, 270, and 360 degrees.
This is my code so far:
angle=0;
lineLength2 = 20;
angle=[90:90:360];
k=15;
l=12;
plot(k,l); % this is the center. All five lines start from here.
hold on;
e= k + lineLength2*cosd(angle(1));
h= l + lineLength2*sind(angle(1));
plot([k l], [e h]);
hold off;
Can someone kinldy guide me how to make a for loop so that 5 lines are contsructed accroding to their respective angles?
Thanks!

Accepted Answer

Bora Eryilmaz
Bora Eryilmaz on 21 Dec 2022
Edited: Bora Eryilmaz on 21 Dec 2022
Looks like you are actually looking for 4 lines since angles of 0 and 360 would produce the same lines. But here is the for-loop you want:
lineLength2 = 20;
angle=[90:90:360];
k=15;
l=12;
plot(k,l,'ro'); % this is the center. All five lines start from here.
hold on;
for i = 1:numel(angle)
e = k + lineLength2*cosd(angle(i));
h = l + lineLength2*sind(angle(i));
plot([k e], [l h]);
end
hold off;

More Answers (1)

Midhulesh Vellanki
Midhulesh Vellanki on 21 Dec 2022
you pretty much have it, you just need to loop over the code for generating the lines and plotting, in MATLAB plotting goes as plot(X,Y). plot([k l], [e h]); becomes plot([k e], [l h]);
angle=0;
lineLength2 = 20;
angle=[90:90:360];
k=15;
l=12;
plot(k,l, '*'); % this is the center. use '*' so that it is visible
hold on;
for i=1:4
e= k + lineLength2*cosd(angle(i));
h= l + lineLength2*sind(angle(i));
plot([k e], [l h]);
end
hold off;

Categories

Find more on Loops and Conditional Statements 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!