-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandel.js
385 lines (325 loc) · 9.36 KB
/
mandel.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
function mandel(x0,y0,thresh){
var x = 0;
var y = 0;
var i = 0;
for (i = 0; i < thresh; i++) {
//z = z**2 + c
//z = (x + iy)**2 + x0 + iy0
//z = x*x - y * y + i2xy + x0 + iy0
//z = x*x - y * y + x0 + i(2xy + y0)
var xtemp = x;
x = x * x - y * y + x0;
y = 2 * xtemp * y + y0;
if ( x * x + y * y > 4 ) {
return i;//"white";
}
}
return i;//"black";
}
/*
x0 = -1.8250624142661178
y0 = 0.298670096021948
smooth_mandel(x0,y0,thresh)
0.05949693252401459
x0 = -1.8250624142661178
y0 = 0.2995699588477368
smooth_mandel(x0,y0,thresh)
0.07643118176607885
*/
function smooth_mandel(x0, y0, thresh) {
var x = 0;
var y = 0;
var i = 0;
var P = 2;
for (i = 0; i < thresh; i++) {
//z = z**P + c
//z = (x + iy)**P + x0 + iy0
//z = x*x - y * y + i2xy + x0 + iy0
//z = x*x - y * y + x0 + i(2xy + y0)
var xtemp = x;
var abs_z;
x = x * x - y * y + x0;
y = 2 * xtemp * y + y0;
abs_z2 = x * x + y * y;
if ( abs_z2 > P * P * 10 ) {
/* \nu(z) = n - \log_P (\log(z_n)/(2 * \log(N)) ),\, */
return (i + 1 - (Math.log(Math.log(Math.sqrt(abs_z2)) ) ) / Math.log(P)) / thresh;
}
}
return 1;
}
var threshold = 0;
var old_x;
var old_y;
var delta_x;
var delta_y;
var canvas_x_offset;
var canvas_y_offset;
var mouseUp = 1;
function mousedown(e)
{
old_x = e.clientX;
old_y = e.clientY;
mouseUp = 0;
//alert(e.clientX + " " + e.clientY);
}
function mouseup(e)
{
delta_x = e.clientX - old_x;
delta_y = e.clientY - old_y;
//alert(center_x + " " + center_y);
//shift_canvas(delta_x, delta_y);
//setTimeout(draw(1), 1);
draw(1);
mouseUp = 1;
delta_x = 0;
delta_y = 0;
}
function mousemove(e)
{
var coordDiv = document.getElementById('coord');
var canvas = document.getElementById('mandel');
var x = e.pageX - canvas_x_offset;
var y = e.pageY - canvas_y_offset;
var fx_min = fx_origin - pixel_size * canvas.width / 2;
var fy_min = fy_origin - pixel_size * canvas.height / 2;
var x0 = fx_min + pixel_size * x;
var y0 = fy_min + pixel_size * y;
coordDiv.innerHTML = "(" + x + "," + y + ") (" + x0 + "," + y0 +
")-> " + smooth_mandel(x0, y0, iterations);
if (mouseUp) {
return;
}
delta_x = e.clientX - old_x;
delta_y = e.clientY - old_y;
old_x = e.clientX;
old_y = e.clientY;
draw(1);
}
function dblclick(e)
{
/* We have an origin (fx0,fy0). We double click on cX,cY.
* cX,cY correlate to fX,fY. Just set fx0 to fX?
*/
var canvas = document.getElementById('mandel');
var dx = e.pageX - canvas_x_offset;
var dy = e.pageY - canvas_y_offset;
fx_origin = fx_origin + pixel_size * (dx - canvas.width / 2);
fy_origin = fy_origin + pixel_size * (dy - canvas.height / 2);
delta_x = 0;
delta_y = 0;
mouseUp = 1;
draw(0);
}
function shift_canvas(canvas)
{
if (canvas.getContext){
var ctx = canvas.getContext('2d');
}
var idata = ctx.getImageData(0, 0, canvas.width, canvas.height);
ctx.putImageData(idata, delta_x, delta_y);
//ctx.translate(delta_x, delta_y);
// 41 Quadrants
// 32
if (delta_x > 0) {
x_min = 0;
x_max = delta_x + 1;
// 34
} else {
x_min = canvas.width + delta_x - 1;
x_max = canvas.width;
// 12
}
if (delta_y > 0) {
y_min = 0;
y_max = delta_y + 1;
// 14
} else {
y_min = canvas.width + delta_y - 1;
y_max = canvas.width;
// 23
}
sub_draw(canvas, x_min, 0, x_max, canvas.height)
sub_draw(canvas, x_min, y_min, x_max, y_max)
//draw (x_min to x_max) from 0 to height
// (y_min to y_max) from 0 to width
}
function draw(moved)
{
var canvas = document.getElementById('mandel');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
}
update_window_scale();
var x_min = 0;
var x_max = canvas.width;
var y_min = 0;
var y_max = canvas.height;
if (moved) {
fx_origin = fx_origin - pixel_size * delta_x;
fy_origin = fy_origin - pixel_size * delta_y;
}
sub_draw(canvas, x_min, y_min, x_max, y_max)
}
function sub_draw(canvas, x_min, y_min, x_max, y_max) {
if (canvas.getContext){
var ctx = canvas.getContext('2d');
}
var idata = ctx.getImageData(0, 0, canvas.width, canvas.height);
var warn = 0;
var min_color = 1;
/* 0 x xmax
* 0+----------+
* | | | px,py (0<px<xmax,0<py<ymax)
* y|-----p----| ->
* | | | fx (fx_center - window/2 < fx < fx_center + window/2)
* | | | fx (fx_min < fx < fx_min + window)
* y+-----+----+ fx_min = fx_center - window/2
*max fx = fx_min + window * (px / xmax)
*
* We have canvas coordinates and mandel coordinates.
* Canvas matters for drawing and shifting, but mandel should maintain
* the "picture origin" as it matters for zooming to remain consistent.
*/
var fx_min = fx_origin - pixel_size * canvas.width / 2;
var fy_min = fy_origin - pixel_size * canvas.height / 2;
var scaleDiv = document.getElementById('scale');
scaleDiv.innerHTML = "(" + fx_min + " = " + fx_origin + " - " + pixel_size * canvas.width / 2 + ")\n(" + fy_min + " = " + fy_origin + " - " + pixel_size * canvas.height / 2 + ")" + pixel_size * canvas.height + " " + canvas.width + "," + canvas.height + "\n(" + pixel_size * canvas.width + "," + pixel_size * canvas.height + ")";
for (y = y_min; y < y_max; y++) {
for (x = x_min; x < x_max; x++) {
var x0 = fx_min + pixel_size * x;
var y0 = fy_min + pixel_size * y;
var color = smooth_mandel(x0, y0, iterations);
if (color < min_color) {
min_color = color;
}
// 42 43 y=463 transition
if (0.49 < color && color < 0.5 && warn) {
alert(x0 + " " + y0 + " " + color);
warn = 0;
}
var rcolor = Math.sin(Math.PI * 1 * color);
var gcolor = Math.sin(Math.PI * 2 * color);
var bcolor = Math.sin(Math.PI * 3 * color);
var rnum = Math.floor(rcolor * 255);
var gnum = Math.floor(gcolor * 255);
var bnum = Math.floor(bcolor * 255);
if (warn) {
alert(rnum + " " + gnum + " " + bnum);
warn = 0;
}
idata.data[((y *(idata.width * 4)) + (x * 4)) + 0] = rnum;
idata.data[((y *(idata.width * 4)) + (x * 4)) + 1] = gnum;
idata.data[((y *(idata.width * 4)) + (x * 4)) + 2] = bnum;
idata.data[((y *(idata.width * 4)) + (x * 4)) + 3] = 255;
}
}
ctx.putImageData(idata, 0, 0);
document.getElementById('nums').innerHTML = delta_x + "," + delta_y + " (" + fx_origin + "," + fy_origin + ")";
draw_axis()
}
var i = 0;
function draw_loop() {
if (i < 19) {
draw(0);
iterations = i;
i++;
setTimeout(draw_loop, 100);
}
}
var iterations;
var fx_origin = 0;
var fy_origin = 0;
var window_size;
var window_size_x = 1;
var window_size_y = 1;
var vpheight;
var vpwidth;
var pixel_size;
function update_window_scale() {
if (vpheight < vpwidth) {
window_size_y = 1;
window_size_x = vpwidth / vpheight;
pixel_size = window_size / vpheight;
} else {
window_size_y = vpheight / vpwidth;
window_size_x = 1;
pixel_size = window_size / vpwidth;
}
}
function reset_view() {
delta_x = 0;
delta_y = 0;
fx_origin = 0;
fy_origin = 0;
window_size = 4.1;
mouseUp = 1;
iterations = 20;
}
function init() {
var canvas = document.getElementById('mandel');
var container = document.getElementById('container');
container.onmousedown = mousedown;
container.onmouseup = mouseup;
container.onmousemove = mousemove;
container.ondblclick = dblclick;
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
vpheight = canvas.height;
vpwidth = canvas.width;
canvas_x_offset = 0;
canvas_y_offset = 0;
var curElem = canvas;
// accumulate all offset; not just the canvas itself.
do {
canvas_x_offset += curElem.offsetLeft;
canvas_y_offset += curElem.offsetTop;
} while (curElem = curElem.offsetParent)
}
var zoom_factor = 1.5;
function zoom_in() {
window_size /= zoom_factor;
iterations += 4;
draw(0);
}
function zoom_out() {
window_size *= zoom_factor;
iterations -= 4;
draw(0);
}
function reset() {
reset_view();
draw(0);
}
var draw_axes = 0
function draw_axis() {
if (!draw_axes) {
return;
}
var axes = document.getElementById('axes');
axes.style.display = "inline";
return;
}
function undraw_axes() {
var axes = document.getElementById('axes');
axes.style.display = "none";
return;
}
function toggle_axes() {
draw_axes = ! draw_axes;
//draw(0);
button = document.getElementById('axes_button');
if (draw_axes) {
draw_axis()
button.value = "axes off";
} else {
undraw_axes();
button.value = "axes on";
}
}
function draw_one() {
init();
reset_view();
draw(0);
draw_axis();
}