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
| /**
| * 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]);
| }
| }
|
|