How do I plot multiple random lines, of length 1, not connected to one another?
1 回表示 (過去 30 日間)
古いコメントを表示
I am creating a GUI for Buffon's Needle Experiment- suppose we have a floor made of parallel strips each the same width, and we drop a needle on the floor. The needle length is 1 and the width of each wood strip is 2. In my gui, I have limited my x values so i only have 4 pieces of wood. I am struggling plotting random lines, length 1, without connecting them to one another. This is what I am graphing and my code is below
my code so far is as follows
% L = length of the needle
L = 1;
% D = width of each strip of wood
D = 2;
% L < D
for n = 10:10:100
x = 8*rand(n,1);
y = rand(n,1);
theta = rand(n,1)*(2*pi);
for i= 1:100
x1=x-(L/2)*cos(theta);
y1=y-(L/2)*sin(theta);
x2=x+(L/2)*cos(theta);
y2=y+(L/2)*sin(theta);
end
end
n=10:10:100;
plot([x1 x2],[y1 y2])
hold on
0 件のコメント
回答 (1 件)
Image Analyst
2018 年 11 月 14 日
Use the function line() inside the loop, not plot() after the loop
line([x1, x2], [y1, y2]);
参考
カテゴリ
Help Center および File Exchange で Debugging and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!