-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c9b86d5
Showing
12 changed files
with
6,966 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<!DOCTYPE HTML> | ||
<html lang="en-US"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Estimating Pi</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link rel="shortcut icon" href="favicon.ico" /> | ||
<link rel="stylesheet" href="bootstrap.css" type="text/css" media="screen"> | ||
<link rel="stylesheet" href="montecarlo.css" type="text/css" media="screen"> | ||
<script src="jquery-2.1.1.min.js"></script> | ||
<script src="jquery-ui.min.js"></script> | ||
<script src='montecarlo.js'></script> | ||
</head> | ||
<body class="container"> | ||
|
||
<div id="header" class="row"> | ||
<div class="col-md-12"> | ||
<h2>Estimating Pi Using Monte Carlo Methods</h2> | ||
<h4>APCS Lesson 29</h4> | ||
</div> | ||
</div> | ||
<div class="row"> | ||
<div class="col-md-3"> | ||
<button>Place <span class="qty"></span> points</button> | ||
<div class="hide-on-start top-margin"> | ||
<p>Total dots: <span id="totalCount"></span></p> | ||
<p>Dots inside circle: <span id="insideCount"></span></p> | ||
<p>Estimate of π: <span id="estimate"></span></p> | ||
<p>Actual value of π: <span id="actual"></span></p> | ||
<p>Percentage error: <span id="error"></span>%</p> | ||
</div> | ||
</div> | ||
<div class="col-md-6"> | ||
<canvas id="canvas" width="400" height="400"></canvas> | ||
</div> | ||
<div class="col-md-3"> | ||
<p class="divider">How the math works:</p> | ||
<p>The ratio of the area of the circle to the area of the square should be proportional to the ratio of points in the circle to the points in the square</p> | ||
<img src="ratio.gif" class="img-rounded"> | ||
<p class="text">Substitute the number of points in each region for the area of each region and solve the above equation for π</p> | ||
<img src="eq2.gif" class="eqn"> | ||
<p>But since 2r = s, this simplifies to</p> | ||
<img src="eq3.gif" class="eqn"> | ||
</div> | ||
</div> | ||
|
||
<div id="footer" class="row"> | ||
<div class="col-md-12"> | ||
<p class="small">Estimating Pi <span class="version"></span> | ||
MIT License <span data-bind="text: copyrightYear"></span><br/> | ||
Source code is at <a href="https://bitbucket.org/rogerjaffe/piestimator/src" target="_blank">BitBucket</a></p> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
canvas { | ||
width: 400px; | ||
height: 400px; | ||
border: 1px solid black; | ||
} | ||
|
||
img { | ||
margin-bottom: 10px; | ||
} | ||
|
||
#footer { | ||
margin-top: 10px; | ||
} | ||
|
||
.top-margin { | ||
margin-top: 5px; | ||
} | ||
|
||
.hide-on-start { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
(function (width, version) { | ||
// Configuration constants | ||
var config = { | ||
width: 400, // Canvas width/height | ||
version: 'V1.0', | ||
errorPrecision: 5, // Decimal precision for display | ||
quantity: 10000, | ||
inside: 0, // Pixel count inside circle | ||
total: 0 // Pixel count total | ||
}; | ||
config.half = config.width >> 1; // Half canvas size | ||
|
||
// These functions are called once when the page loads | ||
// Draw the circle inscribed inside the canvas square area | ||
function drawCircle(context) { | ||
context.beginPath(); | ||
context.arc(config.half, config.half, config.half, 0, 2*Math.PI); | ||
context.stroke(); | ||
} | ||
|
||
// Create the pixel image object | ||
function createPixelImageData(context) { | ||
var id = context.createImageData(1, 1); | ||
var d = id.data; | ||
d[3] = 255; | ||
return id; | ||
} | ||
|
||
// Draw config.quantity pixels on canvas | ||
function drawPixels() { | ||
for (var i = 0; i<config.quantity; i++) { | ||
var coord = drawPixel(config.context, config.id, selectPixel(config.width)); | ||
if (insideCircle(coord)) { | ||
config.inside++; | ||
} | ||
config.total++; | ||
} | ||
$('.hide-on-start').show(); | ||
} | ||
|
||
// Draw a dot on the canvas | ||
// width: width of canvas (x and y since it's a square) | ||
function selectPixel(width) { | ||
var x = Math.floor(Math.random() * width); | ||
var y = Math.floor(Math.random() * width); | ||
return {x: x, y: y}; | ||
} | ||
|
||
// Draw the pixel on the page | ||
// context: canvas context | ||
// id: pixel image data | ||
// coord: coordinate to place pixel (.x, .y) | ||
function drawPixel(context, id, coord) { | ||
context.putImageData(id, coord.x, coord.y); | ||
return coord; | ||
} | ||
|
||
// Is the coordinate inside the circle? | ||
function insideCircle(coord) { | ||
var _x = coord.x - config.half; | ||
var _y = coord.y - config.half; | ||
var dx = _x*_x + _y*_y; | ||
return (dx <= config.half * config.half); | ||
} | ||
|
||
// Compute the estimate of pi using the ratio of pixels inside and | ||
// the total number of pixels | ||
function computeEstimate(inside, total) { | ||
config.estimate = config.inside / config.total * 4; | ||
config.error = ((config.estimate - Math.PI) / Math.PI * 100.0).toFixed(5); | ||
} | ||
|
||
// Update all the display fields | ||
function updateDisplay() { | ||
$('#insideCount').text(config.inside); | ||
$('#totalCount').text(config.total); | ||
$('#estimate').text((config.estimate).toFixed(config.errorPrecision)); | ||
$('#error').text(((config.estimate - Math.PI) / Math.PI * 100.0).toFixed(config.errorPrecision)); | ||
} | ||
|
||
$(document).ready(function () { | ||
config.canvas = document.getElementById('canvas'); // Canvas object | ||
config.context = config.canvas.getContext('2d'); // Context to canvas object | ||
config.fillStyle = '#000000'; // Black fill | ||
config.strokeStyle = "#FF0000"; // Red line (for circle) | ||
config.lineWidth = 3; // Line width | ||
config.context.fillStyle = config.fillStyle; // Set context values | ||
config.context.strokeStyle = config.strokeStyle; | ||
config.context.lineWidth = config.lineWidth; | ||
|
||
config.id = createPixelImageData(config.context); // Create pixel object | ||
drawCircle(config.context); // Draw the circle | ||
|
||
// Display static values | ||
$('#actual').text(Math.PI.toFixed(config.errorPrecision)); | ||
$('.version').text(config.version); | ||
$('.qty').text(config.quantity); | ||
|
||
// Button click handler | ||
$('button').click(function () { | ||
drawPixels(); | ||
computeEstimate(); | ||
updateDisplay(); | ||
}); | ||
}); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"folders": | ||
[ | ||
{ | ||
"follow_symlinks": true, | ||
"path": "." | ||
} | ||
] | ||
} |
Oops, something went wrong.