mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 08:01:30 -07:00
Files

Having stated the principle in the previous commit, I should apply it consistently. A source file linked into the Puzzles library of common support code should not also define a main() under ifdef. This commit only goes as far as the _library_ support modules. It would be a much bigger job to do the same for all the actual _puzzles_ that have test main()s or standalone-solver main()s. And it's not necessary, because modifying one of those source files only triggers a rebuild of _one_ puzzle, not absolutely everything. (Not to mention that it's quite likely the puzzle and the test main() will need to be modified in conjunction anyway.) As in the previous commit, this has required exposing a few internal API functions as global, and maybe editing them a bit. In particular, the one-shot internal function that divvy_rectangle() loops on until it succeeds is now exposed as divvy_rectangle_attempt(), which means the test program doesn't have to condition a failure counter into the real function. I've thrown away penrose-vector-test completely, because that didn't look like a test program with any ongoing use at all - it was surely vestigial, while James was getting the vector representation up and running in the first place.
96 lines
2.4 KiB
C
96 lines
2.4 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "puzzles.h"
|
|
#include "penrose.h"
|
|
|
|
static int show_recursion = 0;
|
|
static int ntiles, nfinal;
|
|
|
|
static int test_cb(penrose_state *state, vector *vs, int n, int depth)
|
|
{
|
|
int i, xoff = 0, yoff = 0;
|
|
double l = penrose_side_length(state->start_size, depth);
|
|
double rball = l / 10.0;
|
|
const char *col;
|
|
|
|
ntiles++;
|
|
if (state->max_depth == depth) {
|
|
col = n == 4 ? "black" : "green";
|
|
nfinal++;
|
|
} else {
|
|
if (!show_recursion)
|
|
return 0;
|
|
col = n == 4 ? "red" : "blue";
|
|
}
|
|
if (n != 4) yoff = state->start_size;
|
|
|
|
printf("<polygon points=\"");
|
|
for (i = 0; i < n; i++) {
|
|
printf("%s%f,%f", (i == 0) ? "" : " ",
|
|
v_x(vs, i) + xoff, v_y(vs, i) + yoff);
|
|
}
|
|
printf("\" style=\"fill: %s; fill-opacity: 0.2; stroke: %s\" />\n", col, col);
|
|
printf("<ellipse cx=\"%f\" cy=\"%f\" rx=\"%f\" ry=\"%f\" fill=\"%s\" />",
|
|
v_x(vs, 0) + xoff, v_y(vs, 0) + yoff, rball, rball, col);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void usage_exit(void)
|
|
{
|
|
fprintf(stderr, "Usage: penrose-test [--recursion] P2|P3 SIZE DEPTH\n");
|
|
exit(1);
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
penrose_state ps;
|
|
int which = 0;
|
|
|
|
while (--argc > 0) {
|
|
char *p = *++argv;
|
|
if (!strcmp(p, "-h") || !strcmp(p, "--help")) {
|
|
usage_exit();
|
|
} else if (!strcmp(p, "--recursion")) {
|
|
show_recursion = 1;
|
|
} else if (*p == '-') {
|
|
fprintf(stderr, "Unrecognised option '%s'\n", p);
|
|
exit(1);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (argc < 3) usage_exit();
|
|
|
|
if (strcmp(argv[0], "P2") == 0) which = PENROSE_P2;
|
|
else if (strcmp(argv[0], "P3") == 0) which = PENROSE_P3;
|
|
else usage_exit();
|
|
|
|
ps.start_size = atoi(argv[1]);
|
|
ps.max_depth = atoi(argv[2]);
|
|
ps.new_tile = test_cb;
|
|
|
|
ntiles = nfinal = 0;
|
|
|
|
printf("\
|
|
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n\
|
|
<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\"\n\
|
|
\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n\
|
|
\n\
|
|
<svg xmlns=\"http://www.w3.org/2000/svg\"\n\
|
|
xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n\n");
|
|
|
|
printf("<g>\n");
|
|
penrose(&ps, which, 0);
|
|
printf("</g>\n");
|
|
|
|
printf("<!-- %d tiles and %d leaf tiles total -->\n",
|
|
ntiles, nfinal);
|
|
|
|
printf("</svg>");
|
|
|
|
return 0;
|
|
}
|