#include #include #include #include typedef long hint; // Compile with gcc -DITERATION=n or uncomment the #define // The XPM image printed to stdout will be 2^n pixels on a side //#define ITERATION 11 #define IW (1 << ITERATION) // Monochrome: #define S0 "0 0 0" #define S1 "16384 16384 16384" #define S2 "32768 32768 32768" #define S3 "49152 49152 49152" // Color: //#define S0 "65535 0 0" //#define S1 "65535 65535 0" //#define S2 "0 65535 0" //#define S3 "0 0 65535" // iw is half the real dimensions, so we can just check x < iw int quadrant(hint x, hint y, hint *newx, hint *newy, hint iw) { if(x < iw) { *newx = x; if(y < iw) { // 00 // x0 *newy = y; return 0; } else { // x0 // 00 *newy = y-iw; return 1; } } else { *newx = x - iw; if(y < iw) { // 00 // 0x *newy = y; return 3; } else { // 0x // 00 *newy = y - iw; return 2; } } } char *hilbertA(hint x, hint y, hint iw); char *hilbertC(hint x, hint y, hint iw); char *hilbertD(hint x, hint y, hint iw); char *hilbertU(hint x, hint y, hint iw); char *hilbertA(hint x, hint y, hint iw) { iw = iw >> 1; hint newx, newy; int quad = quadrant(x,y, &newx,&newy, iw); if(iw == 1) { switch(quad) { case 0: return S0; case 1: return S1; case 2: return S2; case 3: return S3; } } switch(quad) { case 0: return hilbertD(newx, newy, iw); case 1: return hilbertA(newx, newy, iw); case 2: return hilbertA(newx, newy, iw); case 3: return hilbertC(newx, newy, iw); } } char *hilbertC(hint x, hint y, hint iw) { iw = iw >> 1; hint newx, newy; int quad = quadrant(x,y, &newx,&newy, iw); if(iw == 1) { switch(quad) { case 2: return S0; case 1: return S1; case 0: return S2; case 3: return S3; } } switch(quad) { case 2: return hilbertU(newx, newy, iw); case 1: return hilbertC(newx, newy, iw); case 0: return hilbertC(newx, newy, iw); case 3: return hilbertA(newx, newy, iw); } } char *hilbertU(hint x, hint y, hint iw) { iw = iw >> 1; hint newx, newy; int quad = quadrant(x,y, &newx,&newy, iw); if(iw == 1) { switch(quad) { case 2: return S0; case 3: return S1; case 0: return S2; case 1: return S3; } } switch(quad) { case 2: return hilbertC(newx, newy, iw); case 3: return hilbertU(newx, newy, iw); case 0: return hilbertU(newx, newy, iw); case 1: return hilbertD(newx, newy, iw); } } char *hilbertD(hint x, hint y, hint iw) { iw = iw >> 1; hint newx, newy; int quad = quadrant(x,y, &newx,&newy, iw); if(iw == 1) { switch(quad) { case 0: return S0; case 3: return S1; case 2: return S2; case 1: return S3; } } switch(quad) { case 0: return hilbertA(newx, newy, iw); case 3: return hilbertD(newx, newy, iw); case 2: return hilbertD(newx, newy, iw); case 1: return hilbertU(newx, newy, iw); } } main() { printf("P3 %d %d 65535\n", IW, IW); hint x, y; for(x=0; x < IW; x++) { for(y=0; y < IW; y++) { printf("%s\n", hilbertA(x,y, IW)); } } }