#include <math.h>
#include <helianthus.h>
#define COUNT 9
unsigned int colors[3] = { COLOR_RED, COLOR_BLUE, COLOR_YELLOW };
void drawTriangle(double x, double y, double radius, double angle, unsigned int color, int level) {
if (level <= 0) {
saveState();
strokeWidth(radius * 0.1);
fill(color);
regularPolygon(x, y, 3, radius, angle);
restoreState();
} else {
--level;
double r = radius/((COUNT + 3)/2);
radius -= r;
saveState();
noStroke();
fill(colorWithAlpha(color, 0.5));
regularPolygon(x, y, 3, radius - r - r, angle);
restoreState();
double a0 = angle*PI/180;
double da = 2*PI/3;
for(int i = 0; i < 3; ++i) {
double x0 = x + radius*cos(a0 + i*da);
double y0 = y + radius*sin(a0 + i*da);
double x1 = x + radius*cos(a0 + i*da + da);
double y1 = y + radius*sin(a0 + i*da + da);
double dx = cos(a0 + i*da + da/2)*r/2;
double dy = sin(a0 + i*da + da/2)*r/2;
for(int j = 0; j < COUNT; ++j) {
double k = (double)j/(COUNT + 1);
double xx = x0*(1 - k) + x1*k;
double yy = y0*(1 - k) + y1*k;
if (j % 2) { xx -= dx; yy -= dy; }
double a = angle + j*180;
drawTriangle(xx, yy, r, a, colors[i], level);
}
}
}
}
void init() {
}
void draw() {
double w = windowGetWidth();
double h = windowGetHeight();
saveState();
translate(w/2, h/2);
drawTriangle(0, 0, 400, 0, colors[0], 2);
restoreState();
}
int main() {
windowSetResizable(TRUE);
windowSetVariableFrameRate();
windowSetInit(&init);
windowSetDraw(&draw);
windowRun();
}