1 | <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" > |
---|
2 | <head> |
---|
3 | <title>Pi calculation</title> |
---|
4 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
---|
5 | <style type="text/css"> |
---|
6 | @import "../../../dojo/resources/dojo.css"; |
---|
7 | @import "../../../dijit/tests/css/dijitTests.css"; |
---|
8 | </style> |
---|
9 | <script type="text/javascript" src="../../../dojo/dojo.js" djConfig="isDebug: true"></script> |
---|
10 | <script type="text/javascript"> |
---|
11 | dojo.require("dojox.gfx"); |
---|
12 | |
---|
13 | var iterations = 0, inCounter = 0, totalCounter = 0, |
---|
14 | interval, surface; |
---|
15 | |
---|
16 | var compute = function(){ |
---|
17 | var x = Math.random(); |
---|
18 | var y = Math.random(); |
---|
19 | var pointX = Math.round(x * 300); |
---|
20 | var pointY = Math.round(y * 300); |
---|
21 | |
---|
22 | var pointColor = "red"; |
---|
23 | if(x*x + y*y < 1){ |
---|
24 | inCounter++; |
---|
25 | pointColor = "green"; |
---|
26 | } |
---|
27 | |
---|
28 | surface.createCircle({cx: pointX, cy: pointY, r: 3}). |
---|
29 | setFill(pointColor).setStroke({color: pointColor, width: 2}); |
---|
30 | |
---|
31 | totalCounter++; |
---|
32 | |
---|
33 | if(totalCounter % 100 == 0 || totalCounter == iterations){ |
---|
34 | var PI = parseFloat(inCounter / totalCounter) * 4; |
---|
35 | var error = (PI - Math.PI) / Math.PI * 100; // in % |
---|
36 | dojo.byId("result").innerHTML = PI + " (error = " + error.toFixed(2) + |
---|
37 | "%) after " + totalCounter + " points"; |
---|
38 | } |
---|
39 | |
---|
40 | if(totalCounter == iterations){ |
---|
41 | clearInterval(interval); |
---|
42 | dojo.byId("startButton").disabled = false; |
---|
43 | } |
---|
44 | }; |
---|
45 | |
---|
46 | var go = function(){ |
---|
47 | dojo.byId("startButton").disabled = true; |
---|
48 | dojo.byId("result").innerHTML = " "; |
---|
49 | inCounter = totalCounter = 0; |
---|
50 | iterations = parseInt(0 + parseInt(dojo.byId("iterations").value), 10); |
---|
51 | interval = setInterval(compute, 20); |
---|
52 | }; |
---|
53 | |
---|
54 | var init = function(){ |
---|
55 | surface = dojox.gfx.createSurface("test", 300, 300); |
---|
56 | dojo.connect(dojo.byId("startButton"), "onclick", this, go); |
---|
57 | }; |
---|
58 | |
---|
59 | dojo.addOnLoad(init); |
---|
60 | |
---|
61 | </script> |
---|
62 | </head> |
---|
63 | <body> |
---|
64 | <h1>PI demo</h1> |
---|
65 | <p>Visualization of calculating PI using the Monte Carlo method.</p> |
---|
66 | <p> |
---|
67 | <label>Iterations:</label> <input type="text" name="iterations" value="500" id="iterations"/> |
---|
68 | <input type="button" name="start" value="start" id="startButton" /> |
---|
69 | </p> |
---|
70 | <p><span style="color:green">Green</span>: x^2 + y^2 <= 1, <span style="color:red">Red</span>: x^2 + y^2 > 1</p> |
---|
71 | <p>Estimated value for PI: <span id="result" style="font-weight: bold"></span></p> |
---|
72 | <div id="test" style="width: 300px; height: 300px;border:1px solid black"></div> |
---|
73 | <p>That's all Folks!</p> |
---|
74 | </body> |
---|
75 | </html> |
---|