How to make a line of fixed length?

25 ビュー (過去 30 日間)
Mariam Shahab
Mariam Shahab 2022 年 11 月 29 日
コメント済み: Star Strider 2022 年 11 月 29 日
Hi all,
I want to make a line that has a fixed length and it has a partciular direction. I would appreciate any suggestions for this. Thanks!
  2 件のコメント
Davide Masiello
Davide Masiello 2022 年 11 月 29 日
I am afraid you need to be clearer. By making, do you mean plotting?
Moreover, what do you mean with direction?
Mariam Shahab
Mariam Shahab 2022 年 11 月 29 日
Yes, by making, I meant plotting. As for direction, I mean the orientation of the line (i.e. the slope of the line).

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

採用された回答

Star Strider
Star Strider 2022 年 11 月 29 日
It is relatively straightforward to write an anonymous function for this —
createLine = @(startpt, slope, len) [startpt(1) startpt(1)+len.*cos(atan(slope)); startpt(2) startpt(2)+len.*sin(atan(slope))];
slope = rand % Scalar
slope = 0.9275
startpoint = rand(1,2) % Two-Element Vector
startpoint = 1×2
0.2799 0.8866
long = 10*rand % Scalar
long = 7.5515
linevec = createLine(startpoint, slope, long)
linevec = 2×2
0.2799 5.8167 0.8866 6.0217
Check = [tan(atan2(diff(linevec(2,:)), diff(linevec(1,:)))) hypot(diff(linevec(2,:)),diff(linevec(1,:)))]
Check = 1×2
0.9275 7.5515
figure
plot(linevec(1,:), linevec(2,:))
grid
axis('padded','equal')
title(sprintf('Line from (%.2f, %.2f) to (%.2f, %.2f), slope %.2f, length %.2f', [linevec(:,1); linevec(:,2); slope; long]))
Experiment with the ‘createLine’ function.
.
  2 件のコメント
Mariam Shahab
Mariam Shahab 2022 年 11 月 29 日
Alright, thank you
Star Strider
Star Strider 2022 年 11 月 29 日
As always, my pleasure!

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

その他の回答 (1 件)

Vilém Frynta
Vilém Frynta 2022 年 11 月 29 日
You can specify length with numbers of your choice.
You can specify direction numerically as well, you just need to use your imagination and get creative.
x = [1 11];
y = [2 22];
line(x,y);
  1 件のコメント
Mariam Shahab
Mariam Shahab 2022 年 11 月 29 日
Thank you for your help

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

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by