X-Git-Url: https://pd.if.org/git/?p=hexagon;a=blobdiff_plain;f=hexagon.h;h=a2d4f2f98a8e18306a3352bd32ab3af7b1708d18;hp=50665d058f87c05faaa42e11838688892485d86c;hb=HEAD;hpb=8ca2b8a06b00066a20f744a61c9bcdd65949a344 diff --git a/hexagon.h b/hexagon.h index 50665d0..a2d4f2f 100644 --- a/hexagon.h +++ b/hexagon.h @@ -1,6 +1,24 @@ #ifndef HEXLIB_H_ #define HEXLIB_H_ 1 +struct HL_hex { + int x, y; +}; + +struct HL_isohex { + int i,j,k; +}; + +struct HL_point { + double x, y; +}; + +struct HL_hex HL_hex_xy(int x, int y); + +int HL_hex_cmp(struct HL_hex const *a, struct HL_hex const *b); + +int HL_hex_eq(struct HL_hex const *a, struct HL_hex const *b); + /* * this library implements a generic library for hexgrids for games. * it avoids display issues, other than some general provisions @@ -25,7 +43,8 @@ * on a hex boundary, the result will depend on how the machine * does the floating point math, but should be consistent. */ -int HL_hexbin(double width, double x, double y, int *i, int *j); +struct HL_hex HL_hexbin(double width, double x, double y); +struct HL_hex HL_bin(double x, double y); /* generate the cantor mapping of a hexes x,y coordinate. * See http://en.wikipedia.org/wiki/Cantor_pairing_function. @@ -44,24 +63,10 @@ int HL_hexbin(double width, double x, double y, int *i, int *j); * is independent of the size of the grid. */ -/* Cartesian functions */ +/* Cantor encoding/decoding functions */ +int HL_cantor(struct HL_hex h); int HL_cantor_xy(int x, int y); -int HL_cantor_xyp(int *xy); -int HL_cantor_x(int cantor); -int HL_cantor_y(int cantor); - -/* Isometric functions */ -int HL_cantor_ijk(int i, int j, int k); -int HL_cantor_ijkp(int *ijk); -int HL_cantor_i(int cantor); -int HL_cantor_k(int cantor); -int HL_cantor_j(int cantor); - -int HL_cantor_bin(double x, double y); - -/* A general decode function */ -int HL_cantor_decode(int can, int *x, int *y, int *i, int *j, int *k); -int HL_cantor_arrays(int can, int *xy, int *ijk); +struct HL_hex HL_uncantor(int cantor); /* * layout assistance functions @@ -72,20 +77,23 @@ int HL_cantor_arrays(int can, int *xy, int *ijk); * * Similarly for HL_center_y() and HL_hexcenter() */ -double HL_center_x(int cantor); -double HL_center_y(int cantor); -int HL_hexcenter(int cantor, double *x, double *y); +struct HL_point HL_hexcenter(struct HL_hex hex); -/* how far is it on the hex grid */ -int HL_distance(int from_cantor, int to_cantor); +/* find a point based on a unit hex, given angle and distance from center, + * where a distance of 1 is on the hex side. probably not useful + * for points outside the hex, that is, distance should almost certainly + * be less than or equal to 1.0 + */ +struct HL_point HL_polar(double angle, double distance, double *d); -/* returns 0 for <0 or >5 */ -int HL_adjacent_hex(int start, int dir); +/* how far is it on the hex grid */ +int HL_distance(struct HL_hex from, struct HL_hex to); /* module direction */ -int HL_adjhex(int start, int dir); -int HL_hexes_at_range(int hex, int range, int *list, int size); -int HL_hexes_within_range(int hex, int range, int *list, int size); +struct HL_hex HL_adjhex(struct HL_hex, int dir); +int HL_adjhexp(struct HL_hex, int dir, struct HL_hex *adj); +int HL_hexes_at_range(struct HL_hex, int range, struct HL_hex *list, int size); +int HL_hexes_within_range(struct HL_hex, int range, struct HL_hex *list, int size); typedef double (*HL_costfunc)(int start, int finish); @@ -103,18 +111,14 @@ int HL_map_max_dimension(void); /* * Plotting support */ -void HL_trianglefan(int cantor, double *); - -struct HL_hex { - int x,y; /* hmm. would be nice to be able to just point to a generic */ -}; +void HL_trianglefan(struct HL_hex hex, double *); /* * Pathfinding */ struct HL_astar_hex { - int hex; + struct HL_hex hex; double f, g, h; struct HL_astar_hex *parent; int open; @@ -123,13 +127,12 @@ struct HL_astar_hex { }; struct HL_astar { - int start, goal; + struct HL_hex start, goal; int pathlen; - double (*metric)(int,int); - double (*heuristic)(int,int); - int (*neighbor)(int,int); + double (*metric)(struct HL_hex,struct HL_hex); + double (*heuristic)(struct HL_hex,struct HL_hex); + int (*neighbor)(struct HL_hex,int dir, struct HL_hex *n); int nodes, allocated; - struct HL_astar_hex *sets; struct HL_astar_hex *open; struct HL_astar_hex *closed; struct HL_astar_hex *from, *to; @@ -141,5 +144,13 @@ int HL_findpath(struct HL_astar *s, int loops); void HL_astar_free(struct HL_astar *s); struct HL_astar *HL_astar_clear(struct HL_astar *s); struct HL_astar *HL_astar_init(struct HL_astar *s); +void HL_vertices(struct HL_hex hex, double *vc); + +extern double HL_vertexv[12]; +extern double HL_fand[16]; +extern float HL_fanf[16]; +extern double HL_hfand[16]; /* horizontal grids */ +extern float HL_hfanf[16]; /* horizontal grids */ +extern double HL_square; #endif