简体   繁体   English

如何在圆周上接近pi

[英]How to approach pi in a circle

I have received an admission assignment for the Computer Science program, where I have to approach PI. 我已经收到了计算机科学计划的录取任务,在那里我必须攻读PI。 To calculate pi I have to make a ratio between the area of ​​a square, a circle, the total number of dots and the number of dots within the circle. 为了计算pi,我必须在正方形,圆形的面积,圆点总数和圆点数之间求一个比率。

  • N = total number of dots N =总点数
  • M = dots within the circle M =圆内的点
  • The diameter is 400 直径是400

The formula of the area of ​​a circle 圆的面积公式

radius^2 * π or diameter^2 * π / 4 半径^ 2 *π或直径^ 2 *π/ 4

The formula of the area of a square 平方面积的公式

2*radius^2 or diameter^2 2 *半径^ 2或直径^ 2

I had the formule ( M / N ) * 4 我有配方(M / N)* 4

This is how I got it: 这是我的方法:

(d^2 * π / 4) : d^2 = M : N (d ^ 2 *π/ 4):d ^ 2 = M:N

π / 4 = M / N π/ 4 = M / N

π = ( M / N ) * 4 π=(M / N)* 4

the problem is that I don't get pi as output, but about 14.2.. 问题是我没有得到pi作为输出,而是大约14.2。

Does anyone know what I am doing wrong? 有人知道我在做什么错吗?

So in Processing I wrote the following code 所以在处理中,我编写了以下代码

float N = 0;
float M = 0;

void setup()
{
    size(400, 400);
    frameRate(90000); 
    background(255, 255, 255);
    ellipse(200,200,400,400);

}

void draw()
{

    /* Random x- en y-coordinate. */
    float x = random(0,400);
    float dx= (x-200);
    float y = random(0,400);
    float dy = (y-200);
    float d = (float)(Math.sqrt(Math.pow(dx,2) + Math.pow(dy,2)));

    /*Red in the circle*/
    if(d <= 200 ){ 
      stroke(255,0,0);
      M++;
    } 
    else{ 
      stroke(0,255,0); /*green around the circle*/
      N++;
    }
    point(x,y);
    println

    ((M/N)*4); 

}

The dots that land inside the circle also land in the square since the circle lays inside the square. 由于圆位于正方形内,落在圆内的点也落在正方形内。

You need to do 4*M/(M+N). 您需要执行4 * M /(M + N)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM