fractals in high resolution – the code

//  needs the class OutputBuffer and the improved Vector class

OutputBuffer outputBuffer, activeOutputBuffer;
int n,iteMax;
Vector c;
float rLimitSq;

void setup() {
  size(600, 600);
  noLoop();
  int magnification=10;
  outputBuffer=new OutputBuffer(magnification);
  outputBuffer.setUnitLength(230);
  outputBuffer.setOffset(-0.05,0);
  n=6;
  rLimitSq=5;
  c=new Vector(-1.4,0);
  iteMax=40;
  //  colorMode(HSB,100,100,100);
}

void draw() {
  int startTime=millis();             //  variables for progress report
  int lastTime=startTime;
  int time;
  float progress,restTime;
  int i,j,jWidth,ite;
  Vector z;
  color f;
  int r,g,b;
  outputBuffer.beginPixels();
  println(" start creating");
  for ( j=0;j<outputBuffer.thisHeight;j++){
    jWidth=outputBuffer.thisWidth*j;
    time=millis();
    if(time-lastTime>2000){                // progress report every 2 seconds
      progress=(j+0.1)/outputBuffer.thisHeight;
      restTime=(1.-progress)/progress*(time-startTime)/1000;
      lastTime=time;
      println(" progress: done: "+(100*progress)+" %, remaining time: "+restTime+" seconds");
    }
    for( i=0;i<outputBuffer.thisWidth;i++){
      f=fractal(i,j);
      r=(f >> 16) & 0xFF;
      g=(f >> 8) & 0xFF;
      b=f & 0xFF;
      f=fractal(i+0.5,j);
      r+=(f >> 16) & 0xFF;
      g+=(f >> 8) & 0xFF;
      b+=f & 0xFF;
      f=fractal(i,j+0.5);
      r+=(f >> 16) & 0xFF;
      g+=(f >> 8) & 0xFF;
      b+=f & 0xFF;
      f=fractal(i+0.5,j+0.5);
      r+=(f >> 16) & 0xFF;
      g+=(f >> 8) & 0xFF;
      b+=f & 0xFF;
      f=( 255<<24|(r & 0x3FC)<<14 | (g & 0x3FC)<<6 | b>>2);
      outputBuffer.thisImage.pixels[i+jWidth]=f;
     }    
   }    
  outputBuffer.endPixels();
  outputBuffer.show();
  outputBuffer.saveImage();
}

//--------------------------------------------------------------------------
color fractal(float i,float j){
  Vector z=outputBuffer.getVector(i,j);
  float rsqNow;
  for (int ite=0;ite<iteMax;ite++){
     z=z.copy().power(n).add(z.mult(c));
 //  z=z.copy().power(n).add(z.complexConjugate().mult(c));
//      z=z.copy().power(n).add(z.complexInverse().mult(c));
     rsqNow=z.rsq();
     if(rsqNow>rLimitSq){
       return twoColors(ite);
     }
  }
  return color(150,0,0);
}

color twoColors(int ite){   //  use colorMode RGB
  if(ite%2==0){
     return color(255,255,220);
  }
  else {
     return color(0,0,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