首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理布冯针头模拟

处理布冯针头模拟
EN

Stack Overflow用户
提问于 2018-08-19 19:02:03
回答 1查看 419关注 0票数 2

嘿,伙计们,我们今天开始在我的计算机课上编写布冯针头模拟的代码。我的老师给我们看了下面的代码,但无法解释为什么模拟不能得到精确的近似。噪声很大,近似值在3.6到2.8之间变化很大。我认为有些东西是有缺陷的,但我只是看不清哪一部分。另外,我也不明白程序的最后一部分(如何绘制图表上的点)。18岁是从哪里来的?

如果你有一些空闲时间,我会感激的答案,因为我正在努力计算的时候。事先谢谢你的回答。

代码语言:javascript
复制
//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);
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-20 12:59:33

我运行了代码,在移除字体、更改颜色(供我使用)并将intersectsnumTrials更改为float类型之后,它很好地收敛,并且看起来像预期的那样工作。也许你没有通过足够的试验,这导致你的PI估计不合理地波动(你可以看到这种情况发生在我之前的测试中,如下面屏幕截图上的行所示)。

我添加了frameRate(600),使模拟运行速度快了10倍。

选择的数字18似乎提供了一个合理的边界之间的底部的舞台和位置的线是画。改变这个数字会影响到红线所在的y坐标,以及近似值线收敛到的坐标。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51920919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档