[SOLVED] How do you redraw over this pixel array?

Hello all, I’m trying to redraw over this 2D pixel array of this program.

// main setup settings
int dimy = 800;            // screen width 1681
int dimx = 850;            //screen height 1019
int bailout = 2000;         // number of iterations before bail
int plots = 171294;        // number of plots to execute per frame (x30 = plots per second) 171294
//zoom/magnification
float magx = 4.0; // default 3.0, smaller means more zoom
float magy = magx*(dimy/float(dimx));
//pan
float panx = 0.3; //default is 0.5
float pany = 0.0; //default is 0.0
//brightness multiplier
float brt = 1.0; // brightness factor

// 2D array to hold exposure values
// Creates the Size of Array {5*5 = 25 Total Array Points}
int[] exposure = new int[dimx*dimy];

// maximum exposure value
int maxexposure;           

int time = 0;
int exposures = 0;

 float Ex = 2;

 float x, y;
 float x0, y0;

boolean drawing; 

int sgn(float value) {            //if signus is needed sgn(x)=x/abs(x) = 1, 0 or -1
 if (value < 0) {return -1;}
 if (value > 0) {return 1;}
 return 0;
}
float cosh(float value1) {
 return 0.5*(exp(value1)+exp(-value1));
}
float sinh(float value2) {
 return 0.5*(exp(value2)-exp(-value2));
}
float arg(float axis1, float axis2) {
 if (axis2==0) {return PI;}
 else {return -atan(axis1/axis2)+PI*sgn(axis2)*0.5;}
}
float spharg(float axisa, float axisb, float axisc) {
 return acos(axisc/sqrt(sq(axisa)+sq(axisb)+sq(axisc)));
}
void setup() {
 // set up drawing area
 size(800,850,P3D);
}
void draw() {
 plotPlots();
 time++;
 if (time%1==0) { //every second
   findMaxExposure();
   // Infinitely Looping, nested boolean iteration(x0,y0,DrawIt)
   renderBrot();
 }
}
void plotPlots() {

 // iterate through some plots
 for (int n=0;n<plots;n++) {
   // Choose a random point in same range
   x = random(-2.0,2.0);
   y = random(-2.0,2.0);
   x0 = random(-2.0,2.0);
   y0 = random(-2.0,2.0);
   
   if (iterate(x,y,false)) {
    //Pauses Boolean, (d=pause, D=start)
     if (Ex > 1){
       iterate(x,y,true);
       exposures++;
      }
      if (Ex < 2){
       iterate(x,y,false);
      }
   }
   
 }
}
void renderBrot() {
 colorMode(RGB,1.0);
 // Algorithm that sets and draws pixel values, based on array size
 for (int i=0;i<dimx;i++) {
   for (int j=0;j<dimy;j++) {
     float ramp = brt * exposure[i*dimy+j] / maxexposure;
     // blow out ultra bright regions
     if (ramp > 1)  {
       ramp = 1;
     }
     //shading formulae
     color c = color(pow(sin(ramp),0.4),pow(ramp,0.75),pow(ramp,2.5)); // sunset
     set(j,i,c);
   }
 }
} 
//   Iterate the Mandelbrot and return TRUE if the point exits
//   Also handle the drawing of the exit points
boolean iterate(float x0, float y0, boolean drawIt) {
 float x = 0.0;
 float y = 0.0;
 //float t = random(0,1);
 //float r = random(0,1);  

 //x0 = 0.0;
 //y0 = 0.0;
 
 float xnew, ynew;
 int ix,iy;

 for (int i=0;i<bailout;i++) {
   
   // Display iterations before false DrawIt boolean
   //if (i>0){
   //println(i);
   // }
   
   //set your costum formula here...
   //example:
   //default Mandelbrot
   xnew = x * x - y * y + x0;
   ynew = 2 * x * y + y0;
   
// Draws pixel to screen -
// Based on if drawIt boolean is true, and iterations[i] > 3

  // Defines and Draws Pixels in 2D Exposure Array
  // Draws pixel when iteration[i] > 3 within current boolean cycle of xnew, ynew values
  if (drawIt && (i > 3)) {
    
     ix = int(dimx*(panx+xnew+magx/2.0)/magx);
     iy = int(dimy*(pany+ynew+magy/2.0)/magy);
     
     //Defines points (ix,iy) within screen ranges
     if (ix >= 0 && iy >= 0 && ix < dimx && iy < dimy) {
       // rotate and expose point
       exposure[ix*dimy+iy]++;
   }
 }
   //On keypress, reset 2D Pixel Array....
   
   if ((xnew*xnew + ynew*ynew) > 4) {
     // escapes
     return true;
   }
   x = xnew;
   y = ynew;
 }
 // does not escape
 return false;
}
void findMaxExposure() {
 // assume no exposure
 maxexposure=0;
 // find the largest density value
 for (int i=0;i<dimy;i++) {
   for (int j=0;j<dimx;j++) {
     maxexposure = max(maxexposure,exposure[i*dimx+j]);
   }
 }
} 
 void keyPressed(){

    //Return Iterate(x0,y0,DrawIt) False in Plot  
    if (key == 'r') {
     Ex = 1;
    }
    //Return Iterate(x0,y0,DrawIt) True in Plot  
    if (key == 'R') {
     Ex = 2;
    }
}

I’ve studied and tested many pixel array programs recently, but don’t understand it enough to figure it out the exact method. I know the pixel array exposure[] is a 2D array within the looping, nested boolean iterate(x0,y0,drawIt) and that the pixel array output is derived from the specific code:

// Draws pixel to screen -
// Based on if drawIt boolean is true, and iterations[i] > 3

  // Defines and Draws Pixels in 2D Exposure Array
  // Draws pixel when iteration[i] > 3 within current boolean cycle of xnew, ynew values
  if (drawIt && (i > 3)) {
    
     ix = int(dimx*(panx+xnew+magx/2.0)/magx);
     iy = int(dimy*(pany+ynew+magy/2.0)/magy);
     
     //Defines points (ix,iy) within screen ranges
     if (ix >= 0 && iy >= 0 && ix < dimx && iy < dimy) {
       // rotate and expose point
       exposure[ix*dimy+iy]++;
   }
 }

It’s not a boolean true/false issue as the entire program is written in an infinite loop of nested booleans drawing, iterate(x0,y0,drawIt) and when halted either in the drawing section or the plotPlots() section the pixel array remains intact upon continuation of the program (as demonstrated with Keypressed ‘r’, ‘R’). The program is well threaded into itself with it’s booleans and I have no reason to change that fact.

How would I go about updating the entire pixel array in this program to a single color (black) upon key press? Blanking out the canvas and the program continuing it’s constant pixel drawing process of exposure points on screen again without the old pixels.

1 Like

For those interested in clearing an array with nested Boolean loops, such as the case in this program, I thought I’d post my solution that I created. I created the void function deleteArray and tied it into a binary response in drawing, based on keypress. The solution to clearing the 2D pixel array was rather simple, the void deleteArray merely reinitializes the array []exposure just as seen in the main setup settings:
int[] exposure = new int[dimx*dimy];
And background(0); is added merely for the optics, either way once the drawing of the pixel array is started again on keypress ‘C’ the Boolean starts with a cleared background.

// main setup settings
int dimy = 800;            // screen width 1681
int dimx = 850;            //screen height 1019
int bailout = 2000;         // number of iterations before bail
int plots = 171294;        // number of plots to execute per frame (x30 = plots per second) 171294
//zoom/magnification
float magx = 4.0; // default 3.0, smaller means more zoom
float magy = magx*(dimy/float(dimx));
//pan
float panx = 0.3; //default is 0.5
float pany = 0.0; //default is 0.0
//brightness multiplier
float brt = 1.0; // brightness factor

// 2D array to hold exposure values
// Creates the Size of Array {Total Array Points -> example, 5*5 = 25}
int[] exposure = new int[dimx*dimy];

// maximum exposure value
int maxexposure;           
int max;

int time = 0;
int exposures = 0;

 float Ex = 2;
 float X1 = 1;
 float S1 = 1;
 
// float N=0;

 float x, y;
 float x0, y0;

boolean drawing; 

int sgn(float value) {            //if signus is needed sgn(x)=x/abs(x) = 1, 0 or -1
 if (value < 0) {return -1;}
 if (value > 0) {return 1;}
 return 0;
}
float cosh(float value1) {
 return 0.5*(exp(value1)+exp(-value1));
}
float sinh(float value2) {
 return 0.5*(exp(value2)-exp(-value2));
}
float arg(float axis1, float axis2) {
 if (axis2==0) {return PI;}
 else {return -atan(axis1/axis2)+PI*sgn(axis2)*0.5;}
}
float spharg(float axisa, float axisb, float axisc) {
 return acos(axisc/sqrt(sq(axisa)+sq(axisb)+sq(axisc)));
}

void setup() {
 // set up drawing area
 size(800,850,P3D);
}

void draw() {
  
  // Pauses boolean iterate(x,y,drawIt)
  if (S1<2){
    plotPlots();
   } 

 time++;
 if (time%1==0) { //every second
   
   if (X1>1){
    deleteArray();
   } else {
    findMaxExposure();
    // Infinitely Looping, nested boolean iteration(x0,y0,DrawIt)
    renderBrot();
   }
   
 }
}

void plotPlots() {

 // iterate through some plots, when plots reached then reset n
 for (int n=0;n<plots;n++) {
   
   // Choose a random point in same range
   x = random(-2.0,2.0);
   y = random(-2.0,2.0);
   x0 = random(-2.0,2.0);
   y0 = random(-2.0,2.0);
   
   if (iterate(x,y,false)) {
     
       iterate(x,y,true);
       exposures++;
   }
   
 }
}

void renderBrot() {
 colorMode(RGB,1.0);
 // Algorithm that sets and draws pixel values, based on array size
 for (int i=0;i<dimx;i++) {
   for (int j=0;j<dimy;j++) {
     float ramp = brt * exposure[i*dimy+j] / maxexposure;
     // blow out ultra bright regions
     if (ramp > 1)  {
       ramp = 1;
     }
     //shading formulae
     color c = color(pow(sin(ramp),0.4),pow(ramp,0.75),pow(ramp,2.5)); // sunset
     set(j,i,c);
   }
 }
} 

  // Iterate the Mandelbrot and return TRUE if the point exits
   // Also handle the drawing of the exit points
boolean iterate(float x0, float y0, boolean drawIt) {
 float x = 0.0;
 float y = 0.0;
 //float t = random(0,1);
 //float r = random(0,1);  

 //x0 = 0.0;
 //y0 = 0.0;
 
 float xnew, ynew;
 int ix,iy;

 for (int i=0;i<bailout;i++) {
   
   // Display iterations before false DrawIt boolean
   //if (i>0){
   //println(i);
   // }
   
   //set your costum formula here...
   //example:
   //default Mandelbrot
   xnew = x * x - y * y + x0;
   ynew = 2 * x * y + y0;
   
// Draws pixel to screen -
// Based on if drawIt boolean is true, and iterations[i] > 3

  // Defines and Draws Pixels in 2D Exposure Array
  // Draws pixel when iteration[i] > 3 within current boolean cycle of xnew, ynew values
  if (drawIt && (i > 3)) {
    
     ix = int(dimx*(panx+xnew+magx/2.0)/magx);
     iy = int(dimy*(pany+ynew+magy/2.0)/magy);
     
     //Defines points (ix,iy) within screen ranges
     if (ix >= 0 && iy >= 0 && ix < dimx && iy < dimy) {
       // rotate and expose point
       exposure[ix*dimy+iy]++;
   }
 }
   if ((xnew*xnew + ynew*ynew) > 4) {
     // escapes
     return true;
   }
   x = xnew;
   y = ynew;
 //N = (xnew*xnew + ynew*ynew);
 }
 // does not escape
 return false;
}

void findMaxExposure() {
 // assume no exposure
 maxexposure=0;
 // find the largest density value
 for (int i=0;i<dimy;i++) {
   for (int j=0;j<dimx;j++) {
     maxexposure = max(maxexposure,exposure[i*dimx+j]);
   }
 }
} 

void deleteArray(){
  
  //Clears pixels array drawing, then clears the screen
    exposure = new int[dimx*dimy];
    background(0);
    
  //Boolean values continue to interate, despite cleared pixel array drawing
    //println(N);
  // Update: 's' stops plot iterations, 'S' continues plot iterations
  
}

 void keyPressed(){

   //Deletes Drawn Pixel Array
    if (key == 'c') {
     X1 = 2;
    }
    //Starts Drawing Pixel Array
    if (key == 'C') {
     X1 = 1;
    }
    
    //Stops iterating boolean plots
    if (key == 's') {
     S1 = 2;
    }
    //Starts iterating boolean plots
    if (key == 'S') {
     S1 = 1;
    }
}
2 Likes