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[] = {
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 /* these all are for a hex one unit across */
24 static double hexptvd[6][2] = {
25 {.577350269189625764509148780502, 0.0}, /* 1.0/sqrt3 */
26 {.288675134594812882254574390251, 0.5}, /* 0.5/sqrt3 */
27 {-.288675134594812882254574390251, 0.5},
28 {-.577350269189625764509148780502, 0.0},
29 {-.288675134594812882254574390251, -0.5},
30 {.288675134594812882254574390251, -0.5}
35 /* TODO how is this related? to the above? */
36 static double texptvd[6][2] = {
37 {1.154700538379251529018297561004, 0.5}, /* 2.0/sqrt3 */
38 {.866025403784438646763723170753, 1.0}, /* 1.5/sqrt3 */
39 {.288675134594812882254574390251, 1.0},
41 {.288675134594812882254574390251, 0.0},
42 {.866025403784438646763723170753, 0.0}
45 static double hexpthd[6][2] = {
46 {0.0, .577350269189625764509148780502},
47 {0.5, .288675134594812882254574390251},
48 {0.5, -.288675134594812882254574390251},
49 {0.0, -.577350269189625764509148780502},
50 {-0.5, -.288675134594812882254574390251},
51 {-0.5, .288675134594812882254574390251}
56 void HL_vertices(int cantor, double *vc) {
60 HL_hexcenter(cantor, &xc, &yc);
63 *vc++ = hexptvd[i][0] + xc;
64 *vc++ = hexptvd[i][1] + yc;
66 *vc++ = hexptvd[0][0] + xc;
67 *vc++ = hexptvd[0][1] + yc;
70 void HL_trianglefan(int cantor, double *vc) {
71 HL_hexcenter(cantor, vc, vc+1);
72 HL_vertices(cantor, vc+2);
75 double HL_center_x(int cantor) {
78 HL_hexcenter(cantor, &x, 0);
82 double HL_center_y(int cantor) {
85 HL_hexcenter(cantor, 0, &y);
89 int HL_hexcenter(int cantor, double *xc, double *yc) {
91 double stride = 1.5/sqrt(3.0);
93 inversecantor(cantor, &x, &y);
95 if (xc) *xc = x * stride;
96 if (yc && x >= 0) *yc = y + ((x + 1) % 2) / 2.0 - 0.5;
97 if (yc && x < 0) *yc = y + ((-x + 1) % 2) / 2.0 - 0.5;
103 * This function assumes that the hexes are one unit across, and vertically
104 * oriented. If that is not the case, you will need to transform
105 * your input coordinates first.
107 int HL_cantor_bin(double x, double y) {
108 return HL_hexbin(1.0, x, y, 0, 0);
111 static int xy2ijk(int x, int y, int *i, int *j, int *k) {
117 pj = pj + (-x + 1) / 2;
127 return HL_cantor_xy(x,y);
130 static int ijk2xy(int i, int j, int k, int *x, int *y) {
147 return HL_cantor_xy(px,py);
150 int HL_cantor_ijk(int i, int j, int k) {
151 return ijk2xy(i,j,k,0,0);
154 int HL_distance(int from, int to) {
158 HL_cantor_arrays(from, 0, fc);
159 HL_cantor_arrays(to, 0, tc);
162 dist += abs(fc[i] - tc[i]);
168 int HL_hexes_within_range(int hex, int range, int *list, int size) {
173 return HL_hexes_at_range(hex, 0, list, size);
176 for (i=1;i<=range;i++) {
177 count += HL_hexes_at_range(hex, i, count > size ? 0 : list+count, size-count);
182 int HL_hexes_at_range(int hex, int range, int *list, int size) {
183 int q; /* p and q are count/loop vars */
184 int c[3]; /* ijk coord array */
191 } else if (range < 0) {
195 /* TODO don't bother to collect if the list isn't big enough? */
196 /* i.e. if (!list || size < range * 6) */
197 if (!list || size < 1) return range * 6;
199 HL_cantor_arrays(hex, 0, c);
202 hex = HL_cantor_ijkp(c);
204 for(q=0; q<size && q < range * 6; q++) {
206 hex = HL_adjhex(hex, q/range+2);
212 int HL_adjacent_hex(int start, int dir) {
213 if (dir < 0 || dir > 5) return 0;
215 return HL_adjhex(start, dir);
218 /* direction 0 is positive X , counter clockwise from there */
219 int HL_adjhex(int start, int dir) {
222 HL_cantor_arrays(start, 0, c);
226 c[0]--; c[1]++; break;
228 c[1]++; c[2]--; break;
230 c[0]++; c[2]--; break;
232 c[0]++; c[1]--; break;
234 c[1]--; c[2]++; break;
236 c[0]--; ; c[2]++; break;
239 return HL_cantor_ijkp(c);
242 int HL_cantor_xyp(int *xy) {
243 return HL_cantor_xy(xy[0], xy[1]);
246 int HL_cantor_ijkp(int *ijk) {
247 return HL_cantor_ijk(ijk[0], ijk[1], ijk[2]);
250 int HL_cantor_arrays(int can, int *xy, int *ijk) {
251 return HL_cantor_decode(can, xy, xy ? xy+1 : 0,
252 ijk, ijk ? ijk+1 : 0, ijk ? ijk+2 : 0);
255 int HL_cantor_decode(int can, int *x, int *y, int *i, int *j, int *k) {
258 inversecantor(can, &px, &py);
262 xy2ijk(px, py, i, j, k);
267 int HL_cantor_i(int cantor) {
270 HL_cantor_decode(cantor, 0,0, &i,0,0);
274 int HL_cantor_j(int cantor) {
277 HL_cantor_decode(cantor, 0,0, 0,&j,0);
281 int HL_cantor_k(int cantor) {
284 HL_cantor_decode(cantor, 0,0, 0,0,&k);
288 int HL_cantor_x(int cantor) {
290 inversecantor(cantor, &x, 0);
294 int HL_cantor_y(int cantor) {
296 inversecantor(cantor, 0, &y);
300 /* Determine if a map with these dimensions will overflow */
301 int HL_map_bounds_ok(int xdim, int ydim) {
303 /* return (x+y) * (x + y + 1) / 2 + y+1; */
305 if (INT_MAX - xdim - 1 < ydim) return 0;
306 if (INT_MAX / (xdim+ydim) < (xdim+ydim+1)) return 0;
307 if ( (xdim+ydim) * (xdim+ydim+1) / 2 > INT_MAX - ydim - 1)
313 int HL_map_max_dimension(void) {
316 low = 1; high = INT_MAX/2;
318 while (low != high - 1) {
319 try = (low + high) / 2;
320 if (HL_map_bounds_ok(try,try)) {
330 static int inversenatcantor(int cantor, int *x, int *y) {
335 w = (int)floor((sqrt(8.0 * cantor + 1.0) - 1.0)/2.0);
347 * map non negative integer pairs to their cantor pairing function
348 * number, plus one. We add one so that the result is never zero,
349 * leaving zero to be "invalid" or "none" or what have you.
352 static int natcantor(int x, int y) {
353 return (x+y) * (x + y + 1) / 2 + y+1;
356 /* See http://en.wikipedia.org/wiki/Cantor_pairing_function */
357 /* see also http://szudzik.com/ElegantPairing.pdf */
359 * if either coordinate is negative, map the integers onto the
360 * whole numbers, and then return the negative of the adjusted
361 * cantor number. As for most grids negative coordinates will
362 * be invalid, this will allow for a <= 0 test for invalid
363 * or out of bounds (on the negative side anyway, you'll
364 * still have to test for out of range on the positive side).
366 * TODO figure out what the maximum supported coordinates are
367 * for given integer sizes.
369 int HL_cantor_xy(int x, int y) {
370 if (x < 0 || y < 0) {
371 x = abs(2 * x) - (x < 0);
372 y = abs(2 * y) - (y < 0);
373 return -natcantor(x, y);
375 return natcantor(x,y);
378 static int inversecantor(int cantor, int *x, int *y) {
380 inversenatcantor(-cantor, x, y);
396 inversenatcantor(cantor, x, y);
407 /* y *must* be positive down as the xy /iso conversion assumes this */
408 static int hex_xy(struct hex *h) {
409 if (!h->iso) return 1;
411 h->y = -h->y - (h->x+1)/2;
413 /* need to round toward -inf, not toward zero, so x-1 */
414 h->y = -h->y - h->x/2;
423 static int hex_iso(struct hex *h) {
424 if (h->iso) return 1;
427 h->y = (-h->y - (h->x+1)/2);
429 /* need to round toward -inf, not toward zero, so x-1 */
430 h->y = (-h->y - (h->x)/2);
440 int HL_hexbin(double width, double x, double y, int *i, int *j) {
441 double z, rx, ry, rz;
442 double abs_dx, abs_dy, abs_dz;
446 /* TODO just hard-code this cosine */
447 x = x / cos(30 * M_PI / 180.0); /* rotated X coord */
448 y = y - x / 2.0; /* adjustment for rotated X */
450 /* adjust for actual hexwidth */
456 ix = rx = floor(x + 0.5);
457 iy = ry = floor(y + 0.5);
458 iz = rz = floor(z + 0.5);
463 abs_dx = fabs(rx - x);
464 abs_dy = fabs(ry - y);
465 abs_dz = fabs(rz - z);
467 if (abs_dx >= abs_dy && abs_dx >= abs_dz) {
469 } else if (abs_dy >= abs_dx && abs_dy >= abs_dz) {
483 return HL_cantor_xy(h.x, h.y);