8 static int inversecantor(int cantor, int *x, int *y);
11 * This file is written by Nathan Wagner and dedicated to the public
15 double HL_vertexv[12] = {
16 .577350269189625764509148780502, 0.0,
17 .288675134594812882254574390251, 0.5,
18 -.288675134594812882254574390251, 0.5,
19 -.577350269189625764509148780502, 0.0,
20 -.288675134594812882254574390251, -0.5,
21 .288675134594812882254574390251, -0.5};
23 double HL_fand[16] = {
25 .577350269189625764509148780502, 0.0,
26 .288675134594812882254574390251, 0.5,
27 -.288675134594812882254574390251, 0.5,
28 -.577350269189625764509148780502, 0.0,
29 -.288675134594812882254574390251, -0.5,
30 .288675134594812882254574390251, -0.5,
31 .577350269189625764509148780502, 0.0
34 double HL_hfand[16] = {
36 0.0, .577350269189625764509148780502,
37 0.5, .288675134594812882254574390251,
38 0.5, -.288675134594812882254574390251,
39 0.0, -.577350269189625764509148780502,
40 -0.5, -.288675134594812882254574390251,
41 -0.5, .288675134594812882254574390251,
42 0.0, .577350269189625764509148780502
47 .577350269189625764509148780502f, 0.0f,
48 .288675134594812882254574390251f, 0.5f,
49 -.288675134594812882254574390251f, 0.5f,
50 -.577350269189625764509148780502f, 0.0f,
51 -.288675134594812882254574390251f, -0.5f,
52 .288675134594812882254574390251f, -0.5f,
53 .577350269189625764509148780502f, 0.0f
56 float HL_hfanf[16] = {
58 0.0f, .577350269189625764509148780502f,
59 0.5f, .288675134594812882254574390251f,
60 0.5f, -.288675134594812882254574390251f,
61 0.0f, -.577350269189625764509148780502f,
62 -0.5f, -.288675134594812882254574390251f,
63 -0.5f, .288675134594812882254574390251f,
64 0.0f, .577350269189625764509148780502f
67 /* size of a square that will exactly fit in a hexagon */
68 /* 2.0/(1+sqrt(3.0)) */
69 double HL_square = .73205080756887729352;
71 /* these all are for a hex one unit across */
72 static double hexptvd[6][2] = {
73 {.577350269189625764509148780502, 0.0}, /* 1.0/sqrt3 */
74 {.288675134594812882254574390251, 0.5}, /* 0.5/sqrt3 */
75 {-.288675134594812882254574390251, 0.5},
76 {-.577350269189625764509148780502, 0.0},
77 {-.288675134594812882254574390251, -0.5},
78 {.288675134594812882254574390251, -0.5}
83 /* TODO how is this related? to the above? */
84 static double texptvd[6][2] = {
85 {1.154700538379251529018297561004, 0.5}, /* 2.0/sqrt3 */
86 {.866025403784438646763723170753, 1.0}, /* 1.5/sqrt3 */
87 {.288675134594812882254574390251, 1.0},
89 {.288675134594812882254574390251, 0.0},
90 {.866025403784438646763723170753, 0.0}
93 static double hexpthd[6][2] = {
94 {0.0, .577350269189625764509148780502},
95 {0.5, .288675134594812882254574390251},
96 {0.5, -.288675134594812882254574390251},
97 {0.0, -.577350269189625764509148780502},
98 {-0.5, -.288675134594812882254574390251},
99 {-0.5, .288675134594812882254574390251}
104 void HL_vertices(int cantor, double *vc) {
108 HL_hexcenter(cantor, &xc, &yc);
110 for (i=0; i<6; i++) {
111 *vc++ = hexptvd[i][0] + xc;
112 *vc++ = hexptvd[i][1] + yc;
114 *vc++ = hexptvd[0][0] + xc;
115 *vc++ = hexptvd[0][1] + yc;
118 void HL_trianglefan(int cantor, double *vc) {
119 HL_hexcenter(cantor, vc, vc+1);
120 HL_vertices(cantor, vc+2);
123 double HL_center_x(int cantor) {
126 HL_hexcenter(cantor, &x, 0);
130 double HL_center_y(int cantor) {
133 HL_hexcenter(cantor, 0, &y);
137 int HL_hexcenter(int cantor, double *xc, double *yc) {
139 double stride = 1.5/sqrt(3.0);
141 inversecantor(cantor, &x, &y);
143 if (xc) *xc = x * stride;
144 if (yc && x >= 0) *yc = y + ((x + 1) % 2) / 2.0 - 0.5;
145 if (yc && x < 0) *yc = y + ((-x + 1) % 2) / 2.0 - 0.5;
151 * This function assumes that the hexes are one unit across, and vertically
152 * oriented. If that is not the case, you will need to transform
153 * your input coordinates first.
155 int HL_cantor_bin(double x, double y) {
156 return HL_hexbin(1.0, x, y, 0, 0);
159 static int xy2ijk(int x, int y, int *i, int *j, int *k) {
165 pj = pj + (-x + 1) / 2;
175 return HL_cantor_xy(x,y);
178 static int ijk2xy(int i, int j, int k, int *x, int *y) {
195 return HL_cantor_xy(px,py);
198 int HL_cantor_ijk(int i, int j, int k) {
199 return ijk2xy(i,j,k,0,0);
202 int HL_distance(int from, int to) {
206 HL_cantor_arrays(from, 0, fc);
207 HL_cantor_arrays(to, 0, tc);
210 dist += abs(fc[i] - tc[i]);
216 int HL_hexes_within_range(int hex, int range, int *list, int size) {
221 return HL_hexes_at_range(hex, 0, list, size);
224 for (i=1;i<=range;i++) {
225 count += HL_hexes_at_range(hex, i, count > size ? 0 : list+count, size-count);
230 int HL_hexes_at_range(int hex, int range, int *list, int size) {
231 int q; /* p and q are count/loop vars */
232 int c[3]; /* ijk coord array */
239 } else if (range < 0) {
243 /* TODO don't bother to collect if the list isn't big enough? */
244 /* i.e. if (!list || size < range * 6) */
245 if (!list || size < 1) return range * 6;
247 HL_cantor_arrays(hex, 0, c);
250 hex = HL_cantor_ijkp(c);
252 for(q=0; q<size && q < range * 6; q++) {
254 hex = HL_adjhex(hex, q/range+2);
260 int HL_adjacent_hex(int start, int dir) {
261 if (dir < 0 || dir > 5) return 0;
263 return HL_adjhex(start, dir);
266 /* direction 0 is positive X , counter clockwise from there */
267 int HL_adjhex(int start, int dir) {
270 HL_cantor_arrays(start, 0, c);
274 c[0]--; c[1]++; break;
276 c[1]++; c[2]--; break;
278 c[0]++; c[2]--; break;
280 c[0]++; c[1]--; break;
282 c[1]--; c[2]++; break;
284 c[0]--; ; c[2]++; break;
287 return HL_cantor_ijkp(c);
290 int HL_cantor_xyp(int *xy) {
291 return HL_cantor_xy(xy[0], xy[1]);
294 int HL_cantor_ijkp(int *ijk) {
295 return HL_cantor_ijk(ijk[0], ijk[1], ijk[2]);
298 int HL_cantor_arrays(int can, int *xy, int *ijk) {
299 return HL_cantor_decode(can, xy, xy ? xy+1 : 0,
300 ijk, ijk ? ijk+1 : 0, ijk ? ijk+2 : 0);
303 int HL_cantor_decode(int can, int *x, int *y, int *i, int *j, int *k) {
306 inversecantor(can, &px, &py);
310 xy2ijk(px, py, i, j, k);
315 int HL_cantor_i(int cantor) {
318 HL_cantor_decode(cantor, 0,0, &i,0,0);
322 int HL_cantor_j(int cantor) {
325 HL_cantor_decode(cantor, 0,0, 0,&j,0);
329 int HL_cantor_k(int cantor) {
332 HL_cantor_decode(cantor, 0,0, 0,0,&k);
336 int HL_cantor_x(int cantor) {
338 inversecantor(cantor, &x, 0);
342 int HL_cantor_y(int cantor) {
344 inversecantor(cantor, 0, &y);
348 /* Determine if a map with these dimensions will overflow */
349 int HL_map_bounds_ok(int xdim, int ydim) {
351 /* return (x+y) * (x + y + 1) / 2 + y+1; */
353 if (INT_MAX - xdim - 1 < ydim) return 0;
354 if (INT_MAX / (xdim+ydim) < (xdim+ydim+1)) return 0;
355 if ( (xdim+ydim) * (xdim+ydim+1) / 2 > INT_MAX - ydim - 1)
361 int HL_map_max_dimension(void) {
364 low = 1; high = INT_MAX/2;
366 while (low != high - 1) {
367 try = (low + high) / 2;
368 if (HL_map_bounds_ok(try,try)) {
378 static int inversenatcantor(int cantor, int *x, int *y) {
383 w = (int)floor((sqrt(8.0 * cantor + 1.0) - 1.0)/2.0);
395 * map non negative integer pairs to their cantor pairing function
396 * number, plus one. We add one so that the result is never zero,
397 * leaving zero to be "invalid" or "none" or what have you.
400 static int natcantor(int x, int y) {
401 return (x+y) * (x + y + 1) / 2 + y+1;
404 /* See http://en.wikipedia.org/wiki/Cantor_pairing_function */
405 /* see also http://szudzik.com/ElegantPairing.pdf */
407 * if either coordinate is negative, map the integers onto the
408 * whole numbers, and then return the negative of the adjusted
409 * cantor number. As for most grids negative coordinates will
410 * be invalid, this will allow for a <= 0 test for invalid
411 * or out of bounds (on the negative side anyway, you'll
412 * still have to test for out of range on the positive side).
414 * TODO figure out what the maximum supported coordinates are
415 * for given integer sizes.
417 int HL_cantor_xy(int x, int y) {
418 if (x < 0 || y < 0) {
419 x = abs(2 * x) - (x < 0);
420 y = abs(2 * y) - (y < 0);
421 return -natcantor(x, y);
423 return natcantor(x,y);
426 static int inversecantor(int cantor, int *x, int *y) {
428 inversenatcantor(-cantor, x, y);
444 inversenatcantor(cantor, x, y);
455 /* y *must* be positive down as the xy /iso conversion assumes this */
456 static int hex_xy(struct hex *h) {
457 if (!h->iso) return 1;
459 h->y = -h->y - (h->x+1)/2;
461 /* need to round toward -inf, not toward zero, so x-1 */
462 h->y = -h->y - h->x/2;
471 static int hex_iso(struct hex *h) {
472 if (h->iso) return 1;
475 h->y = (-h->y - (h->x+1)/2);
477 /* need to round toward -inf, not toward zero, so x-1 */
478 h->y = (-h->y - (h->x)/2);
488 #define COS30 (.866025403784438646763723170752)
490 int HL_hexbin(double width, double x, double y, int *i, int *j) {
491 double z, rx, ry, rz;
492 double abs_dx, abs_dy, abs_dz;
496 /*x = x / cos(30 * M_PI / 180.0); */ /* rotated X coord */
498 y = y - x / 2.0; /* adjustment for rotated X */
500 /* adjust for actual hexwidth */
506 ix = rx = floor(x + 0.5);
507 iy = ry = floor(y + 0.5);
508 iz = rz = floor(z + 0.5);
513 abs_dx = fabs(rx - x);
514 abs_dy = fabs(ry - y);
515 abs_dz = fabs(rz - z);
517 if (abs_dx >= abs_dy && abs_dx >= abs_dz) {
519 } else if (abs_dy >= abs_dx && abs_dy >= abs_dz) {
533 return HL_cantor_xy(h.x, h.y);