# How to make lines at given angles?

31 visualizaciones (últimos 30 días)
Mariam Shahab el 21 de Dic. de 2022
Comentada: Mariam Shahab el 21 de Dic. de 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 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Bora Eryilmaz el 21 de Dic. de 2022
Editada: Bora Eryilmaz el 21 de Dic. de 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 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Mariam Shahab el 21 de Dic. de 2022
Thank you! That helps.

Iniciar sesión para comentar.

### Más respuestas (1)

Midhulesh Vellanki el 21 de Dic. de 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 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Mariam Shahab el 21 de Dic. de 2022
Thank you

Iniciar sesión para comentar.

### Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by