How to pace the appearance of recursion objects?

Maybe like this ?
Tell me if it is too complicated :wink:

PShape s; // The PShape object - Cube
int a = 500; // Declare variable - Radious Cube
float b = a * sqrt(3) / 2; // Height of triangle
float c = a * 2 / 3; // Circle02 Radious - 7Days of Creation

float f = c * cos(radians(30));
float g = c * cos(radians(60));

int nb = 0;
int maxVal = 5000;
ArrayList<Circle> array;
int index = 0;
int time = 0;
int timeValue = 1;

void setup() {
  fullScreen();
  background(255);
  stroke(0);
  strokeWeight(1);
  //frameRate(50);

  s = createShape(); //Cube Outline
  s.beginShape();
  s.noFill();
  s.stroke(0);
  s.vertex(width/2, height/2-a);
  s.vertex(width/2+b, height/2-a/2);
  s.vertex(width/2+b, height/2+a/2);
  s.vertex(width/2, height/2+a);
  s.vertex(width/2-b, height/2+a/2);
  s.vertex(width/2-b, height/2-a/2);
  s.endShape(CLOSE);
  
  array = new ArrayList<Circle>();
}

void draw() {
  
  if (nb<maxVal){
    // ----------------Draw Cube--------------
    shape(s); //Cube Outline
  
    //------------------Centre Base Circles --------------
    drawCircle(width/2, height/2, c);
    drawCircle(width/2, height/2-c, c);
    drawCircle(width/2, height/2+c, c);
    drawCircle(width/2+f, height/2-g, c);
    drawCircle(width/2+f, height/2+g, c);
    drawCircle(width/2-f, height/2-g, c);
    drawCircle(width/2-f, height/2+g, c);
  }else{
    
    if (millis()-time > timeValue && index<array.size()){
      array.get(index).display();
      time = millis();
      index++;
    }
  }
}

void drawCircle(float x, float y, float radius) {
  if (nb < maxVal){
    nb++;
    
    array.add(new Circle(x,y,radius));
    
    if (radius > 20) {
      drawCircle(x, y, radius/4);
      drawCircle(x, y + radius/4, radius/4);
      drawCircle(x, y - radius/4, radius/4);
      drawCircle(x + radius/4 * cos(radians(30)), y - radius/4 * cos(radians(60)), radius/4);
      drawCircle(x + radius/4 * cos(radians(30)), y + radius/4 * cos(radians(60)), radius/4);
      drawCircle(x - radius/4 * cos(radians(30)), y - radius/4 * cos(radians(60)), radius/4);
      drawCircle(x - radius/4 * cos(radians(30)), y + radius/4 * cos(radians(60)), radius/4);
      drawCircle(x, y + radius/2, radius/4);
      drawCircle(x, y - radius/2, radius/4);
      drawCircle(x + radius/2 * cos(radians(30)), y - radius/2 * cos(radians(60)), radius/4);
      drawCircle(x + radius/2 * cos(radians(30)), y + radius/2 * cos(radians(60)), radius/4);
      drawCircle(x - radius/2 * cos(radians(30)), y - radius/2 * cos(radians(60)), radius/4);
      drawCircle(x - radius/2 * cos(radians(30)), y + radius/2 * cos(radians(60)), radius/4);
    }
  }else{
    return;
  }
}


class Circle{
  float x,y,radius;
  
  Circle(float xx,float yy,float rad){
    x = xx;
    y = yy;
    radius = rad;
  }
  
  void display(){
    stroke(0);
    noFill();
    ellipse(x, y, radius, radius);
  }
}

Explanation :

  • Create a Circle class which stores coordinates and radius of an ellipse and add a display function to draw it
  • Initialize an ArrayList which will store all you Circles
  • Do the recursion and each time you call the function DrawCircle, add a Circle in your ArrayList array
  • When you finished to store all your ellipses (for exemple 5000) start to draw them one by one in the draw function with a time interval (timeVal)
1 Like