View difference between Paste ID: f0Y19dEt and ypkJy63W
SHOW: | | - or go back to the newest paste.
1
function Mandeliter( cx, cy, maxiter ){
2
  var
3
    x = 0.0,
4
    y = 0.0,
5
    xx = 0,
6
    yy = 0,
7
    xy = 0;
8
 
9
  var i = maxiter;
10
  while( i-- && xx + yy <= 4 ){
11
    xy = x * y;
12
    xx = x * x;
13
    yy = y * y;
14
    x = xx - yy + cx;
15
    y = xy + xy + cy;
16
  }
17
  return maxiter - i;
18
}
19
 
20
function Mandelbrot( width,height, xmin,xmax, ymin,ymax, iterations ){
21
  var canvas = document.createElement( 'canvas' );
22
  canvas.width = width;
23
  canvas.height = height;
24
 
25
  var ctx = canvas.getContext( '2d' );
26
  var img = ctx.getImageData( 0, 0, width, height );
27
  var pix = img.data;
28
  for( var ix = 0; ix < width; ++ix )
29
    for( var iy = 0; iy < height; ++iy )
30
    {
31
      var x = xmin + (xmax - xmin) * ix / (width - 1);
32
      var y = ymin + (ymax - ymin) * iy / (height - 1);
33
      var i = Mandeliter( x, y, iterations );
34
      var ppos = 4 * (width * iy + ix);
35
      if( i === iterations )
36
      {
37
        pix[ppos] = 0;
38
        pix[ppos+1] = 0;
39
        pix[ppos+2] = 0;
40
      }
41
      else
42
      {
43
        var c = 3 * Math.log(i)/Math.log(iterations - 1.0);
44
        if (c < 1)
45
        {
46
          pix[ppos] = 255*c;
47
          pix[ppos+1] = 0;
48
          pix[ppos+2] = 0;
49
        }
50
        else if( c < 2 )
51
        {
52
          pix[ppos] = 255;
53
          pix[ppos+1] = 255*(c-1);
54
          pix[ppos+2] = 0;
55
        }
56
        else
57
        {
58
          pix[ppos] = 255;
59
          pix[ppos+1] = 255;
60
          pix[ppos+2] = 255*(c-2);
61
        }
62
      }
63
      pix[ ppos+3 ] = 255;
64
    }
65
  ctx.putImageData( img, 20,20 );
66
//  document.body.insertBefore( canvas, document.body.childNodes[0] );
67
  document.body.appendChild( canvas, document.body.childNodes[0] );
68
}
69
var mandel = confirm("Do you wish me to draw the Mandelbrot Set?");
70
var p;
71
if(mandel)
72
{
73
	p = prompt("How many iterations (100-5000)?",100);
74
	Mandelbrot( 450,300, -2,1, -1,1, p ); 
75
// print number of iterations
76
	document.write("Iterations: ", p);
77
}
78
else{
79
	document.write("Nothing to see - User declined!");
80
	alert("User Declined the option \n of rendering the Mandelbrot Set");	
81
}