Random line segments confined in a box
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
2021 年 5 月 11 日
2D or 3D?
採用された回答
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 件のコメント
Adam Danz
2021 年 5 月 11 日
Nice one!
By collecting the slopes within the loop using m(k)=diff(P2)/diff(P1) there's a nice gaussian distribution of slopes, histogram(m(abs(m)<10)) (10000 iterations).

lena kappa
2021 年 5 月 12 日
thank you guyss!!!
lena kappa
2021 年 5 月 12 日
Hi again guys!!!
Do you know how can i find all the points where each line intersects with any other line?
1. Look up the math for finding the intersection of two lines - it's not very difficult and there are lots of webpages that walk you through those steps.
2. Then alter Jan's solution by saving the P1 and P2 values,
P1 = nan(2, nLine);
P2 = P1;
for k = 1:nLine
. . .
P1(:,k) = . . .
P2(:,k) = . . .
line([P1(1,k), P2(1,k)], [P1(2,k), P2(2,k)]);
end
3. Now loop through each set of coordinates to determine the intersection of line n with line j.
lena kappa
2021 年 5 月 12 日
I tried your code but i get an error
figure;
axes('XLim', [-0.1, 1.1], 'YLim', [-0.1, 1.1]);
nLine = 252;
Coor = [0, 1, 1, 0, 0; ...
0, 0, 1, 1, 0];
P1 = nan(2, nLine);
P2 = P1;
for k = 1:nLine
edge = randperm(4, 2);
edge1 = edge(1);
edge2 = edge(2);
P1(:,k) = Coor(:, edge1) + rand * (Coor(:, edge1 + 1) - Coor(:, edge1));
P2(:,k) = Coor(:, edge2) + rand * (Coor(:, edge2 + 1) - Coor(:, edge2));
fig= line([P1(k,1), P2(k,1)], [P1(k,2), P2(k,2)])
%[xi,yi] = polyxpoly(P1(k,1),P2(k,1),P1(k,2),P2(k,2));
end
and i get the next error:
Index in position 1 exceeds array bounds (must not exceed 2).
Error in code (line 19)
fig= line([P1(k,1), P2(k,1)], [P1(k,2), P2(k,2)])
Adam Danz
2021 年 5 月 12 日
Oops, I corrected that error in my previous comment.
use this line instead,
line([P1(1,k), P2(1,k)], [P1(2,k), P2(2,k)])
By the way, that was a fairly simple error and the error message hints at the problem. If you try to understand the error and investigate it, you'll learn a lot very quickly.
lena kappa
2021 年 5 月 12 日
Thanks for the code and the tip.
lena kappa
2021 年 5 月 12 日
hey adam sorry to bother you again but i wrote this line to get the intersections and i get empty x and y
do you know why??
[xi,yi] = polyxpoly(P1(1,k),P2(1,k),P1(2,k),P2(2,k));
george korris
2022 年 10 月 5 日
@Jan Sorry to bother you but how would you do this in 3d?
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Object Analysis についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
