Random line segments confined in a box
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
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)])
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.
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));
@Jan Sorry to bother you but how would you do this in 3d?
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Conway's Game of Life에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
