How to make lines at given angles?

92 Ansichten (letzte 30 Tage)
Mariam Shahab
Mariam Shahab am 21 Dez. 2022
Kommentiert: Mariam Shahab am 21 Dez. 2022
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!

Akzeptierte Antwort

Bora Eryilmaz
Bora Eryilmaz am 21 Dez. 2022
Bearbeitet: Bora Eryilmaz am 21 Dez. 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;

Weitere Antworten (1)

Midhulesh Vellanki
Midhulesh Vellanki am 21 Dez. 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;

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by