From: Andy Spencer Date: Tue, 12 Jan 2010 18:27:49 +0000 (+0000) Subject: Add knot X-Git-Url: http://pileus.org/git/?p=~andy%2Fct;a=commitdiff_plain;h=c66ad87d24f507de21f784591e8d5f361ea3c55f Add knot Convert a textual description of a knot into a graphical description using HTML tables. --- diff --git a/knot/.gitignore b/knot/.gitignore new file mode 100644 index 0000000..d201525 --- /dev/null +++ b/knot/.gitignore @@ -0,0 +1,3 @@ +html.c +*.html +knot diff --git a/knot/hitch.txt b/knot/hitch.txt new file mode 100644 index 0000000..a131f4b --- /dev/null +++ b/knot/hitch.txt @@ -0,0 +1,8 @@ + | +.---..--. +'|. ||.-|-+ + ||.-|-.| +.||-||-||--- +'-|-'|'-' + ||'-' | + |'----' diff --git a/knot/html.ct b/knot/html.ct new file mode 100644 index 0000000..40fbe44 --- /dev/null +++ b/knot/html.ct @@ -0,0 +1,41 @@ +<% #include "knot.h" %> + +<% static void print_img(int ptrn) { %> + <% if (ptrn) { %> + + <% if (ptrn & RIGHT) printf("r"); %> + <% if (ptrn & UP ) printf("u"); %> + <% if (ptrn & DOWN ) printf("d"); %>.png"> + <% } %> +<% } %> + +<% void print_index(tile_t **tiles) { %> + + + Knot + + + + + + + <% for (int row = 1; row < 10; row++) { %> + + <% for (int col = 1; col < 30; col++) { %> + + <% } %> + + <% } %> +
+ <% print_img(tiles[row][col].bot); %> + <% print_img(tiles[row][col].top); %> +
+ + +<% } %> diff --git a/knot/img/ld.png b/knot/img/ld.png new file mode 100644 index 0000000..232d866 Binary files /dev/null and b/knot/img/ld.png differ diff --git a/knot/img/lr.png b/knot/img/lr.png new file mode 100644 index 0000000..256c562 Binary files /dev/null and b/knot/img/lr.png differ diff --git a/knot/img/lu.png b/knot/img/lu.png new file mode 100644 index 0000000..135be4e Binary files /dev/null and b/knot/img/lu.png differ diff --git a/knot/img/rd.png b/knot/img/rd.png new file mode 100644 index 0000000..90f74ad Binary files /dev/null and b/knot/img/rd.png differ diff --git a/knot/img/ru.png b/knot/img/ru.png new file mode 100644 index 0000000..7f567b0 Binary files /dev/null and b/knot/img/ru.png differ diff --git a/knot/img/ud.png b/knot/img/ud.png new file mode 100644 index 0000000..059dc95 Binary files /dev/null and b/knot/img/ud.png differ diff --git a/knot/knot.c b/knot/knot.c new file mode 100644 index 0000000..13954d1 --- /dev/null +++ b/knot/knot.c @@ -0,0 +1,85 @@ +#include +#include + +#include "knot.h" + +static void do_tile(tile_t **tiles, int row, int col) +{ + tile_t *t = &tiles[row][col]; + tile_t *l = &tiles[row][col-1]; + tile_t *r = &tiles[row][col+1]; + tile_t *u = &tiles[row-1][col]; + //tile_t *d = &tiles[row+1][col]; + + /* Fill in what we know */ + switch (tiles[row][col].c) { + case '-': t->top = LEFT | RIGHT; break; + case '|': t->top = UP | DOWN; break; + case '\'': t->top = UP; break; + case '.': t->top = DOWN; break; + } + + /* Follow bottoms */ + if (t->c == '|' && (l->top | l->bot) & RIGHT) + t->bot = LEFT | RIGHT; + if (t->c == '-' && (u->top | u->bot) & DOWN) + t->bot = UP | DOWN; + + /* Adds sides for ''s and .'s */ + if (t->c == '.' || t->c == '\'') { + if ((l->top | l->bot) & RIGHT) + t->top |= LEFT; + else + t->top |= RIGHT; + } + +} + +static void print_ptrn(int ptrn) +{ + printf("%c", ptrn & LEFT ? '<' : '-'); + printf("%c", ptrn & RIGHT ? '>' : '-'); + printf("%c", ptrn & UP ? '^' : '-'); + printf("%c", ptrn & DOWN ? 'v' : '-'); +} + +int main() +{ + /* Init tiles */ + tile_t *tiles[64]; + for (int i = 0; i < 64; i++) + tiles[i] = calloc(512, sizeof(tile_t)); + + /* Read data */ + char c; + int row = 1, col = 1; + while ((c = getchar()) != EOF) { + if (row > 63 || col > 511) + exit(-1); + if (c == '\n') { + row++; + col = 1; + } else { + tiles[row][col].c = c; + col++; + } + } + + /* Process */ + for (row = 1; row < 63; row++) + for (col = 1; col < 511; col++) + do_tile(tiles, row, col); + + /* Output */ + //for (row = 1; row < 63; row++) { + // for (col = 1; col < 511; col++) { + // print_ptrn(tiles[row][col].top); + // print_ptrn(tiles[row][col].bot); + // printf(" "); + // } + // printf("\n"); + //} + + /* HTML */ + print_index(tiles); +} diff --git a/knot/knot.h b/knot/knot.h new file mode 100644 index 0000000..0e77797 --- /dev/null +++ b/knot/knot.h @@ -0,0 +1,14 @@ +enum { + LEFT = 1<<0, + RIGHT = 1<<1, + UP = 1<<2, + DOWN = 1<<3, +}; + +typedef struct { + char c; + int top; + int bot; +} tile_t; + +void print_index(tile_t **tiles); diff --git a/knot/mkfile b/knot/mkfile new file mode 100644 index 0000000..f38e2c9 --- /dev/null +++ b/knot/mkfile @@ -0,0 +1,10 @@ +PROGS=knot +CLEAN=html.c *.html + +default:V: hitch.html +knot: knot.o html.o knot.h + +%.html: $PROGS %.txt + ./$PROGS < $stem.txt > $stem.html + +<../mkcommon