/* Reply-To: "Kenneth D. Weinert" To: potm@att.com Subject: gif quilt squares */ /* compilation: * gcc -o quilt quilt.c -lgd -lm * * note that this code depends on the gd package freely available at * http://www.boutell.com/gd/ * * this program assumes an input file that consists of the input file to * the contestants program, a line that contains "--", and then the * contentants solution. * * it outputs quilt.gif file that shows the quilt. * * note also that the colors may require tweaking by someone who has some * idea of what colors would look good. Aside from the ones labelled by * name (red, yellow, etc) they are web safe colors, but I make no * guarantee that they are distinguishable from each other or look * reasonable. color sense is *not* one of my strong points. * * squares are separated by a black line as is each color in the square. * you get a quilt that looks like a bunch of triangles. * * I know you said attachments are a problem for you, but I thought I'd * attach the posted solution for you. If you can't see it, then just * compile this program :-) */ #include #include #include #include #include char squares[8000][4]; int maxSquares = 0; char* input[1000]; int maxInput = 0; #define squareSize 20 int main(int argc, char* argv[]) { FILE* quiltFP; gdImagePtr tmpQuilt; gdImagePtr quilt; gdPoint tri[3]; char inBuf[1000]; char* ptr; int colors[26]; int black; int row, col; int numRows, numCols; int i, inputIdx; int squareIdx, rotation, sideIdx, currentColor; numRows = numCols = 0; quiltFP = fopen("quilt.gif", "w"); while (fgets(inBuf, 999, stdin) != NULL) { if (strncmp(inBuf, "--", 2) == 0) { fprintf(stderr, "found input/solution break\n"); break; } ptr = strtok(inBuf, " \n"); ptr = strtok(NULL, " \n"); /* skip the index */ for (i = 0; i < 4; i++) { squares[maxSquares][i] = *ptr; ptr = strtok(NULL, " \n"); } maxSquares++; fprintf(stderr, "Read %d squares\n", maxSquares); if (maxSquares == 8000) { fprintf(stderr, "Too many squares: 8000 is max\n"); return EXIT_FAILURE; } } while (fgets(inBuf, 999, stdin) != NULL) { printf("inBuf[0]=%s\n",&inBuf[0]); if (!isdigit(inBuf[0])) { break; } input[maxInput++] = strdup(inBuf); printf("maxInput increased to %d\n",maxInput); } ptr = strchr(input[0], ','); for(numCols = 1; numCols; numCols++) { if ((ptr = strchr(ptr+1, ',')) == NULL) { break; } } quilt = gdImageCreate(numCols * squareSize, maxInput * squareSize); black = gdImageColorAllocate(quilt, 0x00, 0x00, 0x00); colors[0] = gdImageColorAllocate(quilt, 0x00, 0x00, 0x00); /* Black */ /* colors[0] = gdImageColorAllocate(quilt, 0x66, 0x99, 0x99); */ /* A */ colors[1] = gdImageColorAllocate(quilt, 0x00, 0x00, 0xff); /* Blue */ colors[2] = gdImageColorAllocate(quilt, 0xff, 0xcc, 0x33); /* C */ colors[3] = gdImageColorAllocate(quilt, 0x00, 0x00, 0xff); /* D */ colors[4] = gdImageColorAllocate(quilt, 0x66, 0xcc, 0x66); /* E */ colors[5] = gdImageColorAllocate(quilt, 0x99, 0x33, 0x99); /* F */ colors[6] = gdImageColorAllocate(quilt, 0x00, 0xff, 0x00); /* Green */ colors[7] = gdImageColorAllocate(quilt, 0x99, 0xcc, 0xff); /* H */ colors[8] = gdImageColorAllocate(quilt, 0x66, 0x99, 0x33); /* I */ colors[9] = gdImageColorAllocate(quilt, 0xff, 0xff, 0x00); /* J */ colors[10] = gdImageColorAllocate(quilt, 0xff, 0x00, 0x00); /* K */ colors[11] = gdImageColorAllocate(quilt, 0xff, 0x66, 0x33); /* L */ colors[12] = gdImageColorAllocate(quilt, 0x33, 0xff, 0x66); /* M */ colors[13] = gdImageColorAllocate(quilt, 0x66, 0x33, 0xff); /* N */ colors[14] = gdImageColorAllocate(quilt, 0xff, 0xff, 0x00); /* Orange */ colors[15] = gdImageColorAllocate(quilt, 0x66, 0x00, 0x00); /* Pink */ colors[16] = gdImageColorAllocate(quilt, 0x99, 0x99, 0x99); /* Q */ colors[17] = gdImageColorAllocate(quilt, 0xff, 0x00, 0x00); /* Red */ colors[18] = gdImageColorAllocate(quilt, 0x33, 0x33, 0x33); /* S */ colors[19] = gdImageColorAllocate(quilt, 0x66, 0x66, 0x66); /* T */ colors[20] = gdImageColorAllocate(quilt, 0x33, 0x66, 0x66); /* U */ colors[21] = gdImageColorAllocate(quilt, 0x66, 0x66, 0x33); /* V */ colors[22] = gdImageColorAllocate(quilt, 0xff, 0xff, 0xff); /* White */ colors[23] = gdImageColorAllocate(quilt, 0x99, 0x33, 0x33); /* X */ colors[24] = gdImageColorAllocate(quilt, 0xff, 0xff, 0x00); /* Yellow */ colors[25] = gdImageColorAllocate(quilt, 0x33, 0x66, 0xff); /* Z */ row = 0; for (row = 0; row < maxInput; row++) { col = 0; ptr = strtok(input[row], " \n"); while (ptr && *ptr) { printf("doing row %d col %d ... maxInput=%d\n",row,col,maxInput); if ((squareIdx = atoi(ptr)) == 0) { break; } ptr = strchr(ptr, ',') + 1; rotation = atoi(ptr); switch(rotation) { case 0: sideIdx = 0; break; case 90: sideIdx = 3; break; case 180: sideIdx = 2; break; case 270: sideIdx = 1; break; } for (i = 0; i < 4; i++) { currentColor = colors[squares[squareIdx - 1][sideIdx] - 'A']; switch(i) { case 0: tri[0].x = col * squareSize; tri[0].y = row * squareSize; tri[1].x = tri[0].x + squareSize; tri[1].y = tri[0].y; tri[2].x = tri[0].x + squareSize / 2; tri[2].y = tri[0].y + squareSize / 2; break; case 1: tri[0].x = col * squareSize + squareSize; tri[0].y = row * squareSize; tri[1].x = tri[0].x; tri[1].y = tri[0].y + squareSize; tri[2].x = tri[0].x - squareSize / 2; tri[2].y = tri[0].y + squareSize / 2; break; case 2: tri[0].x = col * squareSize + squareSize; tri[0].y = row * squareSize + squareSize; tri[1].x = tri[0].x - squareSize; tri[1].y = tri[0].y; tri[2].x = tri[0].x - squareSize / 2; tri[2].y = tri[0].y - squareSize / 2; break; case 3: tri[0].x = col * squareSize; tri[0].y = row * squareSize + squareSize; tri[1].x = tri[0].x; tri[1].y = tri[0].y - squareSize; tri[2].x = tri[0].x + squareSize / 2; tri[2].y = tri[0].y - squareSize / 2; break; } gdImageFilledPolygon(quilt, tri, 3, currentColor); gdImagePolygon(quilt, tri, 3, black); sideIdx++; sideIdx %= 4; } col++; if ((ptr = strtok(NULL, " \n")) == NULL) { numCols = col; break; } } numRows++; } gdImageGif(quilt, quiltFP); fclose(quiltFP); gdImageDestroy(quilt); return 0; }