Hong-Phuc Bui
2025-01-16 2889de7f0c2d587a17fbd322af57c29e84238620
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* 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 pointGenerator = ifsGenerator(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 = pointGenerator.next().value;
                board.create('point', point, pointOption);
                countPlottedPointsEle.innerText = i;
                resolve();
            }, 0);
        });
        await promise;
    }
}
 
function* ifsGenerator(matrix) {
    let p = [];
    for(let i = 0; i < matrix.length; ++i) {
        p.push(matrix[i][6]);
    }
    let x = 0, y = 0;
    while (true) {
        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;
        yield [x, y];
    }
}