Recursive Sierpinski triangle (just a bit of fun)

Ahh I was just searching for Sierpinski triangle (simple).
So, while there I just posted this as well. Very simple.

void setup() {
  size(410, 230);
  background(255);
  fill(0);
  triangle (10, 25, 100, 5);
}
 
void triangle (int x, int y, int l, int n) {
    if( n == 0) text("*", x, y);
    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);
    }
}

sp

4 Likes