forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.js
274 lines (247 loc) · 4.53 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
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
268
269
270
271
272
273
274
/*
@title: Mandelbrot
@author: Henry
Instructions:
Explore the Mandelbrot set!
Use W, A, S, D to move around
Use I, K to zoom
Use J, L to enhance (Useful to decrease res when zoomed in, to load faster)
*/
let n;
const a = "0";
const b = "1";
const c = "2";
const d = "3";
const e = "4";
const f = "5";
const g = "6";
// Try changing these, and see what happens!
let i = 0
let j = 0
let res = 32 * 1.25
let max = res
let xoff = 0
let yoff = 0
let maxiter = 64
let resfac = 1.25
let movefac = 0.2
let zoomfac = 0.2
setLegend(
[ a, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
[ b, bitmap`
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444
4444444444444444`],
[ c, bitmap`
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333`],
[ d, bitmap`
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555`],
[ e, bitmap`
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666`],
[ f, bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777`],
[ g, bitmap`
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888`]
);
class Comp {
constructor(r, i) {
this.r = r;
this.i = i;
}
add(other) {
return new Comp(this.r + other.r, this.i + other.i);
}
abs() {
return Math.sqrt(Math.pow(this.r, 2) + Math.pow(this.i, 2))
}
sqr() {
return new Comp((this.r * this.r) - (this.i * this.i), (this.r * this.i) + (this.i * this.r));
}
}
function mand(x, y, j, k) {
// With just a few tweaks, this becomes a Julia Set viewer!
let c = new Comp(x, y)
//let c = new Comp(-0.64, -0.45)
let n = 0
let z = new Comp(i, j)
//let z = new Comp(x, y)
while (z.abs() <= 2 && n < (Math.log2(max) * 32)) {
z = c.add(z.sqr())
n += 1
}
return [n, z.abs()]
}
function render(i, j) {
var screen = ``
for (let x = 0; x < res; x++) {
for (let y = 0; y < res; y++) {
n = mand(((y / max) * 2) - 1.35 - xoff, ((x / max) * 2) - 1 - yoff, i, j)
if (n[1] < 2) {
screen += "0"
} else if (n[0] > 64 * 0.6) {
screen += "2"
} else if (n[0] > 64 * 0.4) {
screen += "5"
} else if (n[0] > 64 * 0.3) {
screen += "3"
} else if (n[0] > 64 * 0.2) {
screen += "6"
} else if (n[0] > 64 * 0.1) {
screen += "4"
} else {
screen += "5"
}
}
screen += "\n"
}
setMap(screen);
clearText()
addText("Zoom: 2^" + Math.log2(1 / (res/max)), {
x: 2,
y: 0,
color: [ 0, 50, 50 ]
})
}
render(i, j)
onInput("w", () => {
yoff += movefac * (res/max)
render(i, j)
})
onInput("s", () => {
yoff -= movefac * (res/max)
render(i, j)
})
onInput("a", () => {
xoff += movefac * (res/max)
render(i, j)
})
onInput("d", () => {
xoff -= movefac * (res/max)
render(i, j)
})
onInput("i", () => {
max *= 2
yoff -= 1 * (res/max)
xoff -= 1 * (res/max)
render(i, j)
})
onInput("k", () => {
max *= 0.5
yoff += 0.5 * (res/max)
xoff += 0.5 * (res/max)
render(i, j)
})
onInput("j", () => {
res *= resfac
max *= resfac
render(i, j)
})
onInput("l", () => {
res /= resfac
max /= resfac
render(i, j)
})