1/* 2 * Mandelbrot example: 3 * 4 * $ ./duk mandel.js 5 * [...] 6 */ 7 8function mandel() { 9 var w = 76, h = 28, iter = 100; 10 var i, j, k, c; 11 var x0, y0, xx, yy, xx2, yy2; 12 var line; 13 14 for (i = 0; i < h; i++) { 15 y0 = (i / h) * 2.5 - 1.25; 16 17 for (j = 0, line = []; j < w; j++) { 18 x0 = (j / w) * 3.0 - 2.0; 19 20 for (k = 0, xx = 0, yy = 0, c = '#'; k < iter; k++) { 21 /* z -> z^2 + c 22 * -> (xx+i*yy)^2 + (x0+i*y0) 23 * -> xx*xx+i*2*xx*yy-yy*yy + x0 + i*y0 24 * -> (xx*xx - yy*yy + x0) + i*(2*xx*yy + y0) 25 */ 26 27 xx2 = xx*xx; yy2 = yy*yy; 28 29 if (xx2 + yy2 < 4.0) { 30 yy = 2*xx*yy + y0; 31 xx = xx2 - yy2 + x0; 32 } else { 33 /* xx^2 + yy^2 >= 4.0 */ 34 if (k < 3) { c = '.'; } 35 else if (k < 5) { c = ','; } 36 else if (k < 10) { c = '-'; } 37 else { c = '='; } 38 break; 39 } 40 } 41 42 line.push(c); 43 } 44 45 print(line.join('')); 46 } 47} 48 49try { 50 mandel(); 51} catch (e) { 52 print(e.stack || e); 53} 54