{% extends "index.html" %}
{% block header %}

<style>

svg {
  font: 10px sans-serif;
}

.bar rect {
      fill: #428bca;
            shape-rendering: crispEdges;
}
.line {
  fill: none;
  stroke: #428bca;
  stroke-width: 1.5px;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

</style>
{% endblock %}
{% block body %}
<div class="container">
    <div class="row">
        <div class="col-md-12" id='cont'>
            <center>
                <h2>Oops, page not found <br>HTTP Status Code: {{code}} </h2>
                <h3>This might be caused by either manually entering an address,
                mismatch between data and database, or our programming mistake.
                    If this problem persists contact us for assistance! </h3>
                <p>Meanwhile lets approximate \( \pi \) by Monte Carlo method!<p>
                    \( A_{sq} = r^2 \);
                    \( A_{ci} = \pi \left(\frac{1}{2} r \right)^2 \);
                    
                    \( \pi = \frac{4 A_{ci}}{A_{sq}} \approx \frac{4 n_{ci}}{n} = \)  4 · <span id="up">1</span> / <span id="down"></span> = <span id="app">1</span> (|ε| = <span id="e"></span>).
                    <div id="notfound"></div>
                    <small><div id="info"></div></small>
                    <div id="live"></div>
            </center>
        </div> 
    </div>
</div>
{% endblock %}

{%block below_js %}
    <script src="{{url_for('static', filename='js/d3.min.js')}}"></script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  <script type="text/javascript" language="javascript" charset="utf-8">

// histogram of apprx pi
var margin = {top: 20, right: 20, bottom: 20, left: 40},
    width = $("#cont").width() - margin.left - margin.right,
    height = 250 - margin.top - margin.bottom;

var n = 500,
    random = d3.random.normal(0, .001),
    data = d3.range(n).map(random);

var xxx = d3.scale.linear()
    .domain([0, n - 1])
    .range([0, width]);

var y = d3.scale.linear()
    .domain([2.85,3.35])
    .range([height, 0]);

var line = d3.svg.line()
    .x(function(d, i) { return xxx(i); })
    .y(function(d, i) { return y(d); });

var svg = d3.select("#live").append("svg:svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg.append("defs").append("clipPath")
    .attr("id", "clip")
  .append("rect")
    .attr("width", width)
    .attr("height", height);


svg.append("g")
    .attr("class", "y axis")
    .attr("transform", "translate("+(width-margin.left)+",0)")
    .call(d3.svg.axis().scale(y).orient("right"));

var path = svg.append("g")
    .attr("clip-path", "url(#clip)")
  .append("path")
    .datum(data)
    .attr("class", "line")
    .attr("d", line);





    var circle_r = 100.0;
    var upsum = 0;
    var downsum = 0;
    var appc = 0;

    var svg2 = d3.select("#notfound").append("svg:svg")
    svg2.attr("width", 2*circle_r).attr("height", 2*circle_r);

    var circ = svg2.append("circle").attr("cx", circle_r).attr("cy", circle_r).attr("r", circle_r).style("fill", "#eee");
    var rect = svg2.append("rect").attr("x", 0).attr("y", 0).attr("width",2*circle_r).attr("height", 2*circle_r).style("stroke","#eee").style("fill","none");
    var up = $("#up");
    var down = $("#down");
    var app = $("#app");
    var e = $("#e");
    var c2 = circle_r*circle_r;
    function dot2() {
        setTimeout(function () {
            var x = Math.random() * (2*circle_r) - circle_r;
            var y = Math.random() * (2*circle_r) - circle_r;
           
            if ((x*x+y*y)>c2) { 
                if (downsum<10000) // stop due to huge MEM usage (svg)
                    svg2.append("circle").attr("class","dot").attr("cx", x+circle_r).attr("cy", y+circle_r).attr("r",1); 
            } else { 
                upsum ++; 
                if (downsum<10000)
                    svg2.append("circle").attr("class","dot2").attr("cx", x+circle_r).attr("cy", y+circle_r).attr("r",1); 
            }
            downsum++;
            appc = 4.0*upsum/downsum;
            if (downsum==10000) {
                $("#info").html("Adding points to the above fig stopped at 10k points, but c'mon - approximate constantly, since "+appc.toFixed(5)+" is rather poor approximation.");
            }

            up.html(upsum);
            down.html(downsum);
            app.html(appc.toFixed(8));
            e.html(Math.abs(appc-Math.PI).toFixed(8));
  data.push(appc);

  path
      .attr("d", line)
      .attr("transform", null)
    .transition()
      .ease("linear")
      .attr("transform", "translate(" + xxx(-1) + ",0)");
  data.shift();

            dot2();
        }, 20);
    }
$(document).ready( function() {
    setTimeout(function(){
    dot2();
    },3000);
});
  </script>
{%endblock %}
