/* use the javascript canvas to paint a polyomino tiling * from its letter matrix */ #include #include #include #define BOARDWIDTH 250 static const int y0 = 20; static int y1; static int x0, x1; static int dpl; /* dots per letter */ static int width; /* in letters */ static int lineno; /* line number */ #define xloc(n) ((n)*dpl + x0) static char thisline[BOARDWIDTH+6], lastline[BOARDWIDTH+6]; static void setScale(void) { dpl = 20; while(dpl*width > 760) --dpl; if(dpl < 7) { fprintf(stderr, "dpl %d too small\n", dpl); exit(1); } x0 = 400 - dpl * width / 2; x1 = xloc(width); } /* setScale */ static void printRow(void) { int agree, i, y2; if(!lineno) { /* top line of the rectangle */ printf("cvx.moveTo(%d,%d);\n", x0, y0); printf("cvx.lineTo(%d,%d);\n", x1, y0); y1 = y0; } else { /* borders with previous row */ agree = -1; for(i=0; i"); puts("cv=document.getElementById('myCanvas');"); puts("cvx=cv.getContext('2d');"); puts("cvx.beginPath();"); while(fgets(thisline, sizeof(thisline), f)) { s = strpbrk(thisline, "\r\n"); if(s) *s = 0; w = strlen(thisline); if(!lineno) { width = w; setScale(); } else if(width != w) { fprintf(stderr, "width mismatch %d versus %d\n", width, w); exit(1); } printRow(); strcpy(lastline, thisline); } /* bottom line */ printf("cvx.moveTo(%d,%d);\n", x0, y1); printf("cvx.lineTo(%d,%d);\n", x1, y1); printf("cvx.moveTo(%d,%d);\n", x0, y1+20); puts("cvx.stroke();"); puts(""); } /* main */