このブログは、旧・はてなダイアリー「檜山正幸のキマイラ飼育記 メモ編」(http://d.hatena.ne.jp/m-hiyama-memo/)のデータを移行・保存したものであり、今後(2019年1月以降)更新の予定はありません。

今後の更新は、新しいブログ http://m-hiyama-memo.hatenablog.com/ で行います。

時間がねーから未完成版


// mccircle.js -- Monte Carlo method

/*
* 座標形はキャンバス中心を原点とする数学方式に
* 設定されていることを前提とする。
*
* 大域変数canvasにキャンバスが、
* 大域変数ctxにanvasの2Dコンテキストが入っているとする。
*/

var inner = 0; // 円の内部の点の個数
var outer = 0; // 円の外部の点の個数


/*
* 0≦x≦w, 0≦y≦h である整数乱数の組を返す。
*/
function randomPair(w, h) {
var x = Math.random();
var y = Math.random();
// x, yを上書き再利用
x = Math.floor(x*w);
y = Math.floor(y*w);
return [x, y];
}

/*
* 点は長さ2の配列で表現する。
* 最初の要素がx座標
* 次の要素がy座標
*/
function drawPoint(pt) {
var x = pt[0];
var y = pt[1];
var color;
if (Math.sqrt(x1*x1 + y1*y1) <= 200) {
color = "red";
inner++;
} else {
color = "blue";
outer++;
}
ctx.fillStyle = color;
ctx.fillRect(x, y, 2, 2); // 小さな四角を描く
}

function plotPoints(N) {
for (var i = 0; i < N; i++) {
drawPoint(randomPoint(400, 400));
}
}

var tid = 0; // タイマーID

function doDraw() {
tid = setInterval("plotPoints(1000)", 0.5*1000);
}