-
Notifications
You must be signed in to change notification settings - Fork 1
/
pi.htm
142 lines (118 loc) · 2.99 KB
/
pi.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPE html>
<html>
<head>
<title>Pi by Monte Carlo</title>
<script type="text/javascript">
/*parameters*/
waitS = 0;
cSize = 500;
/*globals*/
running = false; //true while simmulation is running
totalDarts = 0;
circleDarts = 0;
maxDarts = 100000;
pi = 0.0;
initialize = function(){
//Display refs. (global)
piVal = document.getElementById("piVal");
cDarts = document.getElementById("cDarts");
tDarts = document.getElementById("tDarts");
dispToggle = document.getElementById("dispToggle");
//Canvas refs. (global)
c = document.getElementById("unitSquare");
c.width = cSize;
c.height = cSize;
ctx = c.getContext("2d");
//Temp Array
dartsArray = zeros([maxDarts,2]);
//ctx.strokeStyle="black";
//ctx.beginPath();
//ctx.arc(0,0,cSize,0.0,Math.PI*0.5);
//ctx.stroke();
//ctx.fillStyle="red";
}
start = function(){
document.getElementById("status").innerHTML = "Running...";
running = true;
var startTime = new Date().getTime();
throwDarts(dartsArray);
var stopTime = new Date().getTime();
updatePi(dartsArray);
document.getElementById("status").innerHTML = "Darts per Second = "
+ maxDarts/(stopTime-startTime)*1000/1000000
+ " million";
//stop();
}
throwDarts = function(outArray){
//throw a darts
for(var i=0; i<outArray.length; i++){
var x = Math.random();
var y = Math.random();
//check if inside unit circle
if( Math.sqrt(x*x+y*y) <= 1.0 ){
circleDarts++;
}
totalDarts++;
outArray[i][0] = x;
outArray[i][1] = y;
}
}
updatePi = function(inArray){
if(dispToggle.checked){
for(var i=0; i<inArray.length; i++){
var x = inArray[i][0];
var y = inArray[i][1];
if( Math.sqrt(x*x+y*y) <= 1.0 ){
ctx.fillStyle="blue";
}else{
ctx.fillStyle="red";
}
ctx.fillRect(x*cSize,y*cSize,1,1);
}
}
//probability is proportional to area
pi = circleDarts/totalDarts*4.0;
piVal.innerHTML = pi;
cDarts.innerHTML = circleDarts;
tDarts.innerHTML = totalDarts;
}
reset = function(){
running = false;
pi = 0.0;
circleDarts = 0;
totalDarts = 0;
ctx.clearRect(0,0,cSize,cSize);
piVal.innerHTML = pi;
cDarts.innerHTML = circleDarts;
tDarts.innerHTML = totalDarts;
document.getElementById("status").innerHTML = "Press Start...";
initialize();
}
/*
stop = function(){
running = false;
document.getElementById("status").innerHTML = "Stopped...";
}
*/
/*helper*/
function zeros(dimensions) {
var array = [];
for (var i = 0; i < dimensions[0]; ++i) {
array.push(dimensions.length == 1 ? 0 : zeros(dimensions.slice(1)));
}
return array;
}
</script>
</head>
<body onLoad="initialize()">
<button onClick="start()">Run 100k</button>
<!--<button onClick="stop()">Stop</button>-->
<button onClick="reset()">Reset</button>
Pi = <a id="piVal">0.0</a>
<br />
<input type="checkbox" id="dispToggle" /> Render results after run
<div id="status">Press Start...</div>
<div> (totDarts = <a id="tDarts">0</a>, circleDarts = <a id="cDarts">0</a>)</div>
<canvas id="unitSquare" style="border:10px solid #c3c3c3;">No HTML5!</canvas>
</body>
</html>