Recursive Sierpinski triangle (just a bit of fun)

I un-simplified it a bit, made it gradual. It’s rather “wasteful” though, going through all the recursions all the time even if not plotting.

Space key resets (as in jeremydouglass’ example).

int depth = 5;
int interval = 10;

int currentTime;
int lastTime;
int progress = 0;
int lastProgress = 0;
//int finished = int(pow(3,depth));
boolean intervalExpired = false;


void setup() {
  size(410, 230);
  background(255);
  fill(0);
  lastTime = millis();
}


void draw() {
  currentTime = millis();
  triangle (10, 25, 100, depth);
}

 
void triangle (int x, int y, int l, int n) {
  if( n == 0) {
    checkIfIntervalExpired();
    if (intervalExpired) {
      if (progress == lastProgress) {
        text("*", x, y);
        lastProgress++;
        intervalExpired = false;
      }
    }
    progress++;
  } else {
      triangle(x, y+l, l/2, n-1);
      triangle(x+l, y, l/2, n-1);
      triangle(x+l*2, y+l, l/2, n-1);
  }
}


void checkIfIntervalExpired() {
  if (currentTime-lastTime > interval) {
    lastTime = currentTime;
    progress = 0;
    intervalExpired = true;
  }
}


void keyReleased() {  
  if (key==' ') {  // reset
    progress = 0;
    lastProgress = 0;
    background(255);
  }
}

2 Likes