Advertisement
Deerenaros

Unoptimized js code that draws mandelbrot set

Dec 15th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.18 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.   <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  6.   <title>Useful Complex Operations</title>
  7.   <style>
  8.     body {
  9.       text-align: center;
  10.     }
  11.   </style>
  12. </head>
  13.  
  14. <body onload="mandelbrot()">
  15.     <canvas id=canvas width=1000 height=700>
  16.         Maybe it's time to <a href="http://en.wikipedia.org/wiki/Canvas_element#Support">change</a> browser?
  17.     </canvas>
  18.   <script>
  19.     function Complex(x, y) {
  20.       this.x = x || 0;
  21.       this.y = y || 0;
  22.     } // Complex
  23.  
  24.     Complex.prototype.toString = function() {
  25.       return this.y >= 0 ? this.x + " + " + this.y + "i" : this.x + " - " + (-this.y) + "i";
  26.     } // toString
  27.  
  28.     Complex.prototype.modulus = function() {
  29.       return Math.sqrt(this.x*this.x + this.y*this.y);
  30.     } // modulus
  31.  
  32.     Complex.prototype.add = function(z) {
  33.       return new Complex(this.x + z.x, this.y + z.y);
  34.     } // sum
  35.  
  36.     Complex.prototype.square = function() {
  37.       return new Complex(this.x*this.x - this.y*this.y, 2*this.x*this.y);
  38.     } // square
  39.  
  40.     Complex.prototype.sqmod = function(){
  41.         return this.x*this.x + this.y*this.y;
  42.     } // square pow of modulus
  43.  
  44.     function limitCheck(c, maxIter){
  45.         var z = new Complex(0, 0);
  46.         var i = 1;
  47.         for(; i <= maxIter; i++){
  48.             z = c.add(z.square());
  49.             if (z.modulus() > 2)
  50.                 return false; //i here is how fast z grow to fast... it's need to colorize
  51.         }
  52.  
  53.         return true;
  54.     }
  55.  
  56.     var scale = 0.005;
  57.     var step = 0.0007;
  58.  
  59.     var Plot = function(canvas){
  60.         this.ctx = canvas.getContext('2d');
  61.         this.ctx.translate(canvas.width/2, canvas.height/2);
  62.         this.ctx.scale(1/scale, -1/scale);
  63.         this.ctx.fillStyle = "red";
  64.     }
  65.  
  66.     Plot.prototype.drawRect = function(x, y, w, h, c){
  67.         this.ctx.fillStyle = "rgb(" + c + "," + c + "," + c + ")";
  68.         this.ctx.fillRect(x, y, w, h);
  69.     }
  70.  
  71.     function mandelbrot(){
  72.         var MAX_X = 2, MAX_Y = 2;
  73.         var plot = new Plot(document.getElementById("canvas"));
  74.         for(var i = -MAX_X; i < MAX_X; i += step){
  75.             for(var j = -MAX_Y; j < MAX_Y; j += step){
  76.                 var c = new Complex(i, j);
  77.                 var lc = limitCheck(c, 32);
  78.                 if(lc == true)
  79.                     plot.drawRect(i, j, step, step, 0);
  80.             }
  81.         }
  82.     }
  83.  </script>
  84. </body>
  85.  
  86. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement