• fullscreen
  • HyperComplex.pde
  • Mandelbrot.pde
  • Mandelbrot_Tracer.pde
  • class HyperComplex { 
      float r; //Real portion of a hypercomplex number 
      float i; //Imaginary portion of a hypercomplex number 
      float j; //J-Imaginary portion of a hypercomplex number 
      float k; //K-Imaginary portion of a hypercomplex number 
       
      HyperComplex(float R, float I, float J, float K) { //Declare the class "Complex" 
        r = R; 
        i = I; 
        j = J; 
        k = K; 
      } 
     
      void Set(float R, float I, float J, float K) { //Declare the class "Complex" 
        r = R; 
        i = I; 
        j = J; 
        k = K; 
      } 
       
      HyperComplex Zadd(HyperComplex Z, HyperComplex Y) { //Add a complex and a complex 
        return new HyperComplex( Z.r + Y.r, Z.i + Y.i, Z.j + Y.j, Z.k + Y.k ); 
      } 
     
      HyperComplex Zsub(HyperComplex Z, HyperComplex Y) { //Add a complex and a complex 
        return new HyperComplex( Z.r - Y.r, Z.i - Y.i, Z.j - Y.j, Z.k - Y.k ); 
      } 
     
      HyperComplex Zmul(HyperComplex Z, HyperComplex Y) { //Multiply a complex and a complex 
        return new HyperComplex(  
        Z.r * Y.r - Z.i * Y.i - Z.j * Y.j + Z.k * Y.k,  
        Z.i * Y.r + Z.r * Y.i - Z.k * Y.j - Z.j * Y.k,  
        Z.j * Y.r + Z.r * Y.j - Z.k * Y.i - Z.k * Y.k,  
        Z.k * Y.r + Z.r * Y.k + Z.j * Y.i + Z.i * Y.j 
        ); 
      } 
       
      float Zabs() { //Returns the absolute value of the class 
        return sqrt( r * r + i * i + j * j + k * k ); 
      } 
       
      float Zangle() { //Identifies the quadrant to return the angle 
                       //of a complex number 
        float theta; 
        if(r == 0) { 
          if(i > 0) { 
            theta = HALF_PI; 
          } 
          else if(i < 0) { 
            theta = 3*HALF_PI; 
          } 
          else { 
            theta = 0; 
          } 
        } 
        else { 
          theta = atan( i / r ); 
          if(( r < 0 ) && ( i >= 0 )) { theta += PI; } 
          if(( r < 0 ) && ( i < 0 )) { theta -= PI; } 
        } 
        return theta; 
      } 
       
      void display() { //Displays the class' value 
        println(r + " + " + i + "i = " +  
                Zabs() + " * e^(" + Zangle() + "i)"); 
      } 
       
    } 
    
    
    void Mandelbrot(float J, float K) { 
       
      for(int ix=0; ix<width; ix++) { 
        for(int iy=0; iy<height; iy++) { 
          stroke( iterate(  ix * (xMax-xMin) / width + xMin, 
                  ( height - iy ) * (yMax-yMin) / height + yMin, 
                  J, K ) * 255 ); 
          point(ix,iy); 
        } 
      } 
       
    } 
     
    void trace( float x, float y, float j, float k ) { 
      HyperComplex Z = new HyperComplex(x,y,j,k); 
      HyperComplex C = new HyperComplex(x,y,j,k); 
      float ix = dim * (x-xMin) / (xMax-xMin); 
      float iy = height - dim * (y-yMin) / (yMax-yMin); 
      float px; 
      float py; 
       
      for(int i=0; i<resolution; i++) { 
        px = ix; 
        py = iy; 
        Z = Z.Zadd( Z.Zmul(Z,Z), C ); 
        ix = dim * (Z.r-xMin) / (xMax-xMin); 
        iy = height - dim * (Z.i-yMin) / (yMax-yMin); 
        line( ix, iy, px, py ); 
      } 
       
    } 
     
    float iterate( float x, float y, float j, float k ) { 
     
      HyperComplex Z = new HyperComplex(x,y,j,k); 
      HyperComplex C = new HyperComplex(x,y,j,k); 
      int r = 0; 
     
      do { 
        r++; 
        Z = Z.Zadd( Z.Zmul(Z,Z), C ); 
         
      } while ((r < resolution) && (Z.Zabs() < 2)); 
     
      return float(r)/resolution; 
       
    } 
    
    
    float xMin = -2; 
    float xMax = 1; 
    float yMin = -1.5; 
    float yMax = 1.5; 
    float J = 0; 
    float K = 0; 
    int resolution = 500; 
    int dim = 640; 
     
    PImage mandelbrot, buddhabrot; 
    PImage back; 
    PFont metaBold; 
    color escape = color( 255, 15, 15, 150 ); 
    color contained = color( 15, 120, 255, 150 ); 
    boolean buddha = false, locked = false; 
     
    float cx, cy; 
    float sx = 1/sin(0), sy = 1/sin(0); 
      
    void setup(){ 
      size( dim, dim ); 
      background(0); 
      smooth(); 
      colorMode(RGB); 
      metaBold = loadFont("ArialMT-20.vlw"); 
      mandelbrot = loadImage("Mandelbrot.png"); 
      buddhabrot = loadImage("Buddhabrot.png"); 
      back = mandelbrot; 
      textFont(metaBold, 16); 
      image( mandelbrot, 0, 0 ); 
       
    } 
     
    void draw() { 
      cx = mouseX * (xMax-xMin) / width + xMin; 
      cy = ( height-mouseY ) * (yMax-yMin) / height + yMin; 
     
      if( !locked ) { 
     
        sx = cx;
        sy = cy;
    
        renderImage(); 
      }   
     
      image( back, 0, 0 ); 
      fill(255); 
      text("Current Coordinate: "+cx+" + "+cy+"i", 5, 20); 
      text("Seed Coordinate: "+sx+" + "+sy+"i", 5, 40); 
    } 
     
    void renderImage() { 
     
      if( buddha ) 
        image( buddhabrot, 0, 0 ); 
      else 
        image( mandelbrot, 0, 0 ); 
       
      if( iterate( sx, sy, J, K ) == 1 ) 
        stroke(contained); 
      else 
        stroke(escape); 
     
      trace( sx, sy, J, K ); 
      back = get( 0, 0, width, height ); 
       
    } 
    
    void mouseReleased() { 
      if( mouseButton == LEFT ) { 
        locked = !locked;
      }
      else {
        buddha = !buddha; 
      }
      
      renderImage(); 
    } 
     
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement


    Claudio Gonzales

    Mandelbrot Tracer

    Add to Faves Me Likey@! 3
    You must login/register to add this sketch to your favorites.

    Allows the user to pick seed points (left click), tracing their orbit through the "Mandelbrot Mill". By right clicking, the background switches from a Mandelbrot fractal to a Buddhabrot fractal, which is created by the amount of times all points in the region spiral in or out of the mill.

    You need to login/register to comment.