Guest User

Untitled

a guest
Jan 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.             $(document).ready(
  3.                 function() {
  4.                     //Complex number object for use with the fractal rendering.
  5.                     function Complex(real, imag) {
  6.                         this.r = real;
  7.                         this.i = imag;
  8.                     }
  9.                     Complex.prototype.square = function() {
  10.                         return(new Complex(this.r * this.r,this.i * -this.i));
  11.                     }
  12.                     Complex.prototype.add = function(c) {
  13.                         return(new Complex(this.r + c.r, this.i + c.i));
  14.                     }
  15.                     Complex.prototype.abs = function() {
  16.                         return(Math.sqrt(this.c*this.c+this.i*this.i))
  17.                     }
  18.                    
  19.                     //gather canvas information
  20.                     var canvas = document.getElementById("myCanvas");
  21.                     var ctx = canvas.getContext("2d");
  22.                     var maxItr = 4;     //maximum iterations to go through for rendering.
  23.                     var maxCol = 256;   //maximum allowed colors.
  24.                     var Data = ctx.createImageData(canvas.width, canvas.height);
  25.                    
  26.                     for (var x = 0; x < Data.width; x++)  { //pixel loop
  27.                         for (var y = 0; y < Data.height; y++)  {
  28.                             //CALCULATING MANDELBROT SET
  29.                             var mx = (x/(Data.width/4))-2;  //get the relevent location in the set.
  30.                             var my = (y/(Data.width/4))-2;
  31.                             var z = new Complex(0,0);
  32.                             var c = new Complex(mx,my);
  33.                             var itr = 0;
  34.                             while((z.abs() < 2) && (itr < maxItr)) {
  35.                                 z = z.square.add(c);
  36.                                 itr++;
  37.                             }
  38.                             var col = maxCol % itr;
  39.                            
  40.                             //SETTING IMAGE DATA
  41.                             var idx = (x + y * Data.width) * 4;     // Index of the pixel in the array
  42.                             Data.data[idx] = col;                   // Red channel
  43.                             Data.data[idx + 1] = col;               // Green channel
  44.                             Data.data[idx + 2] = col;               // Blue channel
  45.                             Data.data[idx + 3] = col;               // Alpha channel
  46.                         }
  47.                     }
  48.                    
  49.                     var Data = ctx.putImageData(Data, 0, 0);
  50.                 }
  51.             );
  52.         </script>
Add Comment
Please, Sign In to add comment