/*
|
* ifs.js
|
*
|
*/
|
|
import * as JXG from "jsxgraph" ;
|
|
import * as DHFIRandom from "./lib/dfhi-random";
|
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
|
});
|
}
|
|
document.addEventListener("DOMContentLoaded", () => {
|
board = initBoard();
|
function exportSvg(event) {
|
exportBoard(board);
|
}
|
document.getElementById("export")
|
.addEventListener("click", exportSvg);
|
document.getElementById("run")
|
.addEventListener("click", run);
|
});
|
|
const tanenbaum = [
|
[ 0.0, 0.0, 0.0, 0.160, 0.0, 0.0, 0.03],
|
[ 0.197, -0.026, 0.226, 0.197, 0.0, 1.6, 0.14],
|
[-0.155, 0.283, 0.260, 0.237, 0.0, 0.44, 0.27],
|
[-0.849, 0.037,-0.037, 0.849, 0.0, 1.6, 0.56]
|
];
|
|
async function run() {
|
if (board) {
|
try {
|
JXG.JSXGraph.freeBoard(board);
|
board = undefined;// remove all child of board
|
board = initBoard();
|
}catch (e) {
|
console.log(e);
|
console.log(board);
|
}
|
}
|
const numOfPointEle = document.getElementById("num-of-point").value;
|
const n = Number.parseInt(numOfPointEle);
|
const pointOption = {withLabel:false, fixed:true, size:0.001, fillColor:"#00aa00", strokeWidth:0};
|
const pointGeneratorFn = ifsWrapper(tanenbaum);
|
const maxPlottedPointsEle = document.getElementById("max-plotted-point");
|
maxPlottedPointsEle.innerText = n;
|
const countPlottedPointsEle = document.getElementById("count-plotted-point");
|
for(let i = 1; i <= n; ++i) {
|
const promise = new Promise((resolve, _reject) => {
|
setTimeout(() => {
|
const point = pointGeneratorFn();
|
board.create('point', point, pointOption);
|
countPlottedPointsEle.innerText = i;
|
resolve();
|
}, 0);
|
});
|
await promise;
|
}
|
}
|
|
function ifsWrapper(matrix) {
|
let p = [];
|
for(let i = 0; i < matrix.length; ++i) {
|
p.push(matrix[i][6]);
|
}
|
let x = 0, y = 0;
|
return function() {
|
let r = DHFIRandom.discrete(p);
|
let nX = (matrix[r][0] * x) + (matrix[r][1] * y) + matrix[r][4];
|
y = (matrix[r][2] * x) + (matrix[r][3] * y) + matrix[r][5];
|
x = nX;
|
console.log({x, y});
|
return [x, y];
|
}
|
}
|