-
Notifications
You must be signed in to change notification settings - Fork 22
/
Mandelbrot.js
64 lines (49 loc) · 1.28 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
62
63
64
class ComplexNumber {
constructor(a, b) {
this.a = a;
this.b = b;
}
squared() {
return new ComplexNumber((Math.pow(this.a, 2) - Math.pow(this.b, 2)), 2 * this.a * this.b);
}
magnitude() {
return Math.sqrt(Math.pow(this.a, 2) + Math.pow(this.b, 2));
}
add(number) {
return new ComplexNumber(this.a + number.a, this.b + number.b);
}
}
const iterations = 32;
const max = 8;
return class SandEffect {
constructor(display) {
this.display = display;
for (let y = 0; y < this.display.height; y++) {
for (let x = 0; x < this.display.width; x++) {
let c = new ComplexNumber(((x - this.display.width / 2) - 16) / 30, (y - this.display.height / 2) / 30);
let i = this.#mandelbrot(c);
let colour;
if (i < iterations) {
colour = [i / iterations * 255, i / iterations * 255, i / iterations * 255];
} else {
colour = [0, 0, 0];
}
this.display.setPixel(x, y, colour);
}
}
this.display.flush();
}
update() {}
#mandelbrot(c) {
let i = 0;
let z = new ComplexNumber(0, 0);
for (let i = 0; i < iterations; i++) {
z = z.squared().add(c)
i++;
if (z.magnitude() > max) {
return i;
}
}
return iterations;
}
}