Blame onefile/triangle-fractal.c

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