-
Notifications
You must be signed in to change notification settings - Fork 9
/
mandelbrot.js
61 lines (61 loc) · 2.23 KB
/
mandelbrot.js
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
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const WIDTH = 1200;
const HEIGHT = 800;
var data = new Uint8Array(WIDTH * HEIGHT * 4);
function colour(iteration, offset, scale) {
iteration = (iteration * scale + offset) & 1023;
if (iteration < 256) {
return iteration;
}
else if (iteration < 512) {
return (255 - (iteration - 255));
}
return 0;
}
function iterateEquation(x0, y0, maxiterations) {
let a = 0.0, b = 0.0, rx = 0.0, ry = 0.0;
let iterations = 0;
while (iterations < maxiterations && (rx * rx + ry * ry <= 4)) {
rx = a * a - b * b + x0;
ry = 2 * a * b + y0;
a = rx;
b = ry;
iterations++;
}
return iterations;
}
// @inline
function scale(domainStart, domainLength, invScreenLength, step) {
return domainStart + domainLength * (step * invScreenLength - 1.0);
}
function mandelbrot(maxIterations, cx, cy, diameter) {
const invWidth = 1.0 / WIDTH;
const invHeight = 1.0 / HEIGHT;
var verticalDiameter = diameter * HEIGHT * invWidth;
for (let x = 0; x < WIDTH; ++x) {
for (let y = 0; y < HEIGHT; ++y) {
// convert from screen coordinates to mandelbrot coordinates
let rx = scale(cx, diameter, invWidth, x);
let ry = scale(cy, verticalDiameter, invHeight, y);
let iterations = iterateEquation(rx, ry, maxIterations);
let idx = (x + y * WIDTH) << 2;
data[idx + 0] = iterations == maxIterations ? 0 : colour(iterations, 0, 4);
data[idx + 1] = iterations == maxIterations ? 0 : colour(iterations, 128, 4);
data[idx + 2] = iterations == maxIterations ? 0 : colour(iterations, 356, 4);
data[idx + 3] = 255;
}
}
}
exports.mandelbrot = mandelbrot;
function getData() {
return data;
}
exports.getData = getData;
function getDataBuffer() {
return (data.buffer);
}
exports.getDataBuffer = getDataBuffer;
;
});