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
36 .577350269189625764509148780502f, 0.0f,
37 .288675134594812882254574390251f, 0.5f,
38 -.288675134594812882254574390251f, 0.5f,
39 -.577350269189625764509148780502f, 0.0f,
40 -.288675134594812882254574390251f, -0.5f,
41 .288675134594812882254574390251f, -0.5f,
42 .577350269189625764509148780502f, 0.0f
45 /* these all are for a hex one unit across */
46 static double hexptvd[6][2] = {
47 {.577350269189625764509148780502, 0.0}, /* 1.0/sqrt3 */
48 {.288675134594812882254574390251, 0.5}, /* 0.5/sqrt3 */
49 {-.288675134594812882254574390251, 0.5},
50 {-.577350269189625764509148780502, 0.0},
51 {-.288675134594812882254574390251, -0.5},
52 {.288675134594812882254574390251, -0.5}
57 /* TODO how is this related? to the above? */
58 static double texptvd[6][2] = {
59 {1.154700538379251529018297561004, 0.5}, /* 2.0/sqrt3 */
60 {.866025403784438646763723170753, 1.0}, /* 1.5/sqrt3 */
61 {.288675134594812882254574390251, 1.0},
63 {.288675134594812882254574390251, 0.0},
64 {.866025403784438646763723170753, 0.0}
67 static double hexpthd[6][2] = {
68 {0.0, .577350269189625764509148780502},
69 {0.5, .288675134594812882254574390251},
70 {0.5, -.288675134594812882254574390251},
71 {0.0, -.577350269189625764509148780502},
72 {-0.5, -.288675134594812882254574390251},
73 {-0.5, .288675134594812882254574390251}
78 void HL_vertices(int cantor, double *vc) {
82 HL_hexcenter(cantor, &xc, &yc);
85 *vc++ = hexptvd[i][0] + xc;
86 *vc++ = hexptvd[i][1] + yc;
88 *vc++ = hexptvd[0][0] + xc;
89 *vc++ = hexptvd[0][1] + yc;
92 void HL_trianglefan(int cantor, double *vc) {
93 HL_hexcenter(cantor, vc, vc+1);
94 HL_vertices(cantor, vc+2);
97 double HL_center_x(int cantor) {
100 HL_hexcenter(cantor, &x, 0);
104 double HL_center_y(int cantor) {
107 HL_hexcenter(cantor, 0, &y);
111 int HL_hexcenter(int cantor, double *xc, double *yc) {
113 double stride = 1.5/sqrt(3.0);
115 inversecantor(cantor, &x, &y);
117 if (xc) *xc = x * stride;
118 if (yc && x >= 0) *yc = y + ((x + 1) % 2) / 2.0 - 0.5;
119 if (yc && x < 0) *yc = y + ((-x + 1) % 2) / 2.0 - 0.5;
125 * This function assumes that the hexes are one unit across, and vertically
126 * oriented. If that is not the case, you will need to transform
127 * your input coordinates first.
129 int HL_cantor_bin(double x, double y) {
130 return HL_hexbin(1.0, x, y, 0, 0);
133 static int xy2ijk(int x, int y, int *i, int *j, int *k) {
139 pj = pj + (-x + 1) / 2;
149 return HL_cantor_xy(x,y);
152 static int ijk2xy(int i, int j, int k, int *x, int *y) {
169 return HL_cantor_xy(px,py);
172 int HL_cantor_ijk(int i, int j, int k) {
173 return ijk2xy(i,j,k,0,0);
176 int HL_distance(int from, int to) {
180 HL_cantor_arrays(from, 0, fc);
181 HL_cantor_arrays(to, 0, tc);
184 dist += abs(fc[i] - tc[i]);
190 int HL_hexes_within_range(int hex, int range, int *list, int size) {
195 return HL_hexes_at_range(hex, 0, list, size);
198 for (i=1;i<=range;i++) {
199 count += HL_hexes_at_range(hex, i, count > size ? 0 : list+count, size-count);
204 int HL_hexes_at_range(int hex, int range, int *list, int size) {
205 int q; /* p and q are count/loop vars */
206 int c[3]; /* ijk coord array */
213 } else if (range < 0) {
217 /* TODO don't bother to collect if the list isn't big enough? */
218 /* i.e. if (!list || size < range * 6) */
219 if (!list || size < 1) return range * 6;
221 HL_cantor_arrays(hex, 0, c);
224 hex = HL_cantor_ijkp(c);
226 for(q=0; q<size && q < range * 6; q++) {
228 hex = HL_adjhex(hex, q/range+2);
234 int HL_adjacent_hex(int start, int dir) {
235 if (dir < 0 || dir > 5) return 0;
237 return HL_adjhex(start, dir);
240 /* direction 0 is positive X , counter clockwise from there */
241 int HL_adjhex(int start, int dir) {
244 HL_cantor_arrays(start, 0, c);
248 c[0]--; c[1]++; break;
250 c[1]++; c[2]--; break;
252 c[0]++; c[2]--; break;
254 c[0]++; c[1]--; break;
256 c[1]--; c[2]++; break;
258 c[0]--; ; c[2]++; break;
261 return HL_cantor_ijkp(c);
264 int HL_cantor_xyp(int *xy) {
265 return HL_cantor_xy(xy[0], xy[1]);
268 int HL_cantor_ijkp(int *ijk) {
269 return HL_cantor_ijk(ijk[0], ijk[1], ijk[2]);
272 int HL_cantor_arrays(int can, int *xy, int *ijk) {
273 return HL_cantor_decode(can, xy, xy ? xy+1 : 0,
274 ijk, ijk ? ijk+1 : 0, ijk ? ijk+2 : 0);
277 int HL_cantor_decode(int can, int *x, int *y, int *i, int *j, int *k) {
280 inversecantor(can, &px, &py);
284 xy2ijk(px, py, i, j, k);
289 int HL_cantor_i(int cantor) {
292 HL_cantor_decode(cantor, 0,0, &i,0,0);
296 int HL_cantor_j(int cantor) {
299 HL_cantor_decode(cantor, 0,0, 0,&j,0);
303 int HL_cantor_k(int cantor) {
306 HL_cantor_decode(cantor, 0,0, 0,0,&k);
310 int HL_cantor_x(int cantor) {
312 inversecantor(cantor, &x, 0);
316 int HL_cantor_y(int cantor) {
318 inversecantor(cantor, 0, &y);
322 /* Determine if a map with these dimensions will overflow */
323 int HL_map_bounds_ok(int xdim, int ydim) {
325 /* return (x+y) * (x + y + 1) / 2 + y+1; */
327 if (INT_MAX - xdim - 1 < ydim) return 0;
328 if (INT_MAX / (xdim+ydim) < (xdim+ydim+1)) return 0;
329 if ( (xdim+ydim) * (xdim+ydim+1) / 2 > INT_MAX - ydim - 1)
335 int HL_map_max_dimension(void) {
338 low = 1; high = INT_MAX/2;
340 while (low != high - 1) {
341 try = (low + high) / 2;
342 if (HL_map_bounds_ok(try,try)) {
352 static int inversenatcantor(int cantor, int *x, int *y) {
357 w = (int)floor((sqrt(8.0 * cantor + 1.0) - 1.0)/2.0);
369 * map non negative integer pairs to their cantor pairing function
370 * number, plus one. We add one so that the result is never zero,
371 * leaving zero to be "invalid" or "none" or what have you.
374 static int natcantor(int x, int y) {
375 return (x+y) * (x + y + 1) / 2 + y+1;
378 /* See http://en.wikipedia.org/wiki/Cantor_pairing_function */
379 /* see also http://szudzik.com/ElegantPairing.pdf */
381 * if either coordinate is negative, map the integers onto the
382 * whole numbers, and then return the negative of the adjusted
383 * cantor number. As for most grids negative coordinates will
384 * be invalid, this will allow for a <= 0 test for invalid
385 * or out of bounds (on the negative side anyway, you'll
386 * still have to test for out of range on the positive side).
388 * TODO figure out what the maximum supported coordinates are
389 * for given integer sizes.
391 int HL_cantor_xy(int x, int y) {
392 if (x < 0 || y < 0) {
393 x = abs(2 * x) - (x < 0);
394 y = abs(2 * y) - (y < 0);
395 return -natcantor(x, y);
397 return natcantor(x,y);
400 static int inversecantor(int cantor, int *x, int *y) {
402 inversenatcantor(-cantor, x, y);
418 inversenatcantor(cantor, x, y);
429 /* y *must* be positive down as the xy /iso conversion assumes this */
430 static int hex_xy(struct hex *h) {
431 if (!h->iso) return 1;
433 h->y = -h->y - (h->x+1)/2;
435 /* need to round toward -inf, not toward zero, so x-1 */
436 h->y = -h->y - h->x/2;
445 static int hex_iso(struct hex *h) {
446 if (h->iso) return 1;
449 h->y = (-h->y - (h->x+1)/2);
451 /* need to round toward -inf, not toward zero, so x-1 */
452 h->y = (-h->y - (h->x)/2);
462 int HL_hexbin(double width, double x, double y, int *i, int *j) {
463 double z, rx, ry, rz;
464 double abs_dx, abs_dy, abs_dz;
468 /* TODO just hard-code this cosine */
469 x = x / cos(30 * M_PI / 180.0); /* rotated X coord */
470 y = y - x / 2.0; /* adjustment for rotated X */
472 /* adjust for actual hexwidth */
478 ix = rx = floor(x + 0.5);
479 iy = ry = floor(y + 0.5);
480 iz = rz = floor(z + 0.5);
485 abs_dx = fabs(rx - x);
486 abs_dy = fabs(ry - y);
487 abs_dz = fabs(rz - z);
489 if (abs_dx >= abs_dy && abs_dx >= abs_dz) {
491 } else if (abs_dy >= abs_dx && abs_dy >= abs_dz) {
505 return HL_cantor_xy(h.x, h.y);