X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=pattern.c;fp=pattern.c;h=e5f958abb817a600f10b1f9985ca2fd095380b7c;hb=ff218728c6953ede1957ddb5b039bcb2ba920a44;hp=ece1ed476e562913941b64b6d74409a35a91e9d7;hpb=b8313181a6104624000e9cc008e8e26b67aeb655;p=sgt-puzzles.git diff --git a/pattern.c b/pattern.c index ece1ed4..e5f958a 100644 --- a/pattern.c +++ b/pattern.c @@ -308,7 +308,18 @@ static void generate(random_state *rs, int w, int h, unsigned char *retgrid) fgrid2 = snewn(w*h, float); memcpy(fgrid2, fgrid, w*h*sizeof(float)); qsort(fgrid2, w*h, sizeof(float), float_compare); - threshold = fgrid2[w*h/2]; + /* Choose a threshold that makes half the pixels black. In case of + * an odd number of pixels, select randomly between just under and + * just over half. */ + { + int index = w * h / 2; + if (w & h & 1) + index += random_upto(rs, 2); + if (index < w*h) + threshold = fgrid2[index]; + else + threshold = fgrid2[w*h-1] + 1; + } sfree(fgrid2); for (i = 0; i < h; i++) {