let n=0,sum=0;
let chord;
let array=[];
function setup(){
createCanvas(windowWidth,windowHeight);
noFill()
strokeWeight(0.1)
}
function draw(){
translate(width/2,height/2);
d=min(width,height)/1.2
x1=random(-d/2,d/2);
p=random(-1,1)
if (p>=0){
p=1
}else{
p=-1
}
q=random(-1,1)
if (q>=0){
q=1
}else{
q=-1
}
y1=p*sqrt(pow(d/2,2)-x1*x1);
y2=random(-d/2,d/2);
x2=q*sqrt(pow(d/2,2)-y2*y2);
line(x1,y1,x2,y2);
rect(width/3,height,3,50,50);
chord=dist(x1,y1,x2,y2)
if(chord>0){
sum = sum+chord;
avg=sum/n;
n++
noStroke()
fill(255)
rect(width/3,height/3-20,300,100);
fill(0)
text("Average Length: " + round(avg,3),width/3, height/3)
text("Diameter: " +round(d,2),width/3,height/3+20)
text("Ratio d/avg: "+ round(d/avg,5),width/3,height/3+40)
text("n: "+ n,width/3,height/3+60)
noFill()
stroke(0)
strokeWeight(0.1)
}else{
console.log(chord,x1,y1,x2,y2)
}
}