Guest User

bullshit.c

a guest
Jul 16th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.32 KB | None | 0 0
  1. /*
  2.   [2018-07-13] Challenge #365 [Hard] Tessellations and Tilings
  3.   /r/dailyprogrammer https://redd.it/8ylltu
  4.  
  5.   *grumble*grumble* *mutter...*
  6.   i hope you like this, view output at: https://imgur.com/a/pKyjatF
  7.  
  8.   it doesn't seem so bad now... :/
  9.  
  10.   usage: $ for i in bullshit*.txt; do
  11.              cat $i | ./bullshit | ffmpeg -i /dev/stdin $(basename $i .txt).png
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17.  
  18. #define FORIJ(N) for(int j = 0; j < (N); j++) \
  19.                      for(int i = 0; i < (N); i++)
  20.  
  21. #define ROT90(N, BEFORE, AFTER, TMP) do { \
  22.             FORIJ(N) TMP[i][(N)-j-1] = BEFORE[j][i]; \
  23.             FORIJ(N) AFTER[j][i] = TMP[j][i]; \
  24.         } while(0)
  25.  
  26. extern unsigned char font[]; /* forward declaration, long array */
  27.  
  28. typedef struct { int ch; int rot; char x[8][8]; } glyph_t;
  29.  
  30. glyph_t mk_glyph(int ch, int rot) {
  31.     glyph_t ret;
  32.     ch %= 256;
  33.     if(rot < 0) rot = rot + (-4)*((rot-3)/4);
  34.     rot %= 4;
  35.     FORIJ(8) {
  36.         int byte = font[8*ch + j];
  37.         int mask = (0x80 >> i);
  38.         int x = (byte & mask) ? 'x' : '.';
  39.     ret.x[j][i] = x;
  40.     }
  41.     char scratch[8][8];
  42.     while(rot--) { ROT90(8, ret.x, ret.x, scratch); }
  43.     ret.ch = ch; ret.rot = rot;
  44.     return ret;
  45. }
  46.  
  47. void print_glyph(glyph_t g) {
  48.     for(int j = 0; j < 8; j++) {
  49.         for(int i = 0; i < 8; i++) putc(g.x[j][i], stdout);
  50.         putc('\n', stdout);
  51.     }
  52.     return;
  53. }
  54.  
  55. #define MAX_N 12
  56. #define MAX_PX (8*N)
  57.  
  58. int N;
  59. int PX;
  60. int TURN;
  61.  
  62. glyph_t grid[2*MAX_N][2*MAX_N];
  63.  
  64. glyph_t after[MAX_N][MAX_N];
  65. glyph_t scratch[MAX_N][MAX_N];
  66.  
  67. int main(int argc, char *argv[]) {
  68.     int tmp, c;
  69.  
  70.     tmp = scanf("%d", &TURN);
  71.     if(tmp <= 0 || (TURN % 90)) { fprintf(stderr, "bad TURN\n"); exit(1); }
  72.     if(TURN < 0) { TURN = TURN + (-90)*((TURN-90)/90); }
  73.     TURN = (TURN / 90) % 4;
  74.  
  75.     tmp = scanf("%d", &N);
  76.     if(tmp <= 0 || N < 1 || N >= MAX_N) { fprintf(stderr, "bad N\n"); exit(1); }
  77.  
  78.     for(int i = 0; i < N*N; i++) {
  79.         do { c = getc(stdin); } while(!isprint(c));
  80.         int row = i / N;
  81.         int col = i % N;
  82.         after[row][col] = mk_glyph(c, 0);
  83.     }
  84.  
  85.     FORIJ(N) { grid[0+j][0+i] = mk_glyph(after[j][i].ch, 0*TURN); }
  86.  
  87.     for(int i = 0; i < TURN; i++) ROT90(N, after, after, scratch);
  88.     FORIJ(N) { grid[0+j][N+i] = mk_glyph(after[j][i].ch, 1*TURN); }
  89.  
  90.     for(int i = 0; i < TURN; i++) ROT90(N, after, after, scratch);
  91.     FORIJ(N) { grid[N+j][N+i] = mk_glyph(after[j][i].ch, 2*TURN); }
  92.  
  93.     for(int i = 0; i < TURN; i++) ROT90(N, after, after, scratch);
  94.     FORIJ(N) { grid[N+j][0+i] = mk_glyph(after[j][i].ch, 3*TURN); }
  95.  
  96.     PX = 8*2*N;
  97.     printf("P1\n%d %d\n1", PX, PX);
  98.  
  99.     int width = 0;
  100.     FORIJ(PX) {
  101.         if(width == 0) printf("\n");
  102.         width = (width + 2) % 64;
  103.  
  104.         glyph_t g = grid[j / 8][i / 8];
  105.         int x = g.x[j % 8][i % 8];
  106.  
  107.         if(x == 'x') printf(" 1");
  108.         else printf(" 0");
  109.     }
  110.  
  111.     printf("\n");
  112.     return 0;
  113. }
  114.  
  115. /* http://web.mit.edu/freebsd/head/share/syscons/fonts/cp437-8x8.fnt after uudecode */
Advertisement
Add Comment
Please, Sign In to add comment