-
Notifications
You must be signed in to change notification settings - Fork 3
/
truchet_hex.c
348 lines (279 loc) · 8.76 KB
/
truchet_hex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* truchet_hex --- draw hexagonal Truchet tiles 2020-09-16 */
/* Copyright (c) 2020 John Honniball, Froods Software Development */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "hpgllib.h"
#define DEG_TO_RAD (M_PI / 180.0)
#define NSIDES (6) // Hexagons
struct XY {
double x;
double y;
};
struct HexRec {
struct XY face[NSIDES];
struct XY vert[NSIDES];
struct XY apex[NSIDES];
};
void drawHexagon(const double x, const double y, const struct HexRec *const hex, const int first, const int last);
void drawTile(const double x, const double y, const struct HexRec *const hex, const int tile_type);
void drawTileB(const double x, const double y, const struct HexRec *const hex, const int i);
void drawTileC(const double x, const double y, const struct HexRec *const hex, const int i);
void drawTileD(const double x, const double y, const struct HexRec *const hex, const int i);
void drawTileE(const double x, const double y, const struct HexRec *const hex, const int i);
void lineFace(const double x, const double y, const struct HexRec *const hex, const int f);
void arcApex(const double x, const double y, const struct HexRec *const hex, const int a);
void arcVertex(const double x, const double y, const struct HexRec *const hex, const int v);
int main(const int argc, char * const argv[])
{
/* Inspired by a tweet on #plottertwitter */
int opt;
int i, j;
int ngridx = 12;
int ngridy = 12;
int tile_type;
int grid[32][32];
double x, y;
double xc;
double maxx, maxy;
double height;
double gridw, gridh;
double r;
double rowht;
double gridx0;
const double delta = (2.0 * M_PI) / (double)NSIDES;
struct HexRec hex;
while ((opt = getopt(argc, argv, "no:p:s:t:v:")) != -1) {
switch (opt) {
case 's':
if (strchr(optarg, '1')) {
ngridx = ngridy = 24;
}
else if (strchr(optarg, '2')) {
ngridx = ngridy = 16;
}
else if (strchr(optarg, '3')) {
ngridx = ngridy = 12;
}
else if (strchr(optarg, '4')) {
ngridx = ngridy = 8;
}
else if (strchr(optarg, '5')) {
ngridx = ngridy = 6;
}
else if (strchr(optarg, '6')) {
ngridx = ngridy = 4;
}
case 'n':
case 'o':
case 'p':
case 't':
case 'v':
plotopt(opt, optarg);
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-p pen] [-s <size>] [-t title]\n", argv[0]);
fprintf(stderr, " <size> ::= A1 | A2 | A3 | A4 | A5\n");
exit(EXIT_FAILURE);
}
}
srand((unsigned int)time(NULL));
if (plotbegin(1) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
getplotsize(&maxx, &maxy);
xc = maxx / 2.0;
height = maxy;
gridx0 = xc - (height / 2.0);
gridw = maxy / ngridx;
gridh = maxy / ngridy;
r = gridw / 2.0;
rowht = 2.0 * r * sin(delta);
/* Generate the base hexagon */
for (i = 0; i < NSIDES; i++) {
const double theta = delta * (double)i;
hex.face[i].x = r * cos(theta);
hex.face[i].y = r * sin(theta);
hex.vert[i].x = r * cos(theta + (delta / 2.0)) / sin(delta);
hex.vert[i].y = r * sin(theta + (delta / 2.0)) / sin(delta);
hex.apex[i].x = r * cos(theta) * 2.0;
hex.apex[i].y = r * sin(theta) * 2.0;
}
/* Generate the grid */
for (i = 0; i < ngridy; i++) {
for (j = 0; j < ngridx; j++) {
// tile_type = (i + j) % 15;
tile_type = rand() / (RAND_MAX / 15);
grid[i][j] = tile_type;
}
}
pencolr(1);
/* Draw the hexagons */
for (i = 0; i < ngridy; i++) {
y = rowht + (i * rowht);
if (i & 1) {
for (j = 0; j < (ngridx - 1) ; j++) {
x = r + gridx0 + (j * r * 2.0);
// circle(x, y, r);
if (j == (ngridx - 2))
drawHexagon(x, y, &hex, 0, 6);
else
drawHexagon(x, y, &hex, 0, 5);
}
}
else {
for (j = 0; j < ngridx; j++) {
x = gridx0 + (j * r * 2.0);
// circle(x, y, r);
if (i == 0) {
if (j == 0)
drawHexagon(x, y, &hex, 1, 5);
else if (j == (ngridx - 1))
drawHexagon(x, y, &hex, 2, 7);
else
drawHexagon(x, y, &hex, 2, 5);
}
else {
if (j == 0)
drawHexagon(x, y, &hex, 1, 4);
else if (j == (ngridx - 1)) {
drawHexagon(x, y, &hex, 2, 3);
drawHexagon(x, y, &hex, 4, 7);
}
else
drawHexagon(x, y, &hex, 2, 3);
}
}
}
}
pencolr(0);
/* Draw the main grid */
for (i = 0; i < ngridy; i++) {
y = rowht + (i * rowht);
if (i & 1)
for (j = 0; j < (ngridx - 1) ; j++) {
x = r + gridx0 + (j * r * 2.0);
drawTile(x, y, &hex, grid[i][j]);
}
else
for (j = 0; j < ngridx; j++) {
x = gridx0 + (j * r * 2.0);
drawTile(x, y, &hex, grid[i][j]);
}
}
plotend();
return (0);
}
/* drawHexagon --- draw the faces of the base hexagon */
void drawHexagon(const double x, const double y, const struct HexRec *const hex, const int first, const int last)
{
int i;
moveto(x + hex->vert[first].x, y + hex->vert[first].y);
for (i = first + 1; i < last + 1; i++)
lineto(x + hex->vert[i % NSIDES].x, y + hex->vert[i % NSIDES].y);
}
/* drawTile --- draw a single tile at (x, y) */
void drawTile(const double x, const double y, const struct HexRec *const hex, const int tile_type)
{
/* See: https://en.wikipedia.org/wiki/Serpentiles */
switch (tile_type) {
case 0: /* Tile A: only one kind (Serpentile 300) */
lineFace(x, y, hex, 0);
lineFace(x, y, hex, 1);
lineFace(x, y, hex, 2);
break;
case 1: /* Tile B: three orientations (Serpentile 102) */
drawTileB(x, y, hex, 0);
break;
case 2:
drawTileB(x, y, hex, 1);
break;
case 3:
drawTileB(x, y, hex, 2);
break;
case 4: /* Tile C: three orientations (Serpentile 120) */
drawTileC(x, y, hex, 0);
break;
case 5:
drawTileC(x, y, hex, 1);
break;
case 6:
drawTileC(x, y, hex, 2);
break;
case 7: /* Tile D: two orientations (Serpentile 003) */
drawTileD(x, y, hex, 0);
break;
case 8:
drawTileD(x, y, hex, 1);
break;
case 9: /* Tile E: six orientations (Serpentile 021) */
drawTileE(x, y, hex, 0);
break;
case 10:
drawTileE(x, y, hex, 1);
break;
case 11:
drawTileE(x, y, hex, 2);
break;
case 12:
drawTileE(x, y, hex, 3);
break;
case 13:
drawTileE(x, y, hex, 4);
break;
case 14:
drawTileE(x, y, hex, 5);
break;
}
}
void drawTileB(const double x, const double y, const struct HexRec *const hex, const int i)
{
lineFace(x, y, hex, i);
arcApex(x, y, hex, i);
arcApex(x, y, hex, i + 3);
}
void drawTileC(const double x, const double y, const struct HexRec *const hex, const int i)
{
lineFace(x, y, hex, i);
arcVertex(x, y, hex, i + 1);
arcVertex(x, y, hex, i + 4);
}
void drawTileD(const double x, const double y, const struct HexRec *const hex, const int i)
{
arcVertex(x, y, hex, i + 1);
arcVertex(x, y, hex, i + 3);
arcVertex(x, y, hex, i + 5);
}
void drawTileE(const double x, const double y, const struct HexRec *const hex, const int i)
{
arcVertex(x, y, hex, i);
arcApex(x, y, hex, i + 3);
arcApex(x, y, hex, i + 4);
}
/* lineFace --- draw a straight line from face f to the opposite face */
void lineFace(const double x, const double y, const struct HexRec *const hex, const int f)
{
const int f1 = f % NSIDES;
const int f2 = (f + 3) % NSIDES;
moveto(x + hex->face[f1].x, y + hex->face[f1].y);
lineto(x + hex->face[f2].x, y + hex->face[f2].y);
}
/* arcApex --- draw an arc centred on apex a, anticlockwise */
void arcApex(const double x, const double y, const struct HexRec *const hex, const int a)
{
const int f = (a + 1) % NSIDES;
moveto(x + hex->face[f].x, y + hex->face[f].y);
arc(x + hex->apex[a % NSIDES].x, y + hex->apex[a % NSIDES].y, 60.0);
}
/* arcVertex --- draw an arc centred on vertex v, anticlockwise */
void arcVertex(const double x, const double y, const struct HexRec *const hex, const int v)
{
const int f = (v + 1) % NSIDES;
moveto(x + hex->face[f].x, y + hex->face[f].y);
arc(x + hex->vert[v % NSIDES].x, y + hex->vert[v % NSIDES].y, 120.0);
}