Skip to content

p5.js version #4

Description

@J0eppp

Hi there, I don't know how to submit this thing, but I made a p5.js version of your processing code. Here it is.

sketch.js:
var r = 200;
var total = 0;
var circle = 0;
var recordPI = 0;

function setup() {
  createCanvas(402, 402);
  background(0);
  translate(width / 2, height / 2);
  stroke(255);
  strokeWeight(4);
  noFill();
  ellipse(0, 0, r * 2, r * 2);
  rectMode(CENTER);
  rect(0, 0, r * 2, r * 2);
}

function draw() {
  translate(width / 2, height / 2);
  for (var i = 0; i < 10000; i++) {
    var x = random(-r, r);
    var y = random(-r, r);
    total++;

    var d = x * x + y * y;
    if (d < r * r) {
      circle++;
      stroke(100, 255, 0, 100);
    } else {
      stroke(0, 100, 255, 100);
    }
    strokeWeight(1);
    point(x, y);

    var pi = 4 * (circle / total);
    var recordDiff = Math.abs(Math.PI - recordPI);
    var diff = Math.abs(Math.PI - pi);
    if (diff < recordDiff) {
      recordDiff = diff;
      recordPI = pi;
      console.log(pi);
      document.getElementById('pi').innerHTML = "PI = " + pi;
    }
  }
}

index.html:



<title>MATH | PI</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script src="sketch.js"></script>




Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions