Creating lines of a random orientation and unit length in matlab

8 ビュー (過去 30 日間)
Himanshi Rani
Himanshi Rani 2017 年 9 月 4 日
回答済み: Image Analyst 2017 年 9 月 4 日
I want to create a line between two points start:(x1,y1) and end:(x2,y2) where the coordinates x1,y1,x2,y2 are sampled randomly from uniform distribution. Then I want to create another line with the same starting point and same orientation but unit length. My approach: I use "rand" function in matlab to get the coordinates. The orientation using dy/dx. I do not know how do I fix the orientation of the second line as dy/dx.

採用された回答

Image Analyst
Image Analyst 2017 年 9 月 4 日
It's just simple division:
maxDistance = 4; % Whatever....
x1 = maxDistance * rand(1, 1)
y1 = maxDistance * rand(1, 1)
x2 = maxDistance * rand(1, 1)
y2 = maxDistance * rand(1, 1)
plot([x1,x2], [y1,y2], 'b*-', 'LineWidth', 2, 'MarkerSize', 10);
grid on;
hold on;
distanceBetween = sqrt((x2-x1)^2 + (y2-y1)^2)
x2unit = x1 + (x2-x1)/distanceBetween
y2unit = y1 + (y2-y1)/distanceBetween
plot([x1,x2unit], [y1,y2unit], 'ro-', 'LineWidth', 2, 'MarkerSize', 15);

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by