-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarnsley.html
245 lines (234 loc) · 9.51 KB
/
barnsley.html
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width,initial-scale=1.0">
<meta name="description" content="Iterated Function Systems and L-Systems">
<meta name="keywords" content="IFS,iterated,function system,
Lindenmayer System, L-system">
<meta name="theme-color" content="#00519e" />
<title>Barnsley Fern</title>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script src="draggable-canvas.js"></script>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="fern-style.css">
</head>
<body>
<header>
<h1>Barnsley-Fern</h1>
<a href="index.html" id="back">
<img src="https://fonts.gstatic.com/s/i/materialicons/keyboard_arrow_left/v6/24px.svg" alt="Go back"/>
</a>
</header>
<main>
<div id="pane-settings" class="pane">
<div class="pane-title"><h2>Settings</h2></div>
<div class="pane-content scrollable-y">
<center>adapted from
<a href="http://jsbin.com/fevira/1/edit?html,js,output">
http://jsbin.com/fevira/1/edit?html,js,output</a></center>
<br/>
<form onchange="updateSettings();">
<p>
The functions \(f_n\) have the form
\[ f_n \left(\pmatrix{x\\y}\right) := \pmatrix{a & b \\ c
& d} \pmatrix{x \\ y} + \pmatrix{e \\ f} \]
and will be applied with a probability of \(p_n\)
</p>
<table>
<tbody>
<tr>
<th class="greyedout">\(n\)</th>
<th>\(p_n\)</th>
<th>\(a\)</th>
<th>\(b\)</th>
<th>\(c\)</th>
<th>\(d\)</th>
<th>\(e\)</th>
<th>\(f\)</th>
<!-- <th class="greyedout">\(p_n\)</th> -->
<!-- <th class="greyedout">\(\det f_n\)</th> -->
</tr>
<tr>
<td style="color: red">1</td>
<td><input type="number" min="0" max="100" value="1" id="s1-p" class="int"></td>
<td><input type="number" value="0.0" step="0.01" id="s1-a" class="float"></td>
<td><input type="number" value="0.0" step="0.01" id="s1-b" class="float"></td>
<td><input type="number" value="0.0" step="0.01" id="s1-c" class="float"></td>
<td><input type="number" value="0.16" step="0.01" id="s1-d" class="float"></td>
<td><input type="number" value="0.0" step="0.01" id="s1-e" class="float"></td>
<td><input type="number" value="0.0" step="0.01" id="s1-f" class="float"></td>
</tr>
<tr>
<td style="color: green">2</td>
<td><input type="number" min="0" max="100" value="85" id="s2-p" class="int"></td>
<td><input type="number" value="0.85" step="0.01" id="s2-a" class="float"></td>
<td><input type="number" value="0.04" step="0.01" id="s2-b" class="float"></td>
<td><input type="number" value="-0.04" step="0.01" id="s2-c" class="float"></td>
<td><input type="number" value="0.85" step="0.01" id="s2-d" class="float"></td>
<td><input type="number" value="0.0" step="0.01" id="s2-e" class="float"></td>
<td><input type="number" value="1.6" step="0.01" id="s2-f" class="float"></td>
</tr>
<tr>
<td style="color: blue">3</td>
<td><input type="number" min="0" max="100" value="7" id="s3-p" class="int"></td>
<td><input type="number" value="0.20" step="0.01" id="s3-a" class="float"></td>
<td><input type="number" value="-0.26" step="0.01" id="s3-b" class="float"></td>
<td><input type="number" value="0.23" step="0.01" id="s3-c" class="float"></td>
<td><input type="number" value="0.22" step="0.01" id="s3-d" class="float"></td>
<td><input type="number" value="0.0" step="0.01" id="s3-e" class="float"></td>
<td><input type="number" value="1.6" step="0.01" id="s3-f" class="float"></td>
</tr>
<tr>
<td style="color: purple">4</td>
<td><input type="number" min="100" max="100" value="7" id="s4-p" class="int" disabled></td>
<td><input type="number" value="-0.15" step="0.01" id="s4-a" class="float"></td>
<td><input type="number" value="0.28" step="0.01" id="s4-b" class="float"></td>
<td><input type="number" value="0.26" step="0.01" id="s4-c" class="float"></td>
<td><input type="number" value="0.24" step="0.01" id="s4-d" class="float"></td>
<td><input type="number" value="0.0" step="0.01" id="s4-e" class="float"></td>
<td><input type="number" value="0.44" step="0.01" id="s4-f" class="float"></td>
</tr>
</tbody>
</table>
<br/>
<input type="number" style="width: 6em" min="100" value="300" step="100" id="sn-iterruns" name="iterruns">
<label for="iterruns">number of iterations (x1000)</label>
<br/>
<input type="number" style="width: 6em" min="20" value="70" step="10" id="s-zfactor" name="zfactor">
<label for="zfactor">scale factor</label>
<br/>
<input type="checkbox" id="s-use-colors" name="usecolors">
<label for="usecolors">use colors</label>
<span style="display: block">
<input type="reset" style="float: right;" class="danger" value="reset options" onclick="updateSettings();">
<input type="button" style="float: right;" class="semidanger" value="reset canvas" onclick="setupCanvas();">
<input type="button" style="float: right;" class="greenbox" value="draw!" onclick="updateSettings();" id="drawbtn">
</span>
</form>
</div>
<div class="pane-bottom"></div>
</div>
<div id="pane-preview" class="pane">
<div class="pane-title"><h2>Preview</h2></div>
<div class="pane-content">
<div id="scrollable-canvas" class="">
<canvas id="canvas" width="1000px" height="1000px"></canvas>
</div>
</div>
<div class="pane-bottom"></div>
</div>
</main>
<script>
drawBtn = document.getElementById("drawbtn");
c = new DraggableCanvas(document.getElementById("canvas"));
c.register(0.5, 0.9);
function setDrawButton(disabled) {
var btn = document.getElementById("drawbtn");
btn.disabled = disabled;
btn.value = disabled ? "drawing..." : "draw!";
}
function checkContraction(s, i) {
var a = s["a"], b = s["b"], c = s["c"], d = s["d"];
var u = a ** 2 + c ** 2, v = b ** 2 + d ** 2, w = a * b + c * d;
if (u == v && (u + v) / 2 + u - v >= 1) {
var msg = `f_${i+1} is not a contraction!\nu=${u},\nv=${v},\nw=${w}`;
console.error(msg); alert(msg); return 1;
}
var sign = u > v ? 1 : -1;
var s = 2 * w / (u - v);
var t = 1 / Math.sqrt(1 + s ** 2);
if ((u + v) / 2 + (Math.abs(u - v) / 2 + 2 * sign * w * s) * t >= 1) {
var msg = `f_${i+1} is not a contraction!\nu=${u}\nv=${v}\nw=${w}\ns=${s}\nt=${t}`;
console.error(msg); alert(msg); return 1;
}
}
function readRules() {
var nrules = [{"color": "red"}, {"color": "green"},
{"color": "blue"}, {"color": "purple"}];
for (var i = 1; i < 5; i++) {
for (x of "abcdef") {
nrules[i - 1][x] = 1 * document.getElementById("s" + i + "-" + x).value;
}
}
var tp = 100;
for (var i = 1; i < 4; i++) {
nrules[i - 1]["p"] = (1 * document.getElementById("s" +
i + "-p").value) / 100;
tp -= 1 * document.getElementById("s" + i + "-p").value
}
nrules[3]["p"] = tp / 100;
document.getElementById("s4-p").value = tp;
if (nrules[3]["p"] < 0) return [nrules, "probability limit"];
var check = true;
for (var i = 0; i < 4; i++) {
var x = nrules[i];
// var d = x["det"] = x["a"] * x["d"] - x["b"] * x["c"];
checkContraction(x, i);
}
return [nrules, check];
}
function getRule(rules) {
var rand = Math.random();
for(var i = 0; i < rules.length; i++) {
var rule = rules[i];
if(rand < rule.p) {
return rule;
}
rand -= rule.p;
}
console.warn("unreachable")
return rules[3];
}
function plot(x, y, color, zfactor) {
// TODO
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.fillStyle = color;
context.fillRect(x * zfactor, -y * zfactor, 0.5, 0.5);
}
function iterate(rules, counter) {
if (counter <= 0) {
console.info("Redraw finished");
setDrawButton(false);
return;
}
// if (counter % 100 == 0) {
// console.debug("iterate(" + counter + ") called with the following rules:");
// console.debug(rules);
// }
var usecolors = document.getElementById("s-use-colors").checked;
var zfactor = 1 * document.getElementById("s-zfactor").value;
var x = Math.random();
var y = Math.random();
for(var i = 0; i < 1000; i++) {
var rule = getRule(rules);
var x1 = x * rule.a + y * rule.b + rule.e;
var y1 = x * rule.c + y * rule.d + rule.f;
x = x1;
y = y1;
plot(x, y, (usecolors ? rule.color : "forestgreen"), zfactor);
}
requestAnimationFrame(function(){iterate(rules, counter - 1);});
}
function updateSettings() {
var res = readRules();
var rules = res[0], detcheck = res[1];
console.info(rules);
if (detcheck) {
console.info("Redraw");
c.clear();
setDrawButton(true);
iterate(rules, 1 * document.getElementById("sn-iterruns").value);
} else {
for (var i=0; i < 4; i++) {
if (Math.abs(rules[i]["det"]) >= 1) {
alert("|det f_" + (i+1) + "| is too large!");
}
}
}
}
</script>
</body>
</html>