Produce a graphical or ASCII-art representation of a Sierpinski pentagon (aka a Pentaflake) of order 5. Your code should also be able to correctly generate representations of lower orders: 1 to 4.





See also
 Sierpinski pentagon


 
float s_angle, scale, margin = 25, total = 4;
float p_size = 700;
float radius = p_size/2-2*margin;
float side = radius * sin(PI/5)*2;
 
void setup() {
  float temp = width/2;
  size(590, 590);
  background(0, 0, 200);
  stroke(255);
  s_angle = 72*PI/180;
  scale = 1/(2+cos(s_angle)*2);
  for (int i = 0; i < total; i++) {
    background(0, 0, 200);
    drawPentagon(width/2, (height-p_size)/2 + 3*margin, side, total);
  }
}
 
void drawPentagon(float x, float y, float side, float depth) {
  float angle = 3*s_angle; 
  if (depth == 0) {  
    for (int i = 0; i < 5; i++) {
      float px = x;
      float py = y;
      x = x+cos(angle)*side;
      y = y-sin(angle)*side;
      line(x, y, px, py);
      angle += s_angle;
    }
  } else {
    side *= scale;
    float distance = side+side*cos(s_angle)*2;
    for (int j = 0; j < 5; j++) {
      x = x+cos(angle)*distance;
      y = y-sin(angle)*distance;
      drawPentagon(x, y, side, depth-1);
      angle += s_angle;
    }
  }
}