2017年4月14日金曜日

マル9円周率その3

意味無しモンテカルロ。こちら
float n=1;
float s=0;

void setup() {
  size(300, 350);
  background(255);  
  noFill();
  stroke(0, 2555, 0);
  ellipse(0, 0, 600, 600);
}

void draw() {

  float x=random(0.0, 1.0);
  float y=random(0.0, 1.0);
  if (pow(x, 2)+pow(y, 2)<1.0) {
    stroke(255, 0, 0);
    s+=1;
  } else {
    stroke(0, 0, 255);
  }
  ellipse(x*300, y*300, 1, 1);
  stroke(255);
  fill(255);
  rect(0, 310, 300, 50);
  fill(0);
  text(str(int(n)), 5, 330);  
  text(str(s/n*4.0), 150, 330);
  n++;
}

void mousePressed() {
  n=1;
  s=0;
  background(255);
  noFill();
  stroke(0, 2555, 0);
  ellipse(0, 0, 600, 600);
}