topical media & game development

talk show tell print

#javascript-processing-example-topic-fractals-mandelbrot.htm / htm



  <!DOCTYPE html>
  <html><head>
  <script src="javascript-processing-example-processing.js"></script>
  <script src="javascript-processing-example-init.js"></script>
  <link rel="stylesheet" href="javascript-processing-example-style.css">
  </head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
  <h2>Mandelbrot</h2>
  
  <p>by Daniel Shiffman.  
  
  Simple rendering of the Mandelbrot set.</p>
  
  <p><a href="http://processing.org/learning/topics/mandelbrot.html"><b>Original Processing.org Example:</b> Mandelbrot</a><br>
  <script type="application/processing">
  // Establish a range of values on the complex plane
  // A different range will allow us to "zoom" in or out on the fractal
  // float xmin = -1.5; float ymin = -.1; float wh = 0.15;
  float xmin = -2.5; 
  float ymin = -2; 
  float wh = 4;
  
  void setup() {
    size(200, 200);
    noLoop();
  }
  
  void draw() {
  
    loadPixels();
    
    // Maximum number of iterations for each point on the complex plane
    int maxiterations = 200;
  
    // x goes from xmin to xmax
    float xmax = xmin + wh;
    // y goes from ymin to ymax
    float ymax = ymin + wh;
    
    // Calculate amount we increment x,y for each pixel
    float dx = (xmax - xmin) / (width);
    float dy = (ymax - ymin) / (height);
  
    // Start y
    float y = ymin;
    for(int j = 0; j < height; j++) {
      // Start x
      float x = xmin;
      for(int i = 0;  i < width; i++) {
        
        // Now we test, as we iterate z = z^2 + cm does z tend towards infinity?
        float a = x;
        float b = y;
        int n = 0;
        while (n < maxiterations) {
          float aa = a * a;
          float bb = b * b;
          float twoab = 2.0 * a * b;
          a = aa - bb + x;
          b = twoab + y;
          // Infinty in our finite world is simple, let's just consider it 16
          if(aa + bb > 16.0f) {
            break;  // Bail
          }
          n++;
        }
        
        // We color each pixel based on how long it takes to get to infinity
        // If we never got there, let's pick the color black
        if (n == maxiterations) pixels[i+j*width] = 0;
        else pixels[i+j*width] = color(n*16 % 255);  // Gosh, we could make fancy colors here if we wanted
        x += dx;
      }
      y += dy;
    }
    updatePixels();
  }
  </script><canvas width="200" height="200"></canvas></p>
  <div style="overflow: hidden; height: 0px; width: 0px;"></div>
  
  <pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
  // unless otherwise stated.</b>
  // Establish a range of values on the complex plane
  // A different range will allow us to "zoom" in or out on the fractal
  // float xmin = -1.5; float ymin = -.1; float wh = 0.15;
  float xmin = -2.5; 
  float ymin = -2; 
  float wh = 4;
  
  void setup() {
    size(200, 200);
    noLoop();
  }
  
  void draw() {
  
    loadPixels();
    
    // Maximum number of iterations for each point on the complex plane
    int maxiterations = 200;
  
    // x goes from xmin to xmax
    float xmax = xmin + wh;
    // y goes from ymin to ymax
    float ymax = ymin + wh;
    
    // Calculate amount we increment x,y for each pixel
    float dx = (xmax - xmin) / (width);
    float dy = (ymax - ymin) / (height);
  
    // Start y
    float y = ymin;
    for(int j = 0; j &lt; height; j++) {
      // Start x
      float x = xmin;
      for(int i = 0;  i &lt; width; i++) {
        
        // Now we test, as we iterate z = z^2 + cm does z tend towards infinity?
        float a = x;
        float b = y;
        int n = 0;
        while (n &lt; maxiterations) {
          float aa = a * a;
          float bb = b * b;
          float twoab = 2.0 * a * b;
          a = aa - bb + x;
          b = twoab + y;
          // Infinty in our finite world is simple, let's just consider it 16
          if(aa + bb &gt; 16.0f) {
            break;  // Bail
          }
          n++;
        }
        
        // We color each pixel based on how long it takes to get to infinity
        // If we never got there, let's pick the color black
        if (n == maxiterations) pixels[i+j*width] = 0;
        else pixels[i+j*width] = color(n*16 % 255);  // Gosh, we could make fancy colors here if we wanted
        x += dx;
      }
      y += dy;
    }
    updatePixels();
  }</pre>
  </body></html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.