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

回答(1 个)

Image Analyst
Image Analyst 2018-11-14
Use the function line() inside the loop, not plot() after the loop
line([x1, x2], [y1, y2]);

类别

Help CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by