penrose-legacy-test.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "puzzles.h"
  4. #include "penrose-legacy.h"
  5. static int show_recursion = 0;
  6. static int ntiles, nfinal;
  7. static int test_cb(penrose_legacy_state *state, vector *vs, int n, int depth)
  8. {
  9. int i, xoff = 0, yoff = 0;
  10. double l = penrose_legacy_side_length(state->start_size, depth);
  11. double rball = l / 10.0;
  12. const char *col;
  13. ntiles++;
  14. if (state->max_depth == depth) {
  15. col = n == 4 ? "black" : "green";
  16. nfinal++;
  17. } else {
  18. if (!show_recursion)
  19. return 0;
  20. col = n == 4 ? "red" : "blue";
  21. }
  22. if (n != 4) yoff = state->start_size;
  23. printf("<polygon points=\"");
  24. for (i = 0; i < n; i++) {
  25. printf("%s%f,%f", (i == 0) ? "" : " ",
  26. penrose_legacy_vx(vs, i) + xoff,
  27. penrose_legacy_vy(vs, i) + yoff);
  28. }
  29. printf("\" style=\"fill: %s; fill-opacity: 0.2; stroke: %s\" />\n", col, col);
  30. printf("<ellipse cx=\"%f\" cy=\"%f\" rx=\"%f\" ry=\"%f\" fill=\"%s\" />",
  31. penrose_legacy_vx(vs, 0) + xoff, penrose_legacy_vy(vs, 0) + yoff,
  32. rball, rball, col);
  33. return 0;
  34. }
  35. static void usage_exit(void)
  36. {
  37. fprintf(stderr, "Usage: penrose-legacy-test [--recursion] "
  38. "P2|P3 SIZE DEPTH\n");
  39. exit(1);
  40. }
  41. int main(int argc, char *argv[])
  42. {
  43. penrose_legacy_state ps;
  44. int which = 0;
  45. while (--argc > 0) {
  46. char *p = *++argv;
  47. if (!strcmp(p, "-h") || !strcmp(p, "--help")) {
  48. usage_exit();
  49. } else if (!strcmp(p, "--recursion")) {
  50. show_recursion = 1;
  51. } else if (*p == '-') {
  52. fprintf(stderr, "Unrecognised option '%s'\n", p);
  53. exit(1);
  54. } else {
  55. break;
  56. }
  57. }
  58. if (argc < 3) usage_exit();
  59. if (strcmp(argv[0], "P2") == 0) which = PENROSE_P2;
  60. else if (strcmp(argv[0], "P3") == 0) which = PENROSE_P3;
  61. else usage_exit();
  62. ps.start_size = atoi(argv[1]);
  63. ps.max_depth = atoi(argv[2]);
  64. ps.new_tile = test_cb;
  65. ntiles = nfinal = 0;
  66. printf("\
  67. <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n\
  68. <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\"\n\
  69. \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n\
  70. \n\
  71. <svg xmlns=\"http://www.w3.org/2000/svg\"\n\
  72. xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n\n");
  73. printf("<g>\n");
  74. penrose_legacy(&ps, which, 0);
  75. printf("</g>\n");
  76. printf("<!-- %d tiles and %d leaf tiles total -->\n",
  77. ntiles, nfinal);
  78. printf("</svg>");
  79. return 0;
  80. }