2

Hey guys we started coding Buffons needle simulation in my computing class today. My teacher showed us the following code but couldn't explain why the simulation was not getting accurate approximations. There is lots of noise and the approximation varies greatly from values of around 3.6 to 2.8. I think something is flawed but I just can't see which part. Also I didn't understand the last part of the program(how to plot the points on the graph). Where does the 18 come from?

If you have some spare time I would appreciate answers as I am struggling in computing at the moment. Thanks in advance for any answers.

//set up and define all the variables that we will use
int numTrials = 0;
int intersects = 0;
int lineWidth = 30;
int needleWidth = 0.5*lineWidth;
int boardHeight;
float piApprox;
final float PI = 3.14159265359;

//Defines our initial enviroment properties and creates our grid
void setup(){
  size(600,800);
  background(255);
  stroke(0);
  boardHeight = int (height - 200);
  for (int i = 0; i<=width; i+=lineWidth){ 
    line(i,0,i,boardHeight);
  }
}

//Continously executes the same command
void draw(){
  //generates a random x and y co-ordinate. This becomes the first point
    int a = round(random(width));
  int b = round(random(boardHeight));
  float theta = random(PI);
  //Generates a random x and y co-ordinate which is one needlelength away from first point. This becomes the second point
    int c = round(needleWidth*cos(theta)+a);
  int d = round(needleWidth*sin(theta)+b);
  numTrials++;

  //checks for intersections
  boolean touching = false;
  for (int i = 0; i<=width; i+=lineWidth){
    if ((min(a,c) <= i) && (max(a,c) > i)){
      intersects++;
      touching = true;
    }
  }
  //changes colour of line
    if (touching){
    stroke(0,50,155);
  }
  else{
    stroke(0,155,0);
  }
  line(a,b,c,d);
  //Calculates PI and then calls upon the GUI and the graph functions which are updated after every new line)
    piApprox =((numTrials)/( intersects));
  printData();
  graph();
}

void printData(){
  PFont f;
  f = createFont("LetterGothicStd.ttf",32,true);
  textFont(f,12);
  String e = "Number of Trials:" + numTrials + "     ";
  String f = "PI approximation: " + piApprox;
  fill(255);
  stroke(255);
  rect(0,height-20,400,20);
  fill(0);
  text(e,3,height-8);
  text(f,150,height-8);
}

void graph(){
  //draw PI line
  int piLine = height - 20 - round(18 * PI);
  stroke(255,0,0);
  line(0,piLine,width,piLine);

    //Speed determines how often a point is drawn
  int speed = 5;
  //Clears graph when it reaches the end of the screen
  if (round(numTrials/speed) % width == 0){
    fill(255);
    stroke(255);
    rect(0,boardHeight,width,180);
  }

  //plots points
  if(numTrials % speed == 0){
    int pointW = round(numTrials/speed) % width;
        int pointH = height - 20 - round(18 * piApprox);
    stroke(0,55,55);
    point(pointW,pointH);
  }
}
micycle
  • 3,328
  • 14
  • 33
Student01
  • 45
  • 2
  • What do you expect this code to do? What does it do instead? Have you tried [debugging your code](https://happycoding.io/tutorials/processing/debugging)? Which line of code behaves differently from what you expected? – Kevin Workman Aug 19 '18 at 23:48

1 Answers1

1

I ran the code, and after removing the font, changing the colours (for my use) and changing intersects and numTrials to float types, it converges well and seems to work as intended. Perhaps you didn't run through enough trials and this caused your PI estimate to fluctuate unreasonably (you can see this happening during the earlier trials on my run-through as indicated by the line on the screenshot below).

I added frameRate(600) to run the simulation 10x faster.

enter image description here

The number 18 seems to be chosen as to provide a reasonable border between the bottom of the stage and the position at which the line is drawn. Changing this number will affect the y-coordinate at which the red line resides and at which the approximation line converges to.

micycle
  • 3,328
  • 14
  • 33