/* tiles.c: program to fill in a deficient 2^n by 2^n square with trominoes Call with: "tiles "*/ #include double scale; tri_fill(int half_size,int x_center,int y_center,float x_missing, float y_missing) /* half_size is half the width of the square to be filled (x_center,y_center) is its center and (x_missing,y_missing) are the coordinates of the center of missing square to be filled */ /* Note that all squares are passed by their centers as this has the advantage of rotational symmetry */ { int x_dir,y_dir,quarter_size; double x1,x2,x4,y1,y3,y5; x_dir=(x_missing>=x_center)?1:-1; y_dir=(y_missing>=y_center)?1:-1; x1=(x_center-x_dir*0.9)*scale; x2=(x_center+x_dir*0.9)*scale; x4=(x_center-x_dir*0.1)*scale; y1=(y_center-y_dir*0.9)*scale; y3=(y_center-y_dir*0.1)*scale; y5=(y_center+y_dir*0.9)*scale; /* Fill in the L shape at the center in the three boxes in directions other than the (x_dir,y_dir) direction */ printf("n %.2lf %.2lf m %.2lf %.2lf l %.2lf %.2lf l ",x1,y1,x2,y1,x2,y3); printf("%.2lf %.2lf l %.2lf %.2lf l %.2lf %.2lf l ",x4,y3,x4,y5,x1,y5); printf("%.2lf %.2lf l cp doit\n",x1,y1); /* If the half-size is 1, we're done. Otherwise, fill in the four deficient squares that are remaining of half the side length */ if (half_size>1) { quarter_size=half_size/2; tri_fill(quarter_size,x_center-quarter_size*x_dir, y_center+quarter_size*y_dir,x_center-x_dir*0.5, y_center+y_dir*0.5); tri_fill(quarter_size,x_center-quarter_size*x_dir, y_center-quarter_size*y_dir,x_center-x_dir*0.5, y_center-y_dir*0.5); tri_fill(quarter_size,x_center+quarter_size*x_dir, y_center-quarter_size*y_dir,x_center+x_dir*0.5, y_center-y_dir*0.5); tri_fill(quarter_size,x_center+quarter_size*x_dir, y_center+quarter_size*y_dir,x_missing,y_missing); } } main(int argc,char **argv) { int n,x_missing,y_missing; if (argc!=4) { fprintf(stderr,"Usage: tiles .\n"); fprintf(stderr,"Size must be a power of 2\n"); exit(-1); } sscanf(argv[1],"%d",&n); /* Print out postscript header*/ printf("%%!PS-Adobe-1.0\n%%%%Pages: (atend)\n"); printf("%%%%BoundingBox:0 0 500 720\n%%%%Endcomments\n\n",n,n); /* Postscript Macro definitions */ printf("/n{ newpath} def\n/m{ moveto} def\n/l{ lineto} def\n"); printf("/doit {gsave stroke grestore} def\n"); printf("/cp{ closepath} def\n\n0 setlinewidth\n\n%%Prog begin\n\n"); sscanf(argv[2],"%d",&x_missing); sscanf(argv[3],"%d",&y_missing); scale=500.0/n; /* Fill in the missing square */ printf("n %.1lf %.1lf m ",(x_missing-0.1)*scale,(y_missing-0.1)*scale); printf("%.1lf %.1lf l ",(x_missing-0.9)*scale,(y_missing-0.1)*scale); printf("%.1lf %.1lf l ",(x_missing-0.9)*scale,(y_missing-0.9)*scale); printf("%.1lf %.1lf l ",(x_missing-0.1)*scale,(y_missing-0.9)*scale); printf("cp gsave eofill grestore\n"); /* Fill the remaining region */ tri_fill(n/2,n/2,n/2,x_missing-0.5,y_missing-0.5); }