16 struct HL_hex HL_hex_xy(int x, int y);
18 int HL_hex_cmp(struct HL_hex const *a, struct HL_hex const *b);
20 int HL_hex_eq(struct HL_hex const *a, struct HL_hex const *b);
23 * this library implements a generic library for hexgrids for games.
24 * it avoids display issues, other than some general provisions
27 * This file is written by Nathan Wagner and dedicated to the
31 /* max size for cantor numbers is 16383, or 32767 if non-negative,
32 * assuming a 32 bit int
35 /* map from cartesian coordinates to hex coords */
36 /* cartesian coordinates are positive Y up, hex coords
37 * are positive Y down. You may need to pass in the
38 * negative of your Y coordinate
40 * width is the distance from one hexside to the opposite
41 * hexside. i and j are the cartesian coordinates of the hex
42 * the x,y point falls in. If a point happens to fall exactly
43 * on a hex boundary, the result will depend on how the machine
44 * does the floating point math, but should be consistent.
46 struct HL_hex HL_hexbin(double width, double x, double y);
47 struct HL_hex HL_bin(double x, double y);
49 /* generate the cantor mapping of a hexes x,y coordinate.
50 * See http://en.wikipedia.org/wiki/Cantor_pairing_function.
51 * We modify the basic formula to support negative coordinates
53 * The cantor mapping is *not* a contiguous mapping, as it maps
54 * onto a triangle, so you can't use the cantor mapping as
55 * an index into an array. Or, you can, but the array
56 * will be (I think) twice as large as it needs to be
58 * See also http://mathworld.wolfram.com/PairingFunction.html
59 * for a reference to a square mapping by Stein that doesn't
60 * have this problem, at least for square maps.
62 * We use the cantor mapping so that the generated integer
63 * is independent of the size of the grid.
66 /* Cantor encoding/decoding functions */
67 int HL_cantor(struct HL_hex h);
68 int HL_cantor_xy(int x, int y);
69 struct HL_hex HL_uncantor(int cantor);
72 * layout assistance functions
74 * HL_center_x(int cantor) returns the cartesian X coordinate
75 * of a given hex, assuming a width of 1.0. Scaling can
76 * be done by the caller.
78 * Similarly for HL_center_y() and HL_hexcenter()
80 struct HL_point HL_hexcenter(struct HL_hex hex);
82 /* find a point based on a unit hex, given angle and distance from center,
83 * where a distance of 1 is on the hex side. probably not useful
84 * for points outside the hex, that is, distance should almost certainly
85 * be less than or equal to 1.0
87 struct HL_point HL_polar(double angle, double distance, double *d);
89 /* how far is it on the hex grid */
90 int HL_distance(struct HL_hex from, struct HL_hex to);
92 /* module direction */
93 struct HL_hex HL_adjhex(struct HL_hex, int dir);
94 int HL_adjhexp(struct HL_hex, int dir, struct HL_hex *adj);
95 int HL_hexes_at_range(struct HL_hex, int range, struct HL_hex *list, int size);
96 int HL_hexes_within_range(struct HL_hex, int range, struct HL_hex *list, int size);
98 typedef double (*HL_costfunc)(int start, int finish);
100 /* writes up to len cantors to path. returns last
102 * perhaps it should return the last value not written, if the
103 * path is incomplete?
104 * either way, it can then be used in a loop
106 int HL_path(int start, int end, int *path, int len);
108 int HL_map_bounds_ok(int xdim, int ydim);
109 int HL_map_max_dimension(void);
114 void HL_trianglefan(struct HL_hex hex, double *);
120 struct HL_astar_hex {
123 struct HL_astar_hex *parent;
125 struct HL_astar_hex *next;
126 struct HL_astar_hex *prev;
130 struct HL_hex start, goal;
132 double (*metric)(struct HL_hex,struct HL_hex);
133 double (*heuristic)(struct HL_hex,struct HL_hex);
134 int (*neighbor)(struct HL_hex,int dir, struct HL_hex *n);
135 int nodes, allocated;
136 struct HL_astar_hex *open;
137 struct HL_astar_hex *closed;
138 struct HL_astar_hex *from, *to;
143 int HL_findpath(struct HL_astar *s, int loops);
144 void HL_astar_free(struct HL_astar *s);
145 struct HL_astar *HL_astar_clear(struct HL_astar *s);
146 struct HL_astar *HL_astar_init(struct HL_astar *s);
147 void HL_vertices(struct HL_hex hex, double *vc);
149 extern double HL_vertexv[12];
150 extern double HL_fand[16];
151 extern float HL_fanf[16];
152 extern double HL_hfand[16]; /* horizontal grids */
153 extern float HL_hfanf[16]; /* horizontal grids */
154 extern double HL_square;