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.

 채택된 답변

Jan
Jan 2021년 5월 11일
편집: Jan 2021년 5월 11일

2 개 추천

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
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
lena kappa 2021년 5월 12일
thank you guyss!!!
lena kappa
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?
Adam Danz
Adam Danz 2021년 5월 12일
편집: Adam Danz 2021년 5월 12일
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
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));
george korris
george korris 2022년 10월 5일
@Jan Sorry to bother you but how would you do this in 3d?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Conway's Game of Life에 대해 자세히 알아보기

질문:

2021년 5월 11일

댓글:

Jan
2022년 10월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by