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.

 Accepted Answer

Jan
Jan on 11 May 2021
Edited: Jan on 11 May 2021
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 Comments

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).
thank you guyss!!!
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.
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?

Sign in to comment.

More Answers (0)

Categories

Find more on Conway's Game of Life in Help Center and File Exchange

Asked:

on 11 May 2021

Commented:

Jan
on 8 Oct 2022

Community Treasure Hunt

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

Start Hunting!