self-similar fractals from function iteration – the code

//  this reproduces the image of " self-similar fractals with ..."
//  you can make your own experiments, try the codes hidden as comments
//  this is for the main tab of processing
// it needs the code of "from pixels to coordinates-with code"
//   and "complex numbers - the code"
Coordinates screen;
float rMaxSq;
Complex c;
int iteMax;
int n;

void setup(){
  size(600,600);
  screen=new Coordinates(1.17);
  screen.setTrans(-0.1,0.);
  rMaxSq=1.8;
  c=new Complex(-1.3,0.01);
  n=6;
  iteMax=40;
//  colorMode(HSB,100,100,100);
}

void draw(){
  noLoop();
  int i,j,jWidth,ite;
  float iteCon,rsqNow,rsqLast;
  Complex z;
  color pix;
  loadPixels();
  for (j=0;j<height;j++){
    jWidth=width*j;
    for (i=0;i<width;i++){
      pix=color(150,0,0);
      z=screen.pixel(i,j);
      rsqLast=z.rsq();
      for (ite=0;ite<iteMax;ite++){
        z=z.pwr(n).add(z.mult(c));
//        z=z.pwr(n).add(z.cc().mult(c));
 //       z=z.pwr(n).add(z.invers().cc().mult(c));
        rsqNow=z.rsq();
          if(rsqNow>rMaxSq){
            iteCon=ite-(rsqNow-rMaxSq)/(rsqNow-rsqLast);
            pix=twoColors(ite);
 //           pix=rainbow(iteCon);
          break;
        }
        rsqLast=rsqNow;
      }
      pixels[i+jWidth]=pix;
    }
  }
  updatePixels();
//  saveImage();
}

color twoColors(int ite){   //  use colorMode RGB
             if(ite%2==0){
              return color(255,255,220);
            }
            else {
              return color(0,0,100);
            }
}

color rainbow(float iteCon){   //  use colorMode HSB     !!!!
  iteCon*=0.25;
  return color(100*(iteCon-floor(iteCon)),100,100);
}
This entry was posted in Fractals, programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s