Random line segments confined in a box

2 ビュー (過去 30 日間)
lena kappa
lena kappa 2021 年 5 月 11 日
コメント済み: Jan 2022 年 10 月 8 日
Hello everyone!
Does anyone know :
How can I create random line segments confined in a box ? Both of the edges of the line segments should touch the sides of the box.
  1 件のコメント
Jan
Jan 2021 年 5 月 11 日
2D or 3D?

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

採用された回答

Jan
Jan 2021 年 5 月 11 日
編集済み: Jan 2021 年 5 月 11 日
figure;
axes('XLim', [-0.1, 1.1], 'YLim', [-0.1, 1.1]);
nLine = 100;
Coor = [0, 1, 1, 0, 0; ...
0, 0, 1, 1, 0];
for k = 1:nLine
edge1 = randi(4);
edge2 = randi(4);
P1 = Coor(:, edge1) + rand * (Coor(:, edge1 + 1) - Coor(:, edge1));
P2 = Coor(:, edge2) + rand * (Coor(:, edge2 + 1) - Coor(:, edge2));
line([P1(1), P2(1)], [P1(2), P2(2)]);
end
With this code the start and endpoint can be of the same edge. If you want different edges:
edge = randperm(4, 2);
edge1 = edge(1)
edge2 = edge(2);
  10 件のコメント
george korris
george korris 2022 年 10 月 5 日
@Jan Sorry to bother you but how would you do this in 3d?
Jan
Jan 2022 年 10 月 8 日

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by