commit f83b7bf23b770df998d0f9b40abceac45f0b7a76
parent 397963db9606f539b1786a392af97ebf089e0a47
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date: Sat, 27 Jun 2020 19:21:54 +0300
Tau approximation algorithm display. Work in progress.
Diffstat:
A | html/tau/2020.html | | | 96 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 96 insertions(+), 0 deletions(-)
diff --git a/html/tau/2020.html b/html/tau/2020.html
@@ -0,0 +1,96 @@
+<!doctype html>
+<html>
+ <head>
+ <meta charset="utf-8" />
+ <title>Tau Day 2020</title>
+ <style tyle="text/css">
+canvas {
+ border: 5px dotted red;
+}
+ </style>
+ <script type="text/javascript">
+'use strict';
+
+(
+ function() {
+ let min_size_factor = 1;
+ let max_size_factor = 100;
+ let current_size_factor_phase = 0.0;
+ let size_factor_phase_increment = 0.01;
+ function lerp(from, to, t) {
+ return to * t + from * (1 - t);
+ }
+ function saw(phase) {
+ let new_phase = phase % 1.0;
+ let signal = 0;
+ if (new_phase < 0.25) {
+ signal = lerp(0, 1, new_phase / 0.25);
+ } else if (new_phase < 0.75) {
+ signal = lerp(1, -1, (new_phase - 0.25) / 0.5);
+ } else {
+ signal = lerp(-1, 0, (new_phase - 0.75) / 0.25);
+ };
+ return signal;
+ }
+ function unit_coordinate_to_canvas_coordinate(unit_x, canvas_length, margin) {
+ return lerp(canvas_length * margin, canvas_length * (1 - margin), (unit_x + 1)/2);
+ }
+ function index_to_unit_coordinate(i, points_on_edge, size_factor) {
+ return (2 * i / (points_on_edge - 1) - 1) * size_factor;
+ }
+ function main() {
+ let canvas = document.querySelector('#canvas');
+ let c = canvas.getContext('2d');
+ c.fillStyle = 'white';
+ c.fillRect(0, 0, canvas.width, canvas.height);
+ let margin = 0.1;
+
+ let size_factor_signal = saw(current_size_factor_phase);
+ let current_size_factor = lerp(max_size_factor, min_size_factor, (size_factor_signal + 1) / 2);
+
+ c.beginPath();
+ c.arc(canvas.width / 2, canvas.height / 2, 0.5 * canvas.width - margin * canvas.width, 0, 2 * Math.PI, false);
+ c.stroke();
+
+ let points_on_edge = 100;
+ let number_of_points = 0;
+ let number_of_points_inside = 0;
+ for (let i = 0; i < points_on_edge; ++i) {
+ for (let j = 0; j < points_on_edge; ++j) {
+ let unit_x = index_to_unit_coordinate(i, points_on_edge, current_size_factor);
+ let unit_y = index_to_unit_coordinate(j, points_on_edge, current_size_factor);
+ if ((unit_x > 1) || (unit_y > 1) || (unit_x < -1) || (unit_y < -1)) {
+ continue;
+ }
+
+ let canvas_x = unit_coordinate_to_canvas_coordinate(unit_x, canvas.width, margin);
+ let canvas_y = unit_coordinate_to_canvas_coordinate(unit_y, canvas.height, margin);
+
+ number_of_points += 1
+
+ if ((unit_x * unit_x + unit_y * unit_y) < 1) {
+ c.fillStyle = 'red';
+ number_of_points_inside += 1;
+ } else {
+ c.fillStyle = 'black';
+ };
+ c.beginPath();
+ c.arc(canvas_x, canvas_y, 1, 0, 2 * Math.PI, false);
+ c.fill();
+ }
+ };
+
+ current_size_factor_phase = (current_size_factor_phase + size_factor_phase_increment) % 1.0;
+
+ window.requestAnimationFrame(main);
+ };
+
+ window.addEventListener('load', main);
+ }
+)();
+ </script>
+ </head>
+ <body>
+ <canvas id="canvas" width="500" height="500"></canvas>
+ </body>
+</html>