# FractalSerpinskyTriangleBoy

I decided to continue the fractal theme and filed a demo - the Serpinsky Triangle…
fractalSerpinskiyTriangle.ino.hex (21.7 KB)

#include <Arduboy2.h>

Arduboy2 arduboy;

int scaleFactor = 50;
int depthFactor = 5;

void drawSierpinsky(int x1, int y1, int x2, int y2, int x3, int y3, int depth) {
if (depth == 0) {
arduboy.fillTriangle(x1, y1, x2, y2, x3, y3);
return;
}

int midX1 = (x1 + x2) / 2;
int midY1 = (y1 + y2) / 2;
int midX2 = (x2 + x3) / 2;
int midY2 = (y2 + y3) / 2;
int midX3 = (x3 + x1) / 2;
int midY3 = (y3 + y1) / 2;

drawSierpinsky(x1, y1, midX1, midY1, midX3, midY3, depth - 1);
drawSierpinsky(midX1, midY1, x2, y2, midX2, midY2, depth - 1);
drawSierpinsky(midX3, midY3, midX2, midY2, x3, y3, depth - 1);
}

void setup() {
arduboy.begin();
arduboy.clear();
arduboy.setFrameRate(60);
}

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

arduboy.pollButtons();

if (arduboy.pressed(UP_BUTTON)) {
scaleFactor ++;
arduboy.clear();
}
if (arduboy.pressed(DOWN_BUTTON)) {
scaleFactor --;
arduboy.clear();
}

int scale = map(scaleFactor, -100, 100, 1, 10);
//int scale = map(0, -100, 100, 1, 10);

int x1 = arduboy.width() / 2;
int y1 = 10;
int x2 = arduboy.width() / 2 + (96 * scale) / 100;
int y2 = arduboy.height() - (77 * scale) / 100;
int x3 = arduboy.width() / 2 - (96 * scale) / 100;
int y3 = arduboy.height() - (77 * scale) / 100;

if (arduboy.justPressed(LEFT_BUTTON)) {
depthFactor --;
arduboy.clear();
}
if (arduboy.justPressed(RIGHT_BUTTON)) {
depthFactor ++;
arduboy.clear();
}
drawSierpinsky(x1, y1, x2, y2, x3, y3, depthFactor);

arduboy.display();
}
1 Like