int t,n,j=0;
void setup(){
size(700,700);
background(255);
strokeWeight(5);
fill(183,242,179);
rect(50,50,600,600);
strokeWeight(3.5);
for(int i=0;i<6;i++){
stroke(61,244,88);
j=i*100+100;
line(53,j,647,j);
}
}
void draw(){
strokeWeight(2);
if(t<10000)d();
noStroke();
fill(255);
rect(0,653,600,50);
textSize(30);
fill(0);
text("Buffon's Needles by Rocky Zhang",50,37);
textSize(15);
text("Needles Dropped: "+t,50,667);
text("Intersecting Needles: "+n,50,687);
text("Approximation of Pi (Dropped/Crossed): ",350,667);
if(frameCount%30==0){
fill(255);
rect(340,675,350,50);
fill(0);
text(t*1.0/n,600,687);
}
}
void d(){
float x,y,a,b;
x=random(100,650);
y=random(100,600);
b=y+random(-50,50);
a=x-pow(2500-pow(y-b,2),0.5);
stroke(104,221,192);
if(v(y,b)){
stroke(255,98,50);
n++;
}
line(x,y,a,b);
t++;
}
boolean v(float y,float b){
boolean c=false;
for(int i=0;i<6;i++){
j=i*100+100;
c=c||y>j&&b<j||y<j&&b>j||y<j+3.3&&y>j-3.4||b<j+3.4&&b>j-3.4;
}
return c;
}