-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
267 lines (213 loc) · 7.78 KB
/
index.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="css/main.css" type="text/css" rel="stylesheet">
<script src="js/math.js" type="text/javascript"></script>
</head>
<body>
<canvas id="canvas" width="800" height="400"></canvas>
<script>
'use strict';
function clamp(val, min, max) {
return Math.max(min, Math.min(val, max));
}
var AngleField = function(width, height, genfun) {
this.width = width;
this.height = height;
this.field = math.zeros(width, height);
this.generate(genfun);
}
AngleField.prototype.generate = function(genfun) {
for (var x = 0; x < this.width; ++x) {
for (var y = 0; y < this.height; ++y) {
this.field.set([x, y], genfun(x, y));
}
}
}
AngleField.prototype.at = function(x, y) {
return this.field.get([x,y]);
}
var normal = function() {
// Generates a pair of normally distributed numbers
// with mean mu and standard deviation sigma,
// using the Box-Muller transform.
if (typeof normal.hasCachedResult == 'undefined') {
normal.hasCachedResult = false;
}
if (normal.hasCachedResult == true) {
normal.hasCachedResult = false;
return normal.cachedResult;
}
else {
let u = 1.0 - Math.random();
let v = 1.0 - Math.random();
let result = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
normal.cachedResult = Math.sqrt(-2.0 * Math.log(u)) * Math.sin(2.0 * Math.PI * v);
normal.hasCachedResult = true;
return result;
}
}
function logseries(p) {
// Sample from a discrete logarithmic distribution with probability p.
// Probability mass function, pk = p^k:
let pm = function(p, pk, k) {
return -pk / (k * Math.log(1.0 - p));
}
let u = Math.random();
let k = 1;
let pk = p;
let cdf = pm(p, pk, k);
while (cdf < u) {
k += 1;
pk *= p;
cdf += pm(p, pk, k);
}
return k;
}
var computeDepth = function(x) {
// hard-coded constant depth
return canvas.height;
}
function computeLength(stepsize) {
// length of segment: clamped normal distribution with mean=stepsize and
// standard deviation proportional to stepsize
let sigma = 0.25 * stepsize;
let length = stepsize + sigma * normal();
return clamp(length, 0.5 * stepsize, 1.5 * stepsize);
}
function computeNumSplits(origin, length, depthfn) {
let depth = depthfn(origin.x);
let proportionalLength = length / depth;
let progress = origin.y / depth;
let p = progress * progress * proportionalLength;
// number of splits is sampled from logarithmic distribution
return logseries(p);
}
function hasSplit(segment, depthfn) {
let depth = depthfn(segment.origin.x);
let normalizedLength = segment.length / depth;
let progress = segment.origin.y / depth;
let p = progress * normalizedLength * segment.intensity;
return p > Math.random();
}
function computeAngle(segment, depthfn) {
let depth = depthfn(segment.origin.x);
let progress = segment.origin.y / depth;
let angle = clamp(0.5 * progress, 0.1, 0.5) * Math.PI * normal();
return clamp(0.5 * (segment.angle + angle), -0.5 * Math.PI, 0.5 * Math.PI);
}
var Thunder = function(origin, depthfn, intensity, stepsize) {
this.stepsize = stepsize;
this.segmentsProcessed = [];
this.segmentsPending = [{
origin: {
x: origin.x,
y: origin.y
},
angle: 0.0,
intensity: intensity
}];
this.processSegments(depthfn);
}
Thunder.prototype.processSegments = function(depthfn) {
let totalsplits = 0;
while (this.segmentsPending.length > 0) {
let segment = this.segmentsPending.pop();
segment.length = computeLength(this.stepsize);
segment.angle = computeAngle(segment, depthfn);
segment.end = {
x: segment.origin.x + segment.length * Math.sin(segment.angle),
y: segment.origin.y + segment.length * Math.cos(segment.angle)
};
this.segmentsProcessed.push(segment);
if (segment.end.y < depthfn(segment.end.x)) {
let intensity1 = segment.intensity;
let intensity2 = 0.0;
let split = hasSplit(segment, depthfn);
if (split === true) {
totalsplits += 1;
let u = Math.random();
intensity1 *= u;
intensity2 = segment.intensity - intensity1;
this.segmentsPending.push({
origin: {
x: segment.end.x,
y: segment.end.y
},
angle: segment.angle,
intensity: intensity2
});
}
this.segmentsPending.push({
origin: {
x: segment.end.x,
y: segment.end.y
},
angle: segment.angle,
intensity: intensity1
});
}
}
}
Thunder.prototype.drawSegments = function(context) {
for (let i = 0; i < this.segmentsProcessed.length; ++i) {
context.lineWidth = Math.max(0.25 * this.segmentsProcessed[i].intensity, 0.5);
context.beginPath();
context.moveTo(this.segmentsProcessed[i].origin.x, this.segmentsProcessed[i].origin.y);
context.lineTo(this.segmentsProcessed[i].end.x, this.segmentsProcessed[i].end.y);
context.stroke();
}
}
var clear = function(canvas, color) {
let context = canvas.getContext('2d');
context.beginPath();
context.fillStyle = color;
context.fillRect(0, 0, canvas.width, canvas.height);
}
const eps = 0.1;
const millisecondsPerFrame = 1000.0 / 24.0;
var doStuff = function(canvas) {
if (typeof doStuff.origin == 'undefined') {
doStuff.momentum = 0.0;
doStuff.origin = {
x: Math.random() * canvas.width,
y: 0
};
}
else {
let dp = 1.0 - Math.random() - (doStuff.origin.x - 0.5 * canvas.width);
doStuff.momentum += dp;
if ( (doStuff.origin.x <= 0 && doStuff.momentum < 0)
|| (doStuff.origin.x >= canvas.width && doStuff.momentum > 0) ) {
doStuff.momentum = -doStuff.momentum;
}
doStuff.origin.x += clamp(doStuff.momentum, -2.0, 2.0);
}
clear(canvas, "#FFFFFF");
let thunder = new Thunder(
doStuff.origin,
computeDepth,
10.0,
8.0
);
thunder.drawSegments(canvas.getContext('2d'));
window.setTimeout(doStuff, millisecondsPerFrame, canvas);
}
window.setTimeout(doStuff, millisecondsPerFrame, document.getElementById('canvas'));
// let angleField = new AngleField(
// Math.floor(0.1 * canvas.width),
// Math.floor(0.1 * canvas.height),
// function genfun(x, y) { return normal(0.0, 0.25 * Math.PI); }
// );
// // test normal dist.
// for (var i = 0; i < 100; ++i) {
// console.log("N(0,1) = " + normal());
// }
// // test logarithmic dist.
// for (var i = 0; i < 100; ++i) {
// console.log("L(p) = ", logseries(0.33));
// }
</script>
</body>
</html>