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 /* size of a square that will exactly fit in a hexagon */
46 /* 2.0/(1+sqrt(3.0)) */
47 double HL_square = .73205080756887729352;
49 /* these all are for a hex one unit across */
50 static double hexptvd[6][2] = {
51 {.577350269189625764509148780502, 0.0}, /* 1.0/sqrt3 */
52 {.288675134594812882254574390251, 0.5}, /* 0.5/sqrt3 */
53 {-.288675134594812882254574390251, 0.5},
54 {-.577350269189625764509148780502, 0.0},
55 {-.288675134594812882254574390251, -0.5},
56 {.288675134594812882254574390251, -0.5}
61 /* TODO how is this related? to the above? */
62 static double texptvd[6][2] = {
63 {1.154700538379251529018297561004, 0.5}, /* 2.0/sqrt3 */
64 {.866025403784438646763723170753, 1.0}, /* 1.5/sqrt3 */
65 {.288675134594812882254574390251, 1.0},
67 {.288675134594812882254574390251, 0.0},
68 {.866025403784438646763723170753, 0.0}
71 static double hexpthd[6][2] = {
72 {0.0, .577350269189625764509148780502},
73 {0.5, .288675134594812882254574390251},
74 {0.5, -.288675134594812882254574390251},
75 {0.0, -.577350269189625764509148780502},
76 {-0.5, -.288675134594812882254574390251},
77 {-0.5, .288675134594812882254574390251}
82 void HL_vertices(int cantor, double *vc) {
86 HL_hexcenter(cantor, &xc, &yc);
89 *vc++ = hexptvd[i][0] + xc;
90 *vc++ = hexptvd[i][1] + yc;
92 *vc++ = hexptvd[0][0] + xc;
93 *vc++ = hexptvd[0][1] + yc;
96 void HL_trianglefan(int cantor, double *vc) {
97 HL_hexcenter(cantor, vc, vc+1);
98 HL_vertices(cantor, vc+2);
101 double HL_center_x(int cantor) {
104 HL_hexcenter(cantor, &x, 0);
108 double HL_center_y(int cantor) {
111 HL_hexcenter(cantor, 0, &y);
115 int HL_hexcenter(int cantor, double *xc, double *yc) {
117 double stride = 1.5/sqrt(3.0);
119 inversecantor(cantor, &x, &y);
121 if (xc) *xc = x * stride;
122 if (yc && x >= 0) *yc = y + ((x + 1) % 2) / 2.0 - 0.5;
123 if (yc && x < 0) *yc = y + ((-x + 1) % 2) / 2.0 - 0.5;
129 * This function assumes that the hexes are one unit across, and vertically
130 * oriented. If that is not the case, you will need to transform
131 * your input coordinates first.
133 int HL_cantor_bin(double x, double y) {
134 return HL_hexbin(1.0, x, y, 0, 0);
137 static int xy2ijk(int x, int y, int *i, int *j, int *k) {
143 pj = pj + (-x + 1) / 2;
153 return HL_cantor_xy(x,y);
156 static int ijk2xy(int i, int j, int k, int *x, int *y) {
173 return HL_cantor_xy(px,py);
176 int HL_cantor_ijk(int i, int j, int k) {
177 return ijk2xy(i,j,k,0,0);
180 int HL_distance(int from, int to) {
184 HL_cantor_arrays(from, 0, fc);
185 HL_cantor_arrays(to, 0, tc);
188 dist += abs(fc[i] - tc[i]);
194 int HL_hexes_within_range(int hex, int range, int *list, int size) {
199 return HL_hexes_at_range(hex, 0, list, size);
202 for (i=1;i<=range;i++) {
203 count += HL_hexes_at_range(hex, i, count > size ? 0 : list+count, size-count);
208 int HL_hexes_at_range(int hex, int range, int *list, int size) {
209 int q; /* p and q are count/loop vars */
210 int c[3]; /* ijk coord array */
217 } else if (range < 0) {
221 /* TODO don't bother to collect if the list isn't big enough? */
222 /* i.e. if (!list || size < range * 6) */
223 if (!list || size < 1) return range * 6;
225 HL_cantor_arrays(hex, 0, c);
228 hex = HL_cantor_ijkp(c);
230 for(q=0; q<size && q < range * 6; q++) {
232 hex = HL_adjhex(hex, q/range+2);
238 int HL_adjacent_hex(int start, int dir) {
239 if (dir < 0 || dir > 5) return 0;
241 return HL_adjhex(start, dir);
244 /* direction 0 is positive X , counter clockwise from there */
245 int HL_adjhex(int start, int dir) {
248 HL_cantor_arrays(start, 0, c);
252 c[0]--; c[1]++; break;
254 c[1]++; c[2]--; break;
256 c[0]++; c[2]--; break;
258 c[0]++; c[1]--; break;
260 c[1]--; c[2]++; break;
262 c[0]--; ; c[2]++; break;
265 return HL_cantor_ijkp(c);
268 int HL_cantor_xyp(int *xy) {
269 return HL_cantor_xy(xy[0], xy[1]);
272 int HL_cantor_ijkp(int *ijk) {
273 return HL_cantor_ijk(ijk[0], ijk[1], ijk[2]);
276 int HL_cantor_arrays(int can, int *xy, int *ijk) {
277 return HL_cantor_decode(can, xy, xy ? xy+1 : 0,
278 ijk, ijk ? ijk+1 : 0, ijk ? ijk+2 : 0);
281 int HL_cantor_decode(int can, int *x, int *y, int *i, int *j, int *k) {
284 inversecantor(can, &px, &py);
288 xy2ijk(px, py, i, j, k);
293 int HL_cantor_i(int cantor) {
296 HL_cantor_decode(cantor, 0,0, &i,0,0);
300 int HL_cantor_j(int cantor) {
303 HL_cantor_decode(cantor, 0,0, 0,&j,0);
307 int HL_cantor_k(int cantor) {
310 HL_cantor_decode(cantor, 0,0, 0,0,&k);
314 int HL_cantor_x(int cantor) {
316 inversecantor(cantor, &x, 0);
320 int HL_cantor_y(int cantor) {
322 inversecantor(cantor, 0, &y);
326 /* Determine if a map with these dimensions will overflow */
327 int HL_map_bounds_ok(int xdim, int ydim) {
329 /* return (x+y) * (x + y + 1) / 2 + y+1; */
331 if (INT_MAX - xdim - 1 < ydim) return 0;
332 if (INT_MAX / (xdim+ydim) < (xdim+ydim+1)) return 0;
333 if ( (xdim+ydim) * (xdim+ydim+1) / 2 > INT_MAX - ydim - 1)
339 int HL_map_max_dimension(void) {
342 low = 1; high = INT_MAX/2;
344 while (low != high - 1) {
345 try = (low + high) / 2;
346 if (HL_map_bounds_ok(try,try)) {
356 static int inversenatcantor(int cantor, int *x, int *y) {
361 w = (int)floor((sqrt(8.0 * cantor + 1.0) - 1.0)/2.0);
373 * map non negative integer pairs to their cantor pairing function
374 * number, plus one. We add one so that the result is never zero,
375 * leaving zero to be "invalid" or "none" or what have you.
378 static int natcantor(int x, int y) {
379 return (x+y) * (x + y + 1) / 2 + y+1;
382 /* See http://en.wikipedia.org/wiki/Cantor_pairing_function */
383 /* see also http://szudzik.com/ElegantPairing.pdf */
385 * if either coordinate is negative, map the integers onto the
386 * whole numbers, and then return the negative of the adjusted
387 * cantor number. As for most grids negative coordinates will
388 * be invalid, this will allow for a <= 0 test for invalid
389 * or out of bounds (on the negative side anyway, you'll
390 * still have to test for out of range on the positive side).
392 * TODO figure out what the maximum supported coordinates are
393 * for given integer sizes.
395 int HL_cantor_xy(int x, int y) {
396 if (x < 0 || y < 0) {
397 x = abs(2 * x) - (x < 0);
398 y = abs(2 * y) - (y < 0);
399 return -natcantor(x, y);
401 return natcantor(x,y);
404 static int inversecantor(int cantor, int *x, int *y) {
406 inversenatcantor(-cantor, x, y);
422 inversenatcantor(cantor, x, y);
433 /* y *must* be positive down as the xy /iso conversion assumes this */
434 static int hex_xy(struct hex *h) {
435 if (!h->iso) return 1;
437 h->y = -h->y - (h->x+1)/2;
439 /* need to round toward -inf, not toward zero, so x-1 */
440 h->y = -h->y - h->x/2;
449 static int hex_iso(struct hex *h) {
450 if (h->iso) return 1;
453 h->y = (-h->y - (h->x+1)/2);
455 /* need to round toward -inf, not toward zero, so x-1 */
456 h->y = (-h->y - (h->x)/2);
466 int HL_hexbin(double width, double x, double y, int *i, int *j) {
467 double z, rx, ry, rz;
468 double abs_dx, abs_dy, abs_dz;
472 /* TODO just hard-code this cosine */
473 x = x / cos(30 * M_PI / 180.0); /* rotated X coord */
474 y = y - x / 2.0; /* adjustment for rotated X */
476 /* adjust for actual hexwidth */
482 ix = rx = floor(x + 0.5);
483 iy = ry = floor(y + 0.5);
484 iz = rz = floor(z + 0.5);
489 abs_dx = fabs(rx - x);
490 abs_dy = fabs(ry - y);
491 abs_dz = fabs(rz - z);
493 if (abs_dx >= abs_dy && abs_dx >= abs_dz) {
495 } else if (abs_dy >= abs_dx && abs_dy >= abs_dz) {
509 return HL_cantor_xy(h.x, h.y);