Fractal Design

edited December 2016 in Share Your Work

I accidentally created a surprisingly complex fractal with only 26 lines of code. A picture of the fractal and the code are listed below.

color RED = color(255, 0, 0);
color BLUE = color(0, 0, 255);
void setup() {
  size(1000, 1000);
  noStroke();
  background(0);
}
void draw() {
  drawCircle(width/2, height/2, 200, BLUE);
}
void drawCircle(float x, float y, float radius, color c) {
  fill(c);
  ellipse(x, y, radius, radius);
  if (radius > 1) {
    pushMatrix();
    translate(x - radius/2, y - radius/2);
    rotate(PI/2); //rotate each branch 90 degrees
    drawCircle(0, 0, radius/1.4, c == RED? BLUE : RED); //the color of each branch alternates from red to blue
    popMatrix();
    pushMatrix();
    translate(x - radius/2, y + radius/2);
    rotate(-PI/2);
    drawCircle(0, 0, radius/1.4, c == RED? BLUE : RED);
    popMatrix();
  }
}

Comments

  • Very beautiful!

    I added a simple animation cycle based on frameCount to better understand how it works....

    color RED = color(255, 0, 0);
    color BLUE = color(0, 0, 255);
    void setup() {
      size(1000, 1000);
      noStroke();
      frameRate(1);
    }
    void draw() {
      background(0);
      drawCircle(width/2, height/2, 200, pow(1.4,17-frameCount%16), BLUE);
    }
    void drawCircle(float x, float y, float radius, float maxradius, color c) {
      fill(c);
      ellipse(x, y, radius, radius);
      if (radius > maxradius) {
        pushMatrix();
        translate(x - radius/2, y - radius/2);
        rotate(PI/2); //rotate each branch 90 degrees
        drawCircle(0, 0, radius/1.4, maxradius, c == RED? BLUE : RED); //the color of each branch alternates from red to blue
        popMatrix();
        pushMatrix();
        translate(x - radius/2, y + radius/2);
        rotate(-PI/2);
        drawCircle(0, 0, radius/1.4, maxradius, c == RED? BLUE : RED);
        popMatrix();
      }
    }
    
  • Very nice work. Thank you both.

Sign In or Register to comment.