# How to make lines at given angles?

51 vues (au cours des 30 derniers jours)
Mariam Shahab le 21 Déc 2022
Commenté : Mariam Shahab le 21 Déc 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!
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Bora Eryilmaz le 21 Déc 2022
Modifié(e) : Bora Eryilmaz le 21 Déc 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;
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Mariam Shahab le 21 Déc 2022
Thank you! That helps.

Connectez-vous pour commenter.

### Plus de réponses (1)

Midhulesh Vellanki le 21 Déc 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;
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Mariam Shahab le 21 Déc 2022
Thank you

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by