]> pd.if.org Git - hexagon/commitdiff
Added some horizontal grid constants.
authorNathan Wagner <nw@hydaspes.if.org>
Mon, 20 Dec 2010 15:58:06 +0000 (15:58 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Mon, 20 Dec 2010 15:58:06 +0000 (15:58 +0000)
doc/Makefile
doc/hexagon.pod
hexagon.c
hexagon.h

index d81d2542d64915def8823fd6c3c740e1a7b8b8e7..c00373a0daa5c781af1e1a17b9c64e83a7c7be53 100644 (file)
@@ -1,5 +1,7 @@
+RELEASE = $(shell git tag | tail -1)
+
 hexagon.3:     hexagon.pod
-       pod2man --center='hexagon library' --section=3 --release='hexagon 0.1' $+ > $@
+       pod2man --center='hexagon library' --section=3 --release='Version $(RELEASE)' $+ > $@
 
 hexagon.txt: hexagon.pod
        pod2text -l $+
index 9dfc4ae6074662a00b39e3fe680b1cc50cf49e52..d5a6e4b61a81e6ba1ce2fdec672160193b2c1a6b 100644 (file)
@@ -44,23 +44,36 @@ above system.
 
 Many games and displays use a positive Y down, rather than the
 usual mathematical positive Y up.  The library uses the mathematical
-convention.  To convert to a positive Y down, you can take the
-negative of the Y coordinate as given by the library.  See the HL_hexbin()
-example below.
+convention.  In general though, this will not matter.  The Y coordinate
+of the hex centers has the same direction as the Y coordinate of the
+hex grid.  If your display is Y down, and your grid is too, you can
+use the coordinates as given.  If your display is Y up and you want
+your grid to be Y down, you will, obviously, need to invert the Y
+coordinates for display, and re-invert them when passing them back to
+the library.
+
+To convert to a positive Y down, you can take the negative of the Y coordinate
+as given by the library.  See the HL_hexbin() example below.
 
 =head3 Horizontal Grids
 
-A horizontal grid layout can be created by either rotating the
-given coordinates by 90 degrees, or, equivalently, swapping the X and
-Y coordinates as given by the library.  Obviously, you will need to
-swap them when passing them into the library also.  You may also
-need to invert the Y coordinate as above for display.
+A horizontal grid layout can be created by either rotating the given
+coordinates by 90 degrees, or, equivalently, swapping the X and Y coordinates
+as given by the library.  Obviously, you will need to swap them when passing
+them into the library also.  You may also need to invert the (resulting) Y
+coordinate as above for display if your display is positive Y down.
 
-=head2 Hexagon grid functions
+=head2 Hexagon IDs
 
-The hexagon grid is constrained to be a vertical grid, with
-the hexagon at (2,1) being to the right and half a hex below
-the hex at (1,1).  Horizontal grid layouts are not supported.
+Two dimensional hex locations are converted into a single integer
+hexagon ID with a pairing function.  A pairing function uniquely
+and reversably maps two integers onto one integer.  At the moment,
+only a modified Cantor's original triangular pairing function,
+extended to handle negative integers,
+is available.
+
+As most hexagon grids will only use positive coordinates, additional
+pairing functions will be provided in the future.
 
 =head3 int HL_cantor(int x, int y)
 
@@ -69,6 +82,12 @@ Converts a two dimensional coordinate to a canonical id.
        int hex;
        hex = HL_cantor(1,1); /* hex == 5 */
 
+=head2 Hexagon grid functions
+
+The hexagon grid is constrained to be a vertical grid, with the hexagon at
+(2,1) being to the right and half a hex below the hex at (1,1).  Horizontal
+grid layouts are not supported.
+
 =head3 int HL_cantor_x(int cantor);
 
 Returns the X coordinate of a hex identified by the given cantor id.
@@ -104,6 +123,33 @@ is because the X axis passes through the center of the even columns,
 so the negation works, but it passes along the edge of the odd columns,
 and you end up with an off by one error.
 
+=head2 Hexagon data
+
+Some pre-calculated data is available.
+
+=head3 double HL_fand[16]
+
+An array of hexagon coordinates, with the hex center x,y at indexes 0 and
+1 (these are 0.0,0.0), and each vertex following.  The first vertex
+is repeated at the end of the array.  Each vertex takes up two consecutive
+array elements for it's X and Y coordinates.  Thus for hex vertexes 1 to
+6, and hex center C this array is CxCy1x1y2x2y3x3y4x4y5x5y6x6y1x1y.
+This is suitable for passing directly to opengl for drawing a hexagon
+as a triangle fan.
+
+=head3 float HL_fanf[16]
+
+An array of floats, laid out the same way as HL_fand.
+
+=head3 double HL_hfand[16]
+
+This is the same as HL_fand, but will draw a horizontal hex.  The same
+effect could be had by rotating 90 degrees.
+
+=head3 float HL_hfanf[16]
+
+An array of float with horizontal layout.
+
 =head2 Pathfinding
 
 The library provides an A* implementation for pathfinding on
index bd07f256d18f1d30d73ce61c3b7d6a17ea9a68c1..264734c6c95ee17493d39f0cad5ce411821377da 100644 (file)
--- a/hexagon.c
+++ b/hexagon.c
@@ -31,6 +31,17 @@ double HL_fand[16] = {
        .577350269189625764509148780502, 0.0
 };
 
+double HL_hfand[16] = {
+       0.0, 0.0,
+       0.0, .577350269189625764509148780502,
+       0.5, .288675134594812882254574390251,
+       0.5, -.288675134594812882254574390251,
+       0.0, -.577350269189625764509148780502,
+       -0.5, -.288675134594812882254574390251,
+       -0.5, .288675134594812882254574390251,
+       0.0, .577350269189625764509148780502
+};
+
 float HL_fanf[16] = {
        0.0f, 0.0f,
        .577350269189625764509148780502f, 0.0f,
@@ -42,6 +53,17 @@ float HL_fanf[16] = {
        .577350269189625764509148780502f, 0.0f
 };
 
+float HL_hfanf[16] = {
+       0.0f, 0.0f,
+       0.0f, .577350269189625764509148780502f,
+       0.5f, .288675134594812882254574390251f,
+       0.5f, -.288675134594812882254574390251f,
+       0.0f, -.577350269189625764509148780502f,
+       -0.5f, -.288675134594812882254574390251f,
+       -0.5f, .288675134594812882254574390251f,
+       0.0f, .577350269189625764509148780502f
+};
+
 /* size of a square that will exactly fit in a hexagon */
 /* 2.0/(1+sqrt(3.0)) */
 double HL_square = .73205080756887729352;
@@ -463,14 +485,16 @@ static int hex_iso(struct hex *h) {
 
 #endif
 
+#define COS30 (.866025403784438646763723170752)
+
 int HL_hexbin(double width, double x, double y, int *i, int *j) {
        double z, rx, ry, rz;
        double abs_dx, abs_dy, abs_dz;
        int ix, iy, iz, s;
        struct hex h;
 
-       /* TODO just hard-code this cosine */
-       x = x / cos(30 * M_PI / 180.0); /* rotated X coord */
+       /*x = x / cos(30 * M_PI / 180.0); */ /* rotated X coord */
+       x = x / COS30;
        y = y - x / 2.0; /* adjustment for rotated X */
 
        /* adjust for actual hexwidth */
index c12614d2b591b751a6f671f8ed84daa955803b60..7ca8001cebc609f307db55ef14f868ac86a7eb8a 100644 (file)
--- a/hexagon.h
+++ b/hexagon.h
@@ -144,6 +144,8 @@ struct HL_astar *HL_astar_init(struct HL_astar *s);
 
 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