/** * barnsley-fern-functional.js * */ import "./lib/dfhi.js"; import * as JXG from "jsxgraph" ; import {exportBoard} from "./lib/dfhi-plot"; let board = undefined; function initBoard () { return JXG.JSXGraph.initBoard('plotter', { boundingbox: [-6, 12, 6, -1], axis: false, keepAspectRatio:true }); } window.init = function() { board = initBoard(); document.getElementById("export") .addEventListener("click", (event) => exportBoard(board)); }; window.main = function(...argv) { if (board) { JXG.JSXGraph.freeBoard(board); board = undefined; board = initBoard(); } let n = Number.parseInt(argv[0], 10); prePlot(); barnsley(n, appendPointToBoard); postPlot(); }; function prePlot() { board.suspendUpdate(); } function postPlot() { board.unsuspendUpdate(); board.dehighlightAll(); } function appendPointToBoard(point) { const pointOption = {withLabel:false, fixed:true, size:0.001, fillColor:"#00aa00", strokeWidth:0}; board.create('point', point, pointOption ); } function barnsley(n, plotFn) { const m = [ [ 0 , 0 , 0 , 0.16 , 0.500 , 0 , 0.01], [ 0.85 , 0.04 , -0.04 , 0.85 , 0.075 , 1.60 , 0.85], [ 0.20 , -0.26 , 0.23 , 0.22 , 0.400 , 1.60 , 0.07], [-0.15 , 0.28 , 0.26 , 0.24 , 0.575 , 0.44 , 0.07] ]; const p = [ m[0][6], m[0][6] + m[1][6], m[0][6] + m[1][6] + m[2][6] ]; let x = 0, y = 0; for(let i = 0; i < n; ++i) { const r = Math.random(); let f = 0; if (r < p[0]) { f = 0; }else if (r < p[1]) { f = 1; }else if (r < p[2]) { f = 2; } else { f = 3; } let nX = (m[f][0] * x) + (m[f][1] * y) + m[f][4]; y = (m[f][2] * x) + (m[f][3] * y) + m[f][5]; x = nX; plotFn([x, y]); } }