hat.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef PUZZLES_HAT_H
  2. #define PUZZLES_HAT_H
  3. struct HatPatchParams {
  4. /*
  5. * A patch of hat tiling is identified by giving the coordinates
  6. * of the kite in one corner, using a multi-level coordinate
  7. * system based on metatile expansions. Coordinates are a sequence
  8. * of small non-negative integers. The valid range for each
  9. * coordinate depends on the next coordinate, or on final_metatile
  10. * if it's the last one in the list. The largest valid range is
  11. * {0,...,12}.
  12. *
  13. * 'final_metatile' is one of the characters 'H', 'T', 'P' or 'F'.
  14. */
  15. size_t ncoords;
  16. unsigned char *coords;
  17. char final_metatile;
  18. };
  19. /*
  20. * Fill in HatPatchParams with a randomly selected set of coordinates,
  21. * in enough detail to generate a patch of tiling covering an area of
  22. * w x h 'squares' of a kite tiling.
  23. *
  24. * The kites grid is considered to be oriented so that it includes
  25. * horizontal lines and not vertical ones. So each of the smallest
  26. * equilateral triangles in the grid has a bounding rectangle whose
  27. * height is sqrt(3)/2 times its width, and either the top or the
  28. * bottom of that bounding rectangle is the horizontal edge of the
  29. * triangle. A 'square' of a kite tiling (for convenience of choosing
  30. * grid dimensions) counts as one of those bounding rectangles.
  31. *
  32. * The 'coords' field of the structure will be filled in with a new
  33. * dynamically allocated array. Any previous pointer in that field
  34. * will be overwritten.
  35. */
  36. void hat_tiling_randomise(struct HatPatchParams *params, int w, int h,
  37. random_state *rs);
  38. /*
  39. * Validate a HatPatchParams to ensure it contains no illegal
  40. * coordinates. Returns NULL if it's acceptable, or an error string if
  41. * not.
  42. */
  43. const char *hat_tiling_params_invalid(const struct HatPatchParams *params);
  44. /*
  45. * Generate the actual set of hat tiles from a HatPatchParams, passing
  46. * each one to a callback. The callback receives the vertices of each
  47. * point, as a sequence of 2*nvertices integers, with x,y coordinates
  48. * interleaved.
  49. *
  50. * The x coordinates are measured in units of 1/4 of the side length
  51. * of the smallest equilateral triangle, or equivalently, 1/2 the
  52. * length of one of the long edges of a single kite. The y coordinates
  53. * are measured in units of 1/6 the height of the triangle, which is
  54. * also 1/2 the length of the short edge of a kite. Therefore, you can
  55. * expect x to go up to 4*w and y up to 6*h.
  56. */
  57. typedef void (*hat_tile_callback_fn)(void *ctx, size_t nvertices,
  58. int *coords);
  59. void hat_tiling_generate(const struct HatPatchParams *params, int w, int h,
  60. hat_tile_callback_fn cb, void *cbctx);
  61. #endif