improved code for fractals in high resolution

I was not satisfied with the earlier code for generating high-resolution fractals and I improved on it to make experimentation more rapid. Now the program first generates only a low-resolution image for the computer screen.Then the code stops the “draw()” method because of the “noLoop()” command. Thus the program does not occupy the computer while you are looking at the screen. This saves energy. If you hit the ‘s’-key a keyPressed-event lets the computer generate and save the high-resolution image at magnification>>1.

And that’s it:

OutputBuffer screenBuffer,outputBuffer,activeOutputBuffer;
int n,iteMax;
Vector c;
float rLimitSq;
float unitLength,offsetX,offsetY;
int magnification;

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

void draw() {
  makeFractal(screenBuffer);
  screenBuffer.show();
}

void keyPressed(){
    if(key=='s') {
      if(outputBuffer==null) {                    // create only onces
        outputBuffer=new OutputBuffer(magnification);
        outputBuffer.setUnitLength(unitLength);
        outputBuffer.setOffset(offsetX,offsetY);
        makeFractal(outputBuffer);
      }
      outputBuffer.saveImage();
    }
}

//--------------------------------------------------------------------------

void makeFractal(OutputBuffer buffer){
  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;
  buffer.beginPixels();
  println(" start creating");
  for ( j=0;j<buffer.thisHeight;j++){
    jWidth=buffer.thisWidth*j;
    time=millis();
    if(time-lastTime>2000){                // progress report every 2 seconds
      progress=(j+0.1)/buffer.thisHeight;
      restTime=(1.-progress)/progress*(time-startTime)/1000;
      lastTime=time;
      println(" progress: done: "+(100*progress)+" %, remaining time: "+restTime+" seconds");
    }
    for( i=0;i<buffer.thisWidth;i++){
      f=fractal(buffer.getVector(i,j));
      r=(f >> 16) & 0xFF;
      g=(f >> 8) & 0xFF;
      b=f & 0xFF;
      f=fractal(buffer.getVector(i+0.5,j));
      r+=(f >> 16) & 0xFF;
      g+=(f >> 8) & 0xFF;
      b+=f & 0xFF;
      f=fractal(buffer.getVector(i,j+0.5));
      r+=(f >> 16) & 0xFF;
      g+=(f >> 8) & 0xFF;
      b+=f & 0xFF;
      f=fractal(buffer.getVector(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);
      buffer.thisImage.pixels[i+jWidth]=f;
     }    
   }    
  buffer.endPixels();
  println(" end creating");
}


color fractal(Vector z){
  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