spectre.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #ifndef PUZZLES_SPECTRE_H
  2. #define PUZZLES_SPECTRE_H
  3. struct SpectrePatchParams {
  4. /*
  5. * A patch of Spectre tiling is identified by giving
  6. *
  7. * - the coordinates of the central Spectre, using a
  8. * combinatorial coordinate system similar to the Hat tiling in
  9. * hat.h
  10. *
  11. * - the orientation of that Spectre, as a number from 0 to 11 (a
  12. * multiple of 30 degrees), with 0 representing the 'head' of
  13. * the Spectre facing upwards, and numbers counting
  14. * anticlockwise.
  15. *
  16. * Coordinates are a sequence of small non-negative integers. The
  17. * valid range for each coordinate depends on the next coordinate,
  18. * or on final_hex if it's the last one in the list. The largest
  19. * valid range is {0,...,7}.
  20. *
  21. * 'final_hex' is one of the letters GDJLXPSFY.
  22. * spectre_valid_hex_letter() will check that.
  23. */
  24. int orientation;
  25. size_t ncoords;
  26. unsigned char *coords;
  27. char final_hex;
  28. };
  29. bool spectre_valid_hex_letter(char c);
  30. /*
  31. * Fill in SpectrePatchParams with a randomly selected set of
  32. * coordinates, in enough detail to generate a patch of tiling filling
  33. * a w x h area. The unit of measurement is 1/(2*sqrt(2)) of a Spectre
  34. * edge, i.e. such that a single Spectre edge at 45 degrees would
  35. * correspond to the vector (2,2).
  36. *
  37. * The 'coords' field of the structure will be filled in with a new
  38. * dynamically allocated array. Any previous pointer in that field
  39. * will be overwritten.
  40. */
  41. void spectre_tiling_randomise(struct SpectrePatchParams *params, int w, int h,
  42. random_state *rs);
  43. /*
  44. * Validate a SpectrePatchParams to ensure it contains no illegal
  45. * coordinates. Returns NULL if it's acceptable, or an error string if
  46. * not.
  47. */
  48. const char *spectre_tiling_params_invalid(
  49. const struct SpectrePatchParams *params);
  50. /*
  51. * Generate the actual set of Spectre tiles from a SpectrePatchParams,
  52. * passing each one to a callback. The callback receives the vertices
  53. * of each point, in the form of an array of 4*14 integers. Each
  54. * vertex is represented by four consecutive integers in this array,
  55. * with the first two giving the x coordinate and the last two the y
  56. * coordinate. Each pair of integers a,b represent a single coordinate
  57. * whose value is a + b*sqrt(3). The unit of measurement is as above.
  58. */
  59. typedef void (*spectre_tile_callback_fn)(void *ctx, const int *coords);
  60. #define SPECTRE_NVERTICES 14
  61. void spectre_tiling_generate(
  62. const struct SpectrePatchParams *params, int w, int h,
  63. spectre_tile_callback_fn cb, void *cbctx);
  64. #endif