JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
Below is a very simple way to estimate Pi. The area of a circle is \(A_c = \pi r^2\). The area of a square that fits over the same circle is \(A_s = (2r)^2\). If we solve each for \(r\) and then solve for \(\pi\), then we prove that \(\frac{A_c}{A_s} = \frac{\pi}{4}\).
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
Proof that \(\frac{A_c}{A_s} = \frac{\pi}{4}\)
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
Solve \(A_s = (2r)^2\) for \(r\):
\[r = \frac{\sqrt{A_s}}{2}\]
Solve \(A_c = \pi r^2\) for \(r\):
\[r = \sqrt{\frac{A_c}{\pi}}\]
Combine and solve:
\[\sqrt{\frac{A_c}{\pi}} = \frac{\sqrt{A_s}}{2}\]
\[\frac{A_c}{\pi} = \frac{A_s}{4}\]
\[\frac{A_c}{A_s} = \frac{\pi}{4}\]
</div>
</div>
</div>
</div>
<div ng-app="PiApp">
<div ng-controller="Controller">
<button class="btn" role="button"
ng-click="running = !running; compute()">
<span ng-show="running">Stop</span>
<span ng-hide="running">Start</span>
</button>
<button class="btn" role="button"
ng-click="points = []; pointsInCircle = 0; allPoints = 0">
Reset
</button>
<table>
<tr><th>\(Math.PI =\)</th><td>{{MathPI}}</td></tr>
<tr><th>\(\frac{\pi}{4}...
CSS
circle {
fill: DarkBlue;
}
circle.inCircle {
fill: DarkRed;
}
th {
text-align:right;
padding-right:0.5em;
}
JavaScript
angular.module('PiApp', [])
.controller('Controller', ['$scope', '$timeout',
function ($scope, $timeout) {
$scope.points = [];
$scope.pointsInCircle = 0;
$scope.allPoints = 0;
$scope.running = false;
$scope.MathPI = Math.PI;
function Point() {
this.x = Math.random();
this.y = Math.random();
this.r = Math.pow(Math.pow(this.x, 2) + Math.pow(this.y, 2), 0.5);
this.inCircle = this.r <= 1;
}
$scope.compute = function () {
if (!$scope.running) return;
for (var i = 0; i < 200; i += 1) {
var p = new Point();
$scope.points.push(p);
if (p.inCircle) $scope.pointsInCircle += 1;
$scope.allPoints += 1;
}
if ($scope.points.length > 1000) {
$scope.points = $scope.points.slice(-1000);
}
$scope.quarterPI = $scope.pointsInCircle / $scope.allPoints;
$scope.PI = $scope.quarterPI * 4;
$timeout($scope.compute);
}
}]);