How to make lines at given angles?

92 ビュー (過去 30 日間)
Mariam Shahab
Mariam Shahab 2022 年 12 月 21 日
コメント済み: Mariam Shahab 2022 年 12 月 21 日
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!

採用された回答

Bora Eryilmaz
Bora Eryilmaz 2022 年 12 月 21 日
編集済み: Bora Eryilmaz 2022 年 12 月 21 日
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 件のコメント
Mariam Shahab
Mariam Shahab 2022 年 12 月 21 日
Thank you! That helps.

サインインしてコメントする。

その他の回答 (1 件)

Midhulesh Vellanki
Midhulesh Vellanki 2022 年 12 月 21 日
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 件のコメント
Mariam Shahab
Mariam Shahab 2022 年 12 月 21 日
Thank you

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by