xxxxxxxxxx
39
const r = 200;
let PI = 0;
const STEP = 100;
let TOTAL = 0;
let IN_CIRCLE = 0;
function setup() {
createCanvas(2*r, 2*r);
background(220);
ellipse(width/2, height/2, r*2, r*2);
}
function draw() {
translate(r,r);
strokeWeight(1)
for(let i=0; i < STEP; i++){
stroke('black');
let v= createVector(random(-r,r),random(-r,r));
if((v.x*v.x+v.y*v.y)<r*r){
IN_CIRCLE++;
stroke('red');
}
TOTAL ++;
point(v.x,v.y);
}
//draw stats
stroke('black')
PI = 4*IN_CIRCLE/TOTAL
rect(-180, -160, 200, 50);
strokeWeight(0);
textSize(12);
textStyle(NORMAL);
text("Nb. points dans le cercle : " + IN_CIRCLE, -175,-145);
text("Nb. points total : "+TOTAL, -175,-130);
text("ratio cercle/total : "+PI.toFixed(9), -175,-115);
}