CodingTrain / CodingTrain/Pi-Day-2018

p5.js version

Open
#4 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Processing
Stars
16
Forks
11
PR merge metrics
No merged PRs in 30d

Description

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:



MATH | PI






Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.