From: Ben Harris Date: Sat, 5 May 2012 17:35:55 +0000 (+0100) Subject: Substantial simplification of dochar(). The logic is the same, but X-Git-Tag: bedstead-001.000~85 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=ed6d6c4348907ff30827448b29ca46e550e84b3f;p=bedstead.git Substantial simplification of dochar(). The logic is the same, but macros and the removal of a gratuitous array of structures make it much less verbose. --- diff --git a/bedstead.c b/bedstead.c index 5835572..feaa377 100644 --- a/bedstead.c +++ b/bedstead.c @@ -104,6 +104,7 @@ #include #include +#include #include #include @@ -114,10 +115,6 @@ #define XPIX 100 #define YPIX 100 -struct corner { - int tl, tr, bl, br; -}; - void doprologue(void); void dochar(char data[YSIZE], unsigned flags); @@ -1112,85 +1109,48 @@ whitepixel(int x, int y, int bl, int br, int tr, int tl) void dochar(char data[YSIZE], unsigned flags) { - struct corner corner[XSIZE][YSIZE] = { }; int x, y; #define GETPIX(x,y) (getpix(data, (x), (y), flags)) +#define L GETPIX(x-1, y) +#define R GETPIX(x+1, y) +#define U GETPIX(x, y-1) +#define D GETPIX(x, y+1) +#define UL GETPIX(x-1, y-1) +#define UR GETPIX(x+1, y-1) +#define DL GETPIX(x-1, y+1) +#define DR GETPIX(x+1, y+1) clearpath(); for (x = 0; x < XSIZE; x++) { for (y = 0; y < YSIZE; y++) { if (GETPIX(x, y)) { + bool tl, tr, bl, br; + /* Assume filled in */ - corner[x][y].tl = 1; - corner[x][y].tr = 1; - corner[x][y].bl = 1; - corner[x][y].br = 1; + tl = tr = bl = br = true; /* Check for diagonals */ - if ((GETPIX(x-1, y) == 0 && - GETPIX(x, y-1) == 0 && - GETPIX(x-1, y-1) == 1) || - (GETPIX(x+1, y) == 0 && - GETPIX(x, y+1) == 0 && - GETPIX(x+1, y+1) == 1)) { - corner[x][y].tr = 0; - corner[x][y].bl = 0; - } - if ((GETPIX(x+1, y) == 0 && - GETPIX(x, y-1) == 0 && - GETPIX(x+1, y-1) == 1) || - (GETPIX(x-1, y) == 0 && - GETPIX(x, y+1) == 0 && - GETPIX(x-1, y+1) == 1)) { - corner[x][y].tl = 0; - corner[x][y].br = 0; - } + if ((UL && !U && !L) || (DR && !D && !R)) + tr = bl = false; + if ((UR && !U && !R) || (DL && !D && !L)) + tl = br = false; /* Avoid odd gaps */ - if (GETPIX(x-1, y) == 1 || - GETPIX(x-1, y-1) == 1 || - GETPIX(x, y-1) == 1) - corner[x][y].tl = 1; - if (GETPIX(x+1, y) == 1 || - GETPIX(x+1, y-1) == 1 || - GETPIX(x, y-1) == 1) - corner[x][y].tr = 1; - if (GETPIX(x-1, y) == 1 || - GETPIX(x-1, y+1) == 1 || - GETPIX(x, y+1) == 1) - corner[x][y].bl = 1; - if (GETPIX(x+1, y) == 1 || - GETPIX(x+1, y+1) == 1 || - GETPIX(x, y+1) == 1) - corner[x][y].br = 1; - blackpixel(x, YSIZE - y - 1, - corner[x][y].bl, corner[x][y].br, - corner[x][y].tr, corner[x][y].tl); + if (L || UL || U) tl = true; + if (R || UR || U) tr = true; + if (L || DL || D) bl = true; + if (R || DR || D) br = true; + blackpixel(x, YSIZE - y - 1, bl, br, tr, tl); } else { + bool tl, tr, bl, br; + /* Assume clear */ - corner[x][y].tl = 0; - corner[x][y].tr = 0; - corner[x][y].bl = 0; - corner[x][y].br = 0; + tl = tr = bl = br = false; /* white pixel -- just diagonals */ - if (GETPIX(x-1, y) == 1 && - GETPIX(x, y-1) == 1 && - GETPIX(x-1, y-1) == 0) - corner[x][y].tl = 1; - if (GETPIX(x+1, y) == 1 && - GETPIX(x, y-1) == 1 && - GETPIX(x+1, y-1) == 0) - corner[x][y].tr = 1; - if (GETPIX(x-1, y) == 1 && - GETPIX(x, y+1) == 1 && - GETPIX(x-1, y+1) == 0) - corner[x][y].bl = 1; - if (GETPIX(x+1, y) == 1 && - GETPIX(x, y+1) == 1 && - GETPIX(x+1, y+1) == 0) - corner[x][y].br = 1; - whitepixel(x, YSIZE - y - 1, - corner[x][y].bl, corner[x][y].br, - corner[x][y].tr, corner[x][y].tl); + if (L && U && !UL) tl = true; + if (R && U && !UR) tr = true; + if (L && D && !DL) bl = true; + if (R && D && !DR) br = true; + whitepixel(x, YSIZE - y - 1, bl, br, tr, tl); } } }