Hi everyone! I hope everyone is doing well!
I have the following problem:
My goal is to create a bunch of random lines in a box as follows: a random point on one of the 4 edges of the box is chosen. A random angle is also chosen, and combing the coordinates of that random point and the angle, I have the equation of the line, so that it can be drawn.
The problem is that most of the lines seem to be close together, which tells me that the process is not that random, as they seem to not be randomly distributed in the box. How can I fix this?
I am attaching a pic that shows what i get when i run the code below.
figure;
axes('XLim', [-0.1, 1.1], 'YLim', [-0.1, 1.1]);
nLine =20;
Coor = [0, 1, 1, 0, 0; ...
0, 0, 1, 1, 0];
P1 = nan(2, nLine);
for k = 1:nLine
edge = randperm(4, 2);
edge1 = edge(1);
P1(:,k) = Coor(:, edge1) + rand * (Coor(:, edge1 + 1) - Coor(:, edge1));
angle = pi*rand;
slope = tan(angle);
b = P1(2,k)- slope* P1(1,k);
fig = refline(slope,b)
end

 採用された回答

Dave B
Dave B 2021 年 10 月 28 日

1 投票

Is your axis still at the limits you specified at the top? My guess is that refline is changing the limits. Can you just put this line at the end (instead of the axes at the beginning):
set(gca,'XLim', [-0.1, 1.1], 'YLim', [-0.1, 1.1])

4 件のコメント

lena kappa
lena kappa 2021 年 10 月 28 日
編集済み: lena kappa 2021 年 10 月 28 日
Hi Dave and thank you for your answer!
Do you mean to alter my code in this way:
figure;
nLine =20;
Coor = [0, 1, 1, 0, 0; ...
0, 0, 1, 1, 0];
P1 = nan(2, nLine);
for k = 1:nLine
edge = randperm(4, 2);
edge1 = edge(1);
P1(:,k) = Coor(:, edge1) + rand * (Coor(:, edge1 + 1) - Coor(:, edge1));
angle = pi*rand;
slope = tan(angle);
b = P1(2,k)- slope* P1(1,k);
fig = refline(slope,b)
end
axes('XLim', [-0.1, 1.1], 'YLim', [-0.1, 1.1]);
because i did it and now i get an empty plot.
Dave B
Dave B 2021 年 10 月 28 日
編集済み: Dave B 2021 年 10 月 28 日
You should paste literally the exact line I gave you above. If you use axes you'll create a new axes on top of the old one.
set(gca,'XLim', [-0.1, 1.1], 'YLim', [-0.1, 1.1])
Or alternatively:
axis([-0.1 1.1 -0.1 1.1])
lena kappa
lena kappa 2021 年 10 月 28 日
Thank you so much Dave!!! So for the mistake.
Dave B
Dave B 2021 年 10 月 28 日
No worries, we have axes and axis, and then properties like XAxis and the whole thing get confusing at times!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Convert Image Type についてさらに検索

質問済み:

2021 年 10 月 28 日

コメント済み:

2021 年 10 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by