| 1 |
|
| #include "hexgrid.h" |
| 2 |
|
| #include <stdlib.h> |
| 3 |
|
| #include <math.h> |
| 4 |
|
| |
| 5 |
42 |
| HexCube hex_oddr_to_cube(HexOddr coord) { |
| 6 |
42 |
| int col = coord.col; |
| 7 |
42 |
| int row = coord.row; |
| 8 |
42 |
| int q = col - ((row - (row & 1)) >> 1); |
| 9 |
42 |
| int r = row; |
| 10 |
42 |
| int s = -q - r; |
| 11 |
42 |
| HexCube c = { q, r, s }; |
| 12 |
42 |
| return c; |
| 13 |
|
| } |
| 14 |
|
| |
| 15 |
14 |
| HexOddr hex_cube_to_oddr(HexCube cube) { |
| 16 |
14 |
| int q = cube.q; |
| 17 |
14 |
| int r = cube.r; |
| 18 |
14 |
| int col = q + ((r - (r & 1)) >> 1); |
| 19 |
14 |
| int row = r; |
| 20 |
14 |
| HexOddr o = { col, row }; |
| 21 |
14 |
| return o; |
| 22 |
|
| } |
| 23 |
|
| |
| 24 |
27 |
| static inline int int_abs(int x) { |
| 25 |
27 |
| return x < 0 ? -x : x; |
| 26 |
|
| } |
| 27 |
|
| |
| 28 |
18 |
| static inline int int_max(int a, int b) { |
| 29 |
18 |
| return a > b ? a : b; |
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
9 |
| int hex_distance(HexOddr a, HexOddr b) { |
| 33 |
9 |
| HexCube ca = hex_oddr_to_cube(a); |
| 34 |
9 |
| HexCube cb = hex_oddr_to_cube(b); |
| 35 |
9 |
| return int_max(int_abs(ca.q - cb.q), int_max(int_abs(ca.r - cb.r), int_abs(ca.s - cb.s))); |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
6 |
| bool hex_is_collinear(HexOddr a, HexOddr b) { |
| 39 |
6 |
▶ | if (a.col == b.col && a.row == b.row) return false; |
Branch: 2 branches
| Branch | True | False | Total |
| if (a.col == b.col && a.row == b.row) return false; |
0 |
6 |
6 |
| if (a.col == b.col && a.row == b.row) return false; |
0 |
0 |
0 |
|
| 40 |
6 |
| HexCube ca = hex_oddr_to_cube(a); |
| 41 |
6 |
| HexCube cb = hex_oddr_to_cube(b); |
| 42 |
6 |
▶ | return ca.q == cb.q || ca.r == cb.r || ca.s == cb.s; |
Branch: 3 branches
| Branch | True | False | Total |
| return ca.q == cb.q || ca.r == cb.r || ca.s == cb.s; |
6 |
0 |
6 |
| return ca.q == cb.q || ca.r == cb.r || ca.s == cb.s; |
2 |
4 |
6 |
| return ca.q == cb.q || ca.r == cb.r || ca.s == cb.s; |
0 |
2 |
2 |
|
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
18 |
| HexOddr hex_neighbor(HexOddr coord, int dir_index) { |
| 46 |
18 |
| int c = coord.col; |
| 47 |
18 |
| int r = coord.row; |
| 48 |
18 |
| int parity = r & 1; |
| 49 |
18 |
| dir_index = (dir_index % 6 + 6) % 6; |
| 50 |
|
| |
| 51 |
18 |
| int dc = 0; |
| 52 |
18 |
| int dr = 0; |
| 53 |
|
| |
| 54 |
18 |
| switch (dir_index) { |
| 55 |
3 |
| case 0: dc = +1; dr = 0; break; |
| 56 |
3 |
| case 1: dc = parity == 0 ? 0 : +1; dr = -1; break; |
| 57 |
3 |
▶ | case 2: dc = parity == 0 ? -1 : 0; dr = -1; break; |
Branch: 1 branch
| Branch | True | False | Total |
| case 2: dc = parity == 0 ? -1 : 0; dr = -1; break; |
2 |
1 |
3 |
|
| 58 |
3 |
| case 3: dc = -1; dr = 0; break; |
| 59 |
3 |
▶ | case 4: dc = parity == 0 ? -1 : 0; dr = +1; break; |
Branch: 1 branch
| Branch | True | False | Total |
| case 4: dc = parity == 0 ? -1 : 0; dr = +1; break; |
2 |
1 |
3 |
|
| 60 |
3 |
| case 5: dc = parity == 0 ? 0 : +1; dr = +1; break; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
18 |
| HexOddr res = { c + dc, r + dr }; |
| 64 |
18 |
| return res; |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
3 |
| void hex_neighbors(HexOddr coord, HexOddr out_neighbors[6]) { |
| 68 |
21 |
▶ | for (int i = 0; i < 6; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < 6; i++) { |
18 |
3 |
21 |
|
| 69 |
18 |
| out_neighbors[i] = hex_neighbor(coord, i); |
| 70 |
|
| } |
| 71 |
3 |
| } |
| 72 |
|
| |
| 73 |
3 |
| int hex_collinear_ray(HexOddr from, HexOddr to, HexOddr* out_ray, int max_length) { |
| 74 |
3 |
▶ | if (!hex_is_collinear(from, to) || max_length <= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!hex_is_collinear(from, to) || max_length <= 0) { |
2 |
1 |
3 |
| if (!hex_is_collinear(from, to) || max_length <= 0) { |
0 |
2 |
2 |
|
| 75 |
1 |
| return 0; |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
2 |
| HexCube c_from = hex_oddr_to_cube(from); |
| 79 |
2 |
| HexCube c_to = hex_oddr_to_cube(to); |
| 80 |
2 |
| int dist = hex_distance(from, to); |
| 81 |
2 |
▶ | if (dist == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dist == 0) return 0; |
0 |
2 |
2 |
|
| 82 |
|
| |
| 83 |
2 |
| double dq = (double)(c_to.q - c_from.q) / dist; |
| 84 |
2 |
| double dr = (double)(c_to.r - c_from.r) / dist; |
| 85 |
2 |
| double ds = (double)(c_to.s - c_from.s) / dist; |
| 86 |
|
| |
| 87 |
2 |
| int count = dist < max_length ? dist : max_length; |
| 88 |
|
| |
| 89 |
8 |
▶ | for (int step = 1; step <= count; step++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int step = 1; step <= count; step++) { |
6 |
2 |
8 |
|
| 90 |
6 |
| int q = (int)round(c_from.q + dq * step); |
| 91 |
6 |
| int r = (int)round(c_from.r + dr * step); |
| 92 |
6 |
| int s = (int)round(c_from.s + ds * step); |
| 93 |
6 |
| HexCube step_cube = { q, r, s }; |
| 94 |
6 |
| out_ray[step - 1] = hex_cube_to_oddr(step_cube); |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
2 |
| return count; |
| 98 |
|
| } |