summaryrefslogtreecommitdiff
path: root/apps/plugins/puzzles/src/grid.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/puzzles/src/grid.c')
-rw-r--r--apps/plugins/puzzles/src/grid.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/apps/plugins/puzzles/src/grid.c b/apps/plugins/puzzles/src/grid.c
index 52648e5a92..b5e6bb0937 100644
--- a/apps/plugins/puzzles/src/grid.c
+++ b/apps/plugins/puzzles/src/grid.c
@@ -2060,6 +2060,102 @@ static grid *grid_new_greathexagonal(int width, int height, const char *desc)
2060 return g; 2060 return g;
2061} 2061}
2062 2062
2063#define KAGOME_TILESIZE 18
2064/* Vector for side of triangle - ratio is close to sqrt(3) */
2065#define KAGOME_A 15
2066#define KAGOME_B 26
2067
2068static void grid_size_kagome(int width, int height,
2069 int *tilesize, int *xextent, int *yextent)
2070{
2071 int a = KAGOME_A;
2072 int b = KAGOME_B;
2073
2074 *tilesize = KAGOME_TILESIZE;
2075 *xextent = (4*a) * (width-1) + 6*a;
2076 *yextent = (2*b) * (height-1) + 2*b;
2077}
2078
2079static grid *grid_new_kagome(int width, int height, const char *desc)
2080{
2081 int x, y;
2082 int a = KAGOME_A;
2083 int b = KAGOME_B;
2084
2085 /* Upper bounds - don't have to be exact */
2086 int max_faces = 6 * (width + 1) * (height + 1);
2087 int max_dots = 6 * width * height;
2088
2089 tree234 *points;
2090
2091 grid *g = grid_empty();
2092 g->tilesize = KAGOME_TILESIZE;
2093 g->faces = snewn(max_faces, grid_face);
2094 g->dots = snewn(max_dots, grid_dot);
2095
2096 points = newtree234(grid_point_cmp_fn);
2097
2098 for (y = 0; y < height; y++) {
2099 for (x = 0; x < width; x++) {
2100 grid_dot *d;
2101 /* centre of hexagon */
2102 int px = (4*a) * x;
2103 int py = (2*b) * y;
2104 if (y % 2)
2105 px += 2*a;
2106
2107 /* hexagon */
2108 grid_face_add_new(g, 6);
2109 d = grid_get_dot(g, points, px + a, py - b); grid_face_set_dot(g, d, 0);
2110 d = grid_get_dot(g, points, px + 2*a, py ); grid_face_set_dot(g, d, 1);
2111 d = grid_get_dot(g, points, px + a, py + b); grid_face_set_dot(g, d, 2);
2112 d = grid_get_dot(g, points, px - a, py + b); grid_face_set_dot(g, d, 3);
2113 d = grid_get_dot(g, points, px - 2*a, py ); grid_face_set_dot(g, d, 4);
2114 d = grid_get_dot(g, points, px - a, py - b); grid_face_set_dot(g, d, 5);
2115
2116 /* Triangle above right */
2117 if ((x < width - 1) || (!(y % 2) && y)) {
2118 grid_face_add_new(g, 3);
2119 d = grid_get_dot(g, points, px + 3*a, py - b); grid_face_set_dot(g, d, 0);
2120 d = grid_get_dot(g, points, px + 2*a, py ); grid_face_set_dot(g, d, 1);
2121 d = grid_get_dot(g, points, px + a, py - b); grid_face_set_dot(g, d, 2);
2122 }
2123
2124 /* Triangle below right */
2125 if ((x < width - 1) || (!(y % 2) && (y < height - 1))) {
2126 grid_face_add_new(g, 3);
2127 d = grid_get_dot(g, points, px + 3*a, py + b); grid_face_set_dot(g, d, 0);
2128 d = grid_get_dot(g, points, px + a, py + b); grid_face_set_dot(g, d, 1);
2129 d = grid_get_dot(g, points, px + 2*a, py ); grid_face_set_dot(g, d, 2);
2130 }
2131
2132 /* Left triangles */
2133 if (!x && (y % 2)) {
2134 /* Triangle above left */
2135 grid_face_add_new(g, 3);
2136 d = grid_get_dot(g, points, px - a, py - b); grid_face_set_dot(g, d, 0);
2137 d = grid_get_dot(g, points, px - 2*a, py ); grid_face_set_dot(g, d, 1);
2138 d = grid_get_dot(g, points, px - 3*a, py - b); grid_face_set_dot(g, d, 2);
2139
2140 /* Triangle below left */
2141 if (y < height - 1) {
2142 grid_face_add_new(g, 3);
2143 d = grid_get_dot(g, points, px - a, py + b); grid_face_set_dot(g, d, 0);
2144 d = grid_get_dot(g, points, px - 3*a, py + b); grid_face_set_dot(g, d, 1);
2145 d = grid_get_dot(g, points, px - 2*a, py ); grid_face_set_dot(g, d, 2);
2146 }
2147 }
2148 }
2149 }
2150
2151 freetree234(points);
2152 assert(g->num_faces <= max_faces);
2153 assert(g->num_dots <= max_dots);
2154
2155 grid_make_consistent(g);
2156 return g;
2157}
2158
2063#define OCTAGONAL_TILESIZE 40 2159#define OCTAGONAL_TILESIZE 40
2064/* b/a approx sqrt(2) */ 2160/* b/a approx sqrt(2) */
2065#define OCTAGONAL_A 29 2161#define OCTAGONAL_A 29