2017年4月13日木曜日

マル9な円周率計算デモ

頭の悪い円周率計算デモ。Processing.jsで動かすとこちら
float n=1;

void setup(){
  size(300,350);
  background(255);  
}

void draw(){
  float s=0;
  background(255);
  fill(255);
  for(int m=0;m<n;m++){
    float x=m/n;
    float f=sqrt(1-pow((x),2.0));
    stroke(0);
    rect(x*300,0,1/n*300,f*300);
    noFill();
    stroke(255,0,0);
    ellipse(0,0,600,600);

    s=s+f*1/n;
  }
  n+=int(n/10+1);
 if(n<300){
    delay(int(1000/sqrt(n)));
  }
  fill(0);
  textSize(15);
  text(str(int(n)),5,330);  
  text(str(s*4.0),150,330);
  if(n>10000000){
    noLoop();
  }    
}

void mousePressed(){
  n=1;
  loop();
}