diff options
| author | Simon Tatham <anakin@pobox.com> | 2008-09-06 15:19:47 +0000 |
|---|---|---|
| committer | Simon Tatham <anakin@pobox.com> | 2008-09-06 15:19:47 +0000 |
| commit | f38b711c73174786b1dbf8878fb0cb132a89794d (patch) | |
| tree | 9290783571144d5e3d4380f586147e931ba8b211 /grid.h | |
| parent | a7431c0b7ce232f296ebcd70172ca64e58300105 (diff) | |
| download | puzzles-f38b711c73174786b1dbf8878fb0cb132a89794d.zip puzzles-f38b711c73174786b1dbf8878fb0cb132a89794d.tar.gz puzzles-f38b711c73174786b1dbf8878fb0cb132a89794d.tar.bz2 puzzles-f38b711c73174786b1dbf8878fb0cb132a89794d.tar.xz | |
Completely re-engineered version of Loopy, courtesy of Lambros
Lambrou. Now capable of handling triangular and hexagonal grids as
well as square ones, and then a number of semiregular plane tilings
and duals of semiregular ones. In fact, most of the solver code
supports an _arbitrary_ planar graph (well, provided both the graph
and its dual have no self-edges), so it could easily be extended
further with only a little more effort.
[originally from svn r8162]
Diffstat (limited to 'grid.h')
| -rw-r--r-- | grid.h | 96 |
1 files changed, 96 insertions, 0 deletions
@@ -0,0 +1,96 @@ +/* + * (c) Lambros Lambrou 2008 + * + * Code for working with general grids, which can be any planar graph + * with faces, edges and vertices (dots). Includes generators for a few + * types of grid, including square, hexagonal, triangular and others. + */ + +#ifndef PUZZLES_GRID_H +#define PUZZLES_GRID_H + +/* Useful macros */ +#ifndef SQ +# define SQ(x) ( (x) * (x) ) +#endif + +/* ---------------------------------------------------------------------- + * Grid structures: + * A grid is made up of faces, edges and dots. These structures hold + * the incidence relationships between these types. For example, an + * edge always joins two dots, and is adjacent to two faces. + * The "grid_xxx **" members are lists of pointers which are dynamically + * allocated during grid generation. + * A pointer to a face/edge/dot will always point somewhere inside one of the + * three lists of the main "grid" structure: faces, edges, dots. + * Could have used integer offsets into these lists, but using actual + * pointers instead gives us type-safety. + */ + +/* Need forward declarations */ +typedef struct grid_face grid_face; +typedef struct grid_edge grid_edge; +typedef struct grid_dot grid_dot; + +struct grid_face { + int order; /* Number of edges, also the number of dots */ + grid_edge **edges; /* edges around this face */ + grid_dot **dots; /* corners of this face */ +}; +struct grid_edge { + grid_dot *dot1, *dot2; + grid_face *face1, *face2; /* Use NULL for the infinite outside face */ +}; +struct grid_dot { + int order; + grid_edge **edges; + grid_face **faces; /* A NULL grid_face* means infinite outside face */ + + /* Position in some fairly arbitrary (Cartesian) coordinate system. + * Use large enough values such that we can get away with + * integer arithmetic, but small enough such that arithmetic + * won't overflow. */ + int x, y; +}; +typedef struct grid { + /* These are (dynamically allocated) arrays of all the + * faces, edges, dots that are in the grid. */ + int num_faces; grid_face *faces; + int num_edges; grid_edge *edges; + int num_dots; grid_dot *dots; + + /* Should be a face roughly near the middle of the grid. + * Used to seed path-generation, and also for nearest-edge + * detection. */ + grid_face *middle_face; + + /* Cache the bounding-box of the grid, so the drawing-code can quickly + * figure out the proper scaling to draw onto a given area. */ + int lowest_x, lowest_y, highest_x, highest_y; + + /* A measure of tile size for this grid (in grid coordinates), to help + * the renderer decide how large to draw the grid. + * Roughly the size of a single tile - for example the side-length + * of a square cell. */ + int tilesize; + + /* We really don't want to copy this monstrosity! + * A grid is immutable once generated. + */ + int refcount; +} grid; + +grid *grid_new_square(int width, int height); +grid *grid_new_honeycomb(int width, int height); +grid *grid_new_triangular(int width, int height); +grid *grid_new_snubsquare(int width, int height); +grid *grid_new_cairo(int width, int height); +grid *grid_new_greathexagonal(int width, int height); +grid *grid_new_octagonal(int width, int height); +grid *grid_new_kites(int width, int height); + +void grid_free(grid *g); + +grid_edge *grid_nearest_edge(grid *g, int x, int y); + +#endif /* PUZZLES_GRID_H */ |