// Chaos game, based on the task statement from https://rosettacode.org/wiki/Chaos_game // // Creates a Sierpinski triangle. // // ref: http://math.bu.edu/DYSYS/chaos-game/node1.html // #include #include #include #include #include "ppm.h" #define SIZE 600 // image size #define ITER 100000 // number of iterations typedef struct { int x, y; } Point; int main( int argc, char *argv[]) { srand(time(0)); ppm a = ppm_new( SIZE, SIZE); // corners of a triangle // Point p[3] = { { 0, SIZE-1}, { SIZE/2, 0}, { SIZE-1, SIZE-1} }; #if 0 int z = 0; #else int z = 255; #endif memset( a.r[0], z, a.rows*a.cols); memset( a.g[0], z, a.rows*a.cols); memset( a.b[0], z, a.rows*a.cols); // random starting point // int x = rand() % SIZE, y = rand() % SIZE, k; for( int iter = 0; iter < ITER; ++iter) { // random corner // k = rand() % 3; x += (p[k].x - x)/2; y += (p[k].y - y)/2; #if 0 // should not happen if( x < 0 || x >= SIZE || y < 0 || y >= SIZE) { fprintf( stderr, "x or y out of range!\n"); return 1; } #endif #if 0 a.r[y][x] = 255; #else switch( k) { case 0: a.g[y][x] = a.b[y][x] = 0; break; case 1: a.r[y][x] = a.b[y][x] = 0; break; case 2: a.r[y][x] = a.g[y][x] = 0; break; } #endif } ppm_write(a); }