How do I plot multiple random lines, of length 1, not connected to one another?

3 views (last 30 days)
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

Answers (1)

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

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!