diff --git a/public/main.js b/public/main.js index 222d9d6..c3fa20d 100644 --- a/public/main.js +++ b/public/main.js @@ -36,6 +36,12 @@ var touch_identifier var moving_origin = [] var moving_epsilon = 0.04 +// brownian motion constants +var brownian_callback = false +var brownian_eps = 0.1 +var brownian_time = 50 +var brownian_time_elapsed = 0 + function p_to_canv_coords(x, y) { return [p_canvas.width * (x + p_scale) / (2 * p_scale), p_canvas.height * (-y + p_scale) / (2 * p_scale)] } @@ -244,7 +250,21 @@ function zoom_out_dr() { update_zoom() } +function clear_matrix() { + brownian_time_elapsed = 0 + + matrix_entries = [] + for (let i = 0; i < 2 * dim * dim; i++) { + matrix_entries[i] = 0 + } + + p_draw() + dr_draw() +} + function randomize_matrix() { + brownian_time_elapsed = 0 + matrix_entries = [] for (let i = 0; i < dim * dim; i++) { matrix_entries.push((6.0 * Math.random()) - 3.0) @@ -302,6 +322,8 @@ function enter_matrix() { } } + brownian_time_elapsed = 0 + matrix_entries = entries make_grid() p_draw() @@ -310,6 +332,8 @@ function enter_matrix() { } function dim_update() { + brownian_time_elapsed = 0 + var old_dim = dim dim = Number(document.getElementById("dim").value) update_dr_scale() @@ -356,6 +380,8 @@ function dim_update() { var input_timers = {}; function input_update(e) { + brownian_time_elapsed = 0 + const input = e.srcElement; const row = input.dataset.row; const col = input.dataset.col; @@ -546,3 +572,58 @@ function mouse_up(e) { mouse_moving = false } } + +function brownian_checkbox() { + if (document.getElementById('brownian').checked) { + brownian_time_elapsed = 0 + brownian_callback = window.setInterval(brownian_motion, brownian_time) + document.getElementById('brownian_modifiers').style.display = 'block' + } else { + window.clearInterval(brownian_callback) + document.getElementById('brownian_modifiers').style.display = 'none' + } +} + +function brownian_freq_update() { + f = document.getElementById('brownian_freq').value + if (f < 10) f = 10 + if (f > 10000) f = 10000 + document.getElementById('brownian_freq').value = f + + if (document.getElementById('brownian').checked) { + window.clearInterval(brownian_callback) + brownian_callback = window.setInterval(brownian_motion, brownian_time) + } +} + +function brownian_eps_update() { + e = document.getElementById('brownian_eps').value + if (e <= 0) e = 0 + if (e > 6) e = 6 + document.getElementById('brownian_eps').value = e + brownian_eps = e +} + +function brownian_motion() { + brownian_time_elapsed += brownian_time + + entries = matrix_entries + for (let i = 0; i < 2 * dim * dim; i++) { + entries[i] += brownian_eps * Math.sqrt(0.0001 * brownian_time_elapsed) * gaussian_random() + if (-3 > entries[i]) entries[i] = -3; + if (entries[i] > 3) entries[i] = 3; + } + + matrix_entries = entries + make_grid() + p_draw() + dr_draw() +} + +// https://stackoverflow.com/a/36481059 +function gaussian_random(mean = 0, stdev = 0.5) { + const u = 1 - Math.random(); + const v = Math.random(); + const z = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v ); + return z * stdev + mean; +} \ No newline at end of file diff --git a/public/tjsm.html b/public/tjsm.html index b5235e7..0ee7cdc 100644 --- a/public/tjsm.html +++ b/public/tjsm.html @@ -48,6 +48,7 @@ canvas { + @@ -58,6 +59,14 @@ canvas {
+Brownian motion + + +
+ paper by Otte Heinävaara. source.