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
84
/*
* ifs.js
*
*/
 
import "./lib/dfhi.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
    });
}
 
window.init = function() {
    board = initBoard();
    function exportSvg(event) {
        exportBoard(board);
    }
    document.getElementById("export")
        .addEventListener("click", exportSvg);
};
 
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]
];
 
window.main = function(...argv) {
    if (board) {
        try {
            JXG.JSXGraph.freeBoard(board);
            board = undefined;// remove all child of board
            board = initBoard();
        }catch (e) {
            console.log(e);
            console.log(board);
        }
    }
    let n = Number.parseInt(argv[0]);
    let points = ifs (tanenbaum, n);
    let opt = {withLabel:false, fixed:true, size:0.001, fillColor:"#00aa00", strokeWidth:0};
    plotBarnsley(board, points, opt);
};
 
 
function ifs(matrix, iteration) {
    let p = [];
    for(let i = 0; i < matrix.length; ++i) {
        p.push(matrix[i][6])
    }
    let x = 0, y = 0;
    let points = [ [x, y] ];
    for(let i = 0; i < iteration; ++i ) {
        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;
        points.push([x,y]);
    }
    return points;
}
 
/**
 *
 * @param board
 * @param points
 * @param attr
 *
 * */
function plotBarnsley(board, points, attr ) {
    for(let p of points) {
        board.create('point', p, attr );
    }
}