FractalBernslyBoy

After thinking about it, I decided that it would be interesting to see how Arduboy processes fractals. Of all the fractals, I liked the classic fractal “Bernsley Fern” and decided to make a small demo…

p.s. UP/DOWN - change iterations; LEFT/RIGHT - changeScale; A/B - flipScreen

//FractalBernsly
//created @dima-78
//20230914

#include <Arduboy2.h>

Arduboy2 arduboy;

bool rotateScreen = false; 

int iterations = 1; 

float x, y; 

int iterationCount = 1;
int scaleFactorCount = 1;
int scaleFactor = 32;


void setup() {
  arduboy.begin();
  arduboy.clear();
  arduboy.setFrameRate(60);
  arduboy.initRandomSeed();
  x = arduboy.width() ;
  y = arduboy.height() ;
  arduboy.flipHorizontal(true);
  //arduboy.flipVertical(true);
  
  //Serial.begin(115200);
}



void loop() {
  if (!arduboy.nextFrame()) {
    return;
  }

  arduboy.pollButtons();


  uint8_t iterationsChange = 2;


  int minScaleFactor = 2;
  int maxScaleFactor = 32;

  
  if (arduboy.pressed(UP_BUTTON)) {
    iterations += iterationsChange;
    arduboy.clear();
  }
  if (arduboy.pressed(DOWN_BUTTON)) {
    if (iterations > 1) {
      iterations -= iterationsChange;
    }
    arduboy.clear();
  }

  if (arduboy.pressed(LEFT_BUTTON)) {
    if (scaleFactor > minScaleFactor) {
      scaleFactor--;
      arduboy.clear();
    }
  }
  if (arduboy.pressed(RIGHT_BUTTON)) {
    if (scaleFactor < maxScaleFactor) {
      scaleFactor++;
      arduboy.clear();
    }
  }

  if (arduboy.justPressed(A_BUTTON)) {
    rotateScreen = !rotateScreen; 
    arduboy.flipVertical(rotateScreen); 
  }
  
  if (arduboy.justPressed(B_BUTTON)) {
    rotateScreen = !rotateScreen; 
    arduboy.flipHorizontal(rotateScreen); 
    //arduboy.begin();
    //arduboy.clear();
  }

  //float x = 0;
  //float y = 0;

  for (int i = 0; i < iterations; i++) {
    arduboy.drawPixel(y * scaleFactor, x * scaleFactor, WHITE);


    int r = random(100);
    if (r < 1) {
      float x1 = 0;
      float y1 = 0.16 * y;
      x = x1;
      y = y1;
    }
    else if (r < 86) {
      float x1 = (0.85 * x) + (0.04 * y);
      float y1 = (-0.04 * x) + (0.85 * y) + 1.6;
      x = x1;
      y = y1;
    }
    else if (r < 93) {
      float x1 = (0.2 * x) - (0.26 * y);
      float y1 = (0.23 * x) + (0.22 * y) + 1.6;
      x = x1;
      y = y1;
    }
    else {
      float x1 = (-0.15 * x) + (0.28 * y);
      float y1 = (0.26 * x) + (0.24 * y) + 0.44;
      x = x1;
      y = y1;
    }

    iterationCount++;
    scaleFactorCount++;

    if (iterations == 1) {
      break; //
    }
  }

  //debug();

  arduboy.display();
}


void debug() {
  Serial.print(millis()); Serial.print("\t");
  Serial.print("iterationCount: "); Serial.print(iterationCount); Serial.print("\t");
  Serial.print("iterations: "); Serial.print(iterations); Serial.print("\t");
  Serial.print("scaleFactorCount: "); Serial.print(scaleFactor); Serial.print("\t");
  Serial.print("scaleFactor: "); Serial.print(scaleFactorCount); Serial.print("\t");
  Serial.print("\r\n");
}

FractalBernslyBoy.ino.hex (23.9 KB)

3 Likes

This is really neat! I love the way it updates the display at each step. I’d seen a fractal demo on the arduboy once before, but the image was more or less static. The animation here gives you a more intuitive feel of how the image is being generated, and it just looks nice. I could see this being set up to loop between scales automatically and it’d be a nice artpiece for any spare displays laying around, or if you just wanna keep your arduboy lookin fancy on a charging dock on a shelf or something. Point is, I love this! Nice job :slight_smile:

1 Like

I thought it was just super slow; I should’ve read the instructions lol. Looks really cool! I like this fractal

Thanks… Sorry for p.s. I tried to accurately make a description of the buttons

1 Like

I’ll think about it… This will be second version of the demo with an automatic scale in the loop.

1 Like