+static void game_print_size(game_params *params, float *x, float *y)
+{
+ int pw, ph;
+
+ /*
+ * I'll use 4mm squares by default, I think. Simplest way to
+ * compute this size is to compute the pixel puzzle size at a
+ * given tile size and then scale.
+ */
+ game_compute_size(params, 400, &pw, &ph);
+ *x = pw / 100.0;
+ *y = ph / 100.0;
+}
+
+static void game_print(drawing *dr, game_state *state, int tilesize)
+{
+ int w = state->p.w, h = state->p.h, wh = w*h, n = state->p.n;
+ int ink, c[FOUR], i;
+ int x, y, r;
+ int *coords, ncoords, coordsize;
+
+ /* Ick: fake up `ds->tilesize' for macro expansion purposes */
+ struct { int tilesize; } ads, *ds = &ads;
+ ads.tilesize = tilesize;
+
+ ink = print_mono_colour(dr, 0);
+ for (i = 0; i < FOUR; i++)
+ c[i] = print_rgb_colour(dr, map_hatching[i], map_colours[i][0],
+ map_colours[i][1], map_colours[i][2]);
+
+ coordsize = 0;
+ coords = NULL;
+
+ print_line_width(dr, TILESIZE / 16);
+
+ /*
+ * Draw a single filled polygon around each region.
+ */
+ for (r = 0; r < n; r++) {
+ int octants[8], lastdir, d1, d2, ox, oy;
+
+ /*
+ * Start by finding a point on the region boundary. Any
+ * point will do. To do this, we'll search for a square
+ * containing the region and then decide which corner of it
+ * to use.
+ */
+ x = w;
+ for (y = 0; y < h; y++) {
+ for (x = 0; x < w; x++) {
+ if (state->map->map[wh*0+y*w+x] == r ||
+ state->map->map[wh*1+y*w+x] == r ||
+ state->map->map[wh*2+y*w+x] == r ||
+ state->map->map[wh*3+y*w+x] == r)
+ break;
+ }
+ if (x < w)
+ break;
+ }
+ assert(y < h && x < w); /* we must have found one somewhere */
+ /*
+ * This is the first square in lexicographic order which
+ * contains part of this region. Therefore, one of the top
+ * two corners of the square must be what we're after. The
+ * only case in which it isn't the top left one is if the
+ * square is diagonally divided and the region is in the
+ * bottom right half.
+ */
+ if (state->map->map[wh*TE+y*w+x] != r &&
+ state->map->map[wh*LE+y*w+x] != r)
+ x++; /* could just as well have done y++ */
+
+ /*
+ * Now we have a point on the region boundary. Trace around
+ * the region until we come back to this point,
+ * accumulating coordinates for a polygon draw operation as
+ * we go.
+ */
+ lastdir = -1;
+ ox = x;
+ oy = y;
+ ncoords = 0;
+
+ do {
+ /*
+ * There are eight possible directions we could head in
+ * from here. We identify them by octant numbers, and
+ * we also use octant numbers to identify the spaces
+ * between them:
+ *
+ * 6 7 0
+ * \ 7|0 /
+ * \ | /
+ * 6 \|/ 1
+ * 5-----+-----1
+ * 5 /|\ 2
+ * / | \
+ * / 4|3 \
+ * 4 3 2
+ */
+ octants[0] = x<w && y>0 ? state->map->map[wh*LE+(y-1)*w+x] : -1;
+ octants[1] = x<w && y>0 ? state->map->map[wh*BE+(y-1)*w+x] : -1;
+ octants[2] = x<w && y<h ? state->map->map[wh*TE+y*w+x] : -1;
+ octants[3] = x<w && y<h ? state->map->map[wh*LE+y*w+x] : -1;
+ octants[4] = x>0 && y<h ? state->map->map[wh*RE+y*w+(x-1)] : -1;
+ octants[5] = x>0 && y<h ? state->map->map[wh*TE+y*w+(x-1)] : -1;
+ octants[6] = x>0 && y>0 ? state->map->map[wh*BE+(y-1)*w+(x-1)] :-1;
+ octants[7] = x>0 && y>0 ? state->map->map[wh*RE+(y-1)*w+(x-1)] :-1;
+
+ d1 = d2 = -1;
+ for (i = 0; i < 8; i++)
+ if ((octants[i] == r) ^ (octants[(i+1)%8] == r)) {
+ assert(d2 == -1);
+ if (d1 == -1)
+ d1 = i;
+ else
+ d2 = i;
+ }
+/* printf("%% %d,%d r=%d: d1=%d d2=%d lastdir=%d\n", x, y, r, d1, d2, lastdir); */
+ assert(d1 != -1 && d2 != -1);
+ if (d1 == lastdir)
+ d1 = d2;
+
+ /*
+ * Now we're heading in direction d1. Save the current
+ * coordinates.
+ */
+ if (ncoords + 2 > coordsize) {
+ coordsize += 128;
+ coords = sresize(coords, coordsize, int);
+ }
+ coords[ncoords++] = COORD(x);
+ coords[ncoords++] = COORD(y);
+
+ /*
+ * Compute the new coordinates.
+ */
+ x += (d1 % 4 == 3 ? 0 : d1 < 4 ? +1 : -1);
+ y += (d1 % 4 == 1 ? 0 : d1 > 1 && d1 < 5 ? +1 : -1);
+ assert(x >= 0 && x <= w && y >= 0 && y <= h);
+
+ lastdir = d1 ^ 4;
+ } while (x != ox || y != oy);
+
+ draw_polygon(dr, coords, ncoords/2,
+ state->colouring[r] >= 0 ?
+ c[state->colouring[r]] : -1, ink);
+ }
+ sfree(coords);
+}
+