From 09aa8de52cb962f1ceebfb1fd44f2c54a924fc5c Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Mon, 22 Jul 2024 21:43:25 -0400 Subject: puzzles: resync with upstream This brings the puzzles source in sync with Simon's branch, commit fd304c5 (from March 2024), with some added Rockbox-specific compatibility changes: https://www.franklinwei.com/git/puzzles/commit/?h=rockbox-devel&id=516830d9d76bdfe64fe5ccf2a9b59c33f5c7c078 There are quite a lot of backend changes, including a new "Mosaic" puzzle. In addition, some new frontend changes were necessary: - New "Preferences" menu to access the user preferences system. - Enabled spacebar input for several games. Change-Id: I94c7df674089c92f32d5f07025f6a1059068af1e --- apps/plugins/puzzles/src/penrose.h | 119 +++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 46 deletions(-) (limited to 'apps/plugins/puzzles/src/penrose.h') diff --git a/apps/plugins/puzzles/src/penrose.h b/apps/plugins/puzzles/src/penrose.h index ba5ae16f2c..e6e927d618 100644 --- a/apps/plugins/puzzles/src/penrose.h +++ b/apps/plugins/puzzles/src/penrose.h @@ -1,59 +1,86 @@ -/* penrose.h - * - * Penrose tiling functions. - * - * Provides an interface with which to generate Penrose tilings - * by recursive subdivision of an initial tile of choice (one of the - * four sets of two pairs kite/dart, or thin/thick rhombus). - * - * You supply a callback function and a context pointer, which is - * called with each tile in turn: you choose how many times to recurse. - */ +#ifndef PUZZLES_PENROSE_H +#define PUZZLES_PENROSE_H -#ifndef _PENROSE_H -#define _PENROSE_H +struct PenrosePatchParams { + /* + * A patch of Penrose tiling is identified by giving + * + * - the coordinates of the starting triangle, using a + * combinatorial coordinate system + * + * - which vertex of that triangle is at the centre point of the + * tiling + * + * - the orientation of the triangle's base edge, as a number + * from 0 to 9, measured in tenths of a turn + * + * Coordinates are a sequence of letters. For a P2 tiling all + * letters are from the set {A,B,U,V}; for P3, {C,D,X,Y}. + */ + unsigned start_vertex; + int orientation; + size_t ncoords; + char *coords; +}; -#ifndef PHI -#define PHI 1.6180339887 +#ifndef PENROSE_ENUM_DEFINED +#define PENROSE_ENUM_DEFINED +enum { PENROSE_P2, PENROSE_P3 }; #endif -typedef struct vector vector; - -double v_x(vector *vs, int i); -double v_y(vector *vs, int i); +bool penrose_valid_letter(char c, int which); -typedef struct penrose_state penrose_state; - -/* Return non-zero to clip the tree here (i.e. not recurse - * below this tile). +/* + * Fill in PenrosePatchParams with a randomly selected set of + * coordinates, in enough detail to generate a patch of tiling filling + * a w x h area. + * + * Units of measurement: the tiling will be oriented such that + * horizontal tile edges are possible (and therefore vertical ones are + * not). Therefore, all x-coordinates will be rational linear + * combinations of 1 and sqrt(5), and all y-coordinates will be + * sin(pi/5) times such a rational linear combination. By scaling up + * appropriately we can turn those rational combinations into + * _integer_ combinations, so we do. Therefore, w is measured in units + * of 1/4, and h is measured in units of sin(pi/5)/2, on a scale where + * a length of 1 corresponds to the legs of the acute isosceles + * triangles in the tiling (hence, the long edges of P2 kites and + * darts, or all edges of P3 rhombs). * - * Parameters are state, vector array, npoints, depth. - * ctx is inside state. + * (In case it's useful, the y scale factor sin(pi/5)/2 is an + * algebraic number. Its minimal polynomial is 256x^4 - 80x^2 + 5.) + * + * The 'coords' field of the structure will be filled in with a new + * dynamically allocated array. Any previous pointer in that field + * will be overwritten. */ -typedef int (*tile_callback)(penrose_state *, vector *, int, int); - -struct penrose_state { - int start_size; /* initial side length */ - int max_depth; /* Recursion depth */ - - tile_callback new_tile; - void *ctx; /* for callback */ -}; +void penrose_tiling_randomise(struct PenrosePatchParams *params, int which, + int w, int h, random_state *rs); -enum { PENROSE_P2, PENROSE_P3 }; - -extern int penrose(penrose_state *state, int which, int angle); +/* + * Validate a PenrosePatchParams to ensure it contains no illegal + * coordinates. Returns NULL if it's acceptable, or an error string if + * not. + */ +const char *penrose_tiling_params_invalid( + const struct PenrosePatchParams *params, int which); -/* Returns the side-length of a penrose tile at recursion level - * gen, given a starting side length. */ -extern double penrose_side_length(double start_size, int depth); +/* + * Generate the actual set of Penrose tiles from a PenrosePatchParams, + * passing each one to a callback. The callback receives the vertices + * of each point, in the form of an array of 4*4 integers. Each vertex + * is represented by four consecutive integers in this array, with the + * first two giving the x coordinate and the last two the y + * coordinate. Each pair of integers a,b represent a single coordinate + * whose value is a + b*sqrt(5). The units of measurement for x and y + * are as described above. + */ +typedef void (*penrose_tile_callback_fn)(void *ctx, const int *coords); -/* Returns the count of each type of tile at a given recursion depth. */ -extern void penrose_count_tiles(int gen, int *nlarge, int *nsmall); +#define PENROSE_NVERTICES 4 -/* Calculate start size and recursion depth required to produce a - * width-by-height sized patch of penrose tiles with the given tilesize */ -extern void penrose_calculate_size(int which, int tilesize, int w, int h, - double *required_radius, int *start_size, int *depth); +void penrose_tiling_generate( + const struct PenrosePatchParams *params, int w, int h, + penrose_tile_callback_fn cb, void *cbctx); #endif -- cgit v1.2.3