Das Nadelexperiment von Buffon
Stochastische Approximation der Kreiszahl π
0 Nadeln,
0 Treffer
// Nadelexperiment von Buffon (c)2021 dh
// Der Programmcode ist hier gekürzt wiedergegeben.
var running = false;
var ddefault = 300;
var ldefault = 240;
var ndefault = 100;
var tdefault = 10;
var d = ddefault;
var l = ldefault;
var n = ndefault;
var t = tdefault;
var dold = 0;
var lold = 0;
var nold = 0;
var told = 0;
var num_all = 0;
var num_hit = 0;
var w = 0;
var p = 0;
var lines = 0;
var cnv;
var ctx;
window.onload = function() {
initValues();
initCanvas();
}
function setPoint(x,y, color) {
ctx.lineWidth = 1;
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x,y);
ctx.arc(x, y, 1, 0, 2*Math.PI);
ctx.stroke();
}
function drawLine(x1,y1, x2,y2, width, color) {
ctx.lineWidth = width;
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);
ctx.stroke();
}
function clearCanvas() {
ctx.clearRect(0, 0, 1000, 1000);
}
function initCanvas() {
cnv = document.getElementById ("dhcanvas");
ctx = cnv.getContext("2d");
clearCanvas();
document.getElementById("resultate").style.display = 'none';
ausgabe("resultw", '0');
ausgabe("results", '0');
ausgabe("resultr", '0');
ausgabe("resultp", '0');
num_all = 0;
num_hit = 0;
drawLines(l,d);
cnv.height = lines*d;
drawLines(l,d);
}
function initValues() {
document.getElementById("input_d").value = ddefault;
document.getElementById("input_l").value = ldefault;
document.getElementById("input_n").value = ndefault;
document.getElementById("input_t").value = tdefault;
}
function getRandomInt(min, max) {
return Math.floor(Math.random()*(Math.floor(max)-Math.ceil(min)+1)) + min;
}
function div2(z) {
return Math.trunc(z/2);
}
function IsEven(z) {
var result = (div2(z) - z/2 == 0) ? true : false;
return result;
}
function dropNeedle(l,d) {
var phi = getRandomInt(1, 1440);
var b = (phi*Math.PI)/180;
var x = getRandomInt(1, 1000);
var y = getRandomInt(1, lines*d);
var ms = Math.round((l*Math.sin(b))/2);
var mc = Math.round((l*Math.cos(b))/2);
var i = x - mc;
var j = y - ms;
var ii = x + mc;
var jj = y + ms;
var a = Math.round(d/2);
var s = Math.abs((l*Math.sin(b))/2);
var m = (jj+j)/2;
var color = 'black';
while ((a <= lines*d)&&(color == 'black')) {
if (Math.abs(a - m) <= s) color = 'red';
a += d;
}
drawNeedle (i,j, ii,jj, 2, color);
num_all++;
if (color == 'red') num_hit++;
if (num_hit != 0) w = (2*l*num_all)/(num_hit*d);
w = Math.round(10000*w)/10000;
p = Math.round(10000*w/Math.PI)/10000;
ausgabe("resultw", w);
ausgabe("results", num_all);
ausgabe("resultr", num_hit);
ausgabe("resultp", p);
}
function stopExperiment() {
running = false;
}
function runExperiment() {
if (!running) initExperiment();
var play = function() {dropNeedle(l,d);};
var completed = function() {running = false;};
if (t > 0) play.timer(t, n+1, completed);
if (t == 0) {
for (var i=0; i<n; i++) dropNeedle(l,d);
running = false;
}
}
// Der Programmcode ist hier gekürzt wiedergegeben.
var running = false;
var ddefault = 300;
var ldefault = 240;
var ndefault = 100;
var tdefault = 10;
var d = ddefault;
var l = ldefault;
var n = ndefault;
var t = tdefault;
var dold = 0;
var lold = 0;
var nold = 0;
var told = 0;
var num_all = 0;
var num_hit = 0;
var w = 0;
var p = 0;
var lines = 0;
var cnv;
var ctx;
window.onload = function() {
initValues();
initCanvas();
}
function setPoint(x,y, color) {
ctx.lineWidth = 1;
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x,y);
ctx.arc(x, y, 1, 0, 2*Math.PI);
ctx.stroke();
}
function drawLine(x1,y1, x2,y2, width, color) {
ctx.lineWidth = width;
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);
ctx.stroke();
}
function clearCanvas() {
ctx.clearRect(0, 0, 1000, 1000);
}
function initCanvas() {
cnv = document.getElementById ("dhcanvas");
ctx = cnv.getContext("2d");
clearCanvas();
document.getElementById("resultate").style.display = 'none';
ausgabe("resultw", '0');
ausgabe("results", '0');
ausgabe("resultr", '0');
ausgabe("resultp", '0');
num_all = 0;
num_hit = 0;
drawLines(l,d);
cnv.height = lines*d;
drawLines(l,d);
}
function initValues() {
document.getElementById("input_d").value = ddefault;
document.getElementById("input_l").value = ldefault;
document.getElementById("input_n").value = ndefault;
document.getElementById("input_t").value = tdefault;
}
function getRandomInt(min, max) {
return Math.floor(Math.random()*(Math.floor(max)-Math.ceil(min)+1)) + min;
}
function div2(z) {
return Math.trunc(z/2);
}
function IsEven(z) {
var result = (div2(z) - z/2 == 0) ? true : false;
return result;
}
function dropNeedle(l,d) {
var phi = getRandomInt(1, 1440);
var b = (phi*Math.PI)/180;
var x = getRandomInt(1, 1000);
var y = getRandomInt(1, lines*d);
var ms = Math.round((l*Math.sin(b))/2);
var mc = Math.round((l*Math.cos(b))/2);
var i = x - mc;
var j = y - ms;
var ii = x + mc;
var jj = y + ms;
var a = Math.round(d/2);
var s = Math.abs((l*Math.sin(b))/2);
var m = (jj+j)/2;
var color = 'black';
while ((a <= lines*d)&&(color == 'black')) {
if (Math.abs(a - m) <= s) color = 'red';
a += d;
}
drawNeedle (i,j, ii,jj, 2, color);
num_all++;
if (color == 'red') num_hit++;
if (num_hit != 0) w = (2*l*num_all)/(num_hit*d);
w = Math.round(10000*w)/10000;
p = Math.round(10000*w/Math.PI)/10000;
ausgabe("resultw", w);
ausgabe("results", num_all);
ausgabe("resultr", num_hit);
ausgabe("resultp", p);
}
function stopExperiment() {
running = false;
}
function runExperiment() {
if (!running) initExperiment();
var play = function() {dropNeedle(l,d);};
var completed = function() {running = false;};
if (t > 0) play.timer(t, n+1, completed);
if (t == 0) {
for (var i=0; i<n; i++) dropNeedle(l,d);
running = false;
}
}