]> pd.if.org Git - hexagon/commitdiff
renamed library to hexagon
authorNathan Wagner <nw@hydaspes.if.org>
Sat, 27 Nov 2010 06:26:59 +0000 (06:26 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Sat, 27 Nov 2010 06:26:59 +0000 (06:26 +0000)
17 files changed:
Makefile
astar.c
doc/Makefile [new file with mode: 0644]
doc/hexagon.pod [new file with mode: 0644]
hexagon.c [moved from hexmap.c with 99% similarity]
hexagon.h [moved from hexmap.h with 100% similarity]
hexagon.pod [deleted file]
t/adjacency.c
t/astar.c
t/cantor.c
t/center.c
t/distance.c
t/gridsize.c
t/hexbin.c
t/hextest.c
t/range.c
t/within.c

index 545d6f4d44d2f652fcae10d8d3769ad8d2add25f..9b2f4426ffc8f1cda05ba94a46b00e0bf69184af 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,15 +4,15 @@
 
 LDFLAGS= $(ARCH) -lm
 CFLAGS=        -Wall -Wno-parentheses $(ARCH) -I. -I..
 
 LDFLAGS= $(ARCH) -lm
 CFLAGS=        -Wall -Wno-parentheses $(ARCH) -I. -I..
-OBJS= hexmap.o astar.o
+OBJS= hexagon.o astar.o
 SRC=$(OBJS:.o=.c)
 TESTS= t/cantor.t t/distance.t t/adjacency.t t/range.t t/hexbin.t \
 SRC=$(OBJS:.o=.c)
 TESTS= t/cantor.t t/distance.t t/adjacency.t t/range.t t/hexbin.t \
-       t/gridsize.t t/center.t
+       t/gridsize.t t/center.t t/astar.t
 
 
-all:   test libhexmap.a
+all:   test libhexagon.a docs
 
 clean:
 
 clean:
-       rm -f $(OBJS) libhexmap.a $(TESTS)
+       rm -f $(OBJS) libhexagon.a $(TESTS)
 
 t/%.t:  t/%.o t/tap.o $(OBJS)
        $(CC) -I.. -I. -o $@ $+ $(LDFLAGS)
 
 t/%.t:  t/%.o t/tap.o $(OBJS)
        $(CC) -I.. -I. -o $@ $+ $(LDFLAGS)
@@ -20,13 +20,13 @@ t/%.t:  t/%.o t/tap.o $(OBJS)
 test:   $(TESTS)
        @prove --exec '' 2>/dev/null
 
 test:   $(TESTS)
        @prove --exec '' 2>/dev/null
 
-hextest:       hextest.c libhexmap.a
-       $(CC) $(CFLAGS) -o $@ hextest.c -L. -lhexmap
-
-libhexmap.a:   $(OBJS)
+libhexagon.a:  $(OBJS)
        ar r $@ $+
        ranlib $@
 
        ar r $@ $+
        ranlib $@
 
+docs:
+       make -C doc
+
 .c.o:
        $(CC) $(CFLAGS) -c -o $@ $<
 
 .c.o:
        $(CC) $(CFLAGS) -c -o $@ $<
 
diff --git a/astar.c b/astar.c
index cabe5d08c4c36dec8d0bf86490fe5e2424e4a2a2..5bfc60fc2077ae94b16d8ba24e68d7829efc899c 100644 (file)
--- a/astar.c
+++ b/astar.c
@@ -1,11 +1,11 @@
-#include "hexmap.h"
-
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <limits.h>
 #include <errno.h>
 
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <limits.h>
 #include <errno.h>
 
+#include "hexagon.h"
+
 /*
  * TODO use binary heap for the openlist and closed list.
  * open list should be a min-heap on the f value
 /*
  * TODO use binary heap for the openlist and closed list.
  * open list should be a min-heap on the f value
@@ -229,7 +229,6 @@ int HL_findpath(struct HL_astar *s, int loops) {
                        yopen = in_open(s, y);
                        if (! yopen) {
                                yopen = add_open(s, y);
                        yopen = in_open(s, y);
                        if (! yopen) {
                                yopen = add_open(s, y);
-
                                tent_better = 1;
                        } else if (cost < yopen->g) {
                                tent_better = 1;
                                tent_better = 1;
                        } else if (cost < yopen->g) {
                                tent_better = 1;
diff --git a/doc/Makefile b/doc/Makefile
new file mode 100644 (file)
index 0000000..d81d254
--- /dev/null
@@ -0,0 +1,16 @@
+hexagon.3:     hexagon.pod
+       pod2man --center='hexagon library' --section=3 --release='hexagon 0.1' $+ > $@
+
+hexagon.txt: hexagon.pod
+       pod2text -l $+
+
+hexagon.pdf:   hexagon.pod
+       pod2latex -full $+
+       pdflatex hexagon.tex
+       pdflatex hexagon.tex
+       pdflatex hexagon.tex
+       rm -f hexagon.tex hexagon.idx hexagon.aux hexagon.toc hexagon.log
+
+clean:
+       rm -f hexagon.3 hexagon.pdf hexagon.tex hexagon.txt \
+               hexagon.idx hexagon.aux hexagon.log hexagon.toc
diff --git a/doc/hexagon.pod b/doc/hexagon.pod
new file mode 100644 (file)
index 0000000..9dfc4ae
--- /dev/null
@@ -0,0 +1,177 @@
+=head1 NAME
+
+       hexlib - a library for hexagon grids
+
+=head1 SYNOPSIS
+
+       #include <hexagon.h>
+
+=head1 DESCRIPTION
+
+hexagon is a C library for performing calculations on hexagon grids.
+Hexagons are represented by their canonical ID, which is a single
+integer representation of their two dimensional coordinates.
+Algorithms and functions are provided for calculating hexagon
+centers, vertexes, and distances on the grid.  An A* algorithm
+is also implemented for pathfinding.
+
+The canonical integer id of a hexagon is computed from its
+two dimensional integer coordinates using a modified
+cantor pairing function.  The canonical integer ids are
+referred to as cantors.
+
+=head2 Grid Layout
+
+                   __/15\__/35\ 
+                __/04\__/24\__/
+               /A \__/14\__/34\
+               \__/03\__/23\__/
+               /  \__/13\__/33\
+               \__/02\__/22\__/
+               /  \__/12\__/32\
+               \__/01\__/21\__/
+               /  \__/11\__/31\
+               \__/00\__/20\__/
+                  \__/  \__/
+
+The Grid is numbered as above, and generalizes to negative coordinates,
+thus the hex labeled A would have coordinates (-1,4), and the
+hex (not shown) two hexes below 1,1 would be hex 1,-1.  If you
+need different coordinates, you will have to translate them to the
+above system.
+
+=head3 Inverted Y coordinates
+
+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.
+
+=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.
+
+=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(int x, int y)
+
+Converts a two dimensional coordinate to a canonical id.
+
+       int hex;
+       hex = HL_cantor(1,1); /* hex == 5 */
+
+=head3 int HL_cantor_x(int cantor);
+
+Returns the X coordinate of a hex identified by the given cantor id.
+
+       int x = HL_cantor_x(5); /* x == 1 */
+
+=head3 int HL_cantor_y(int cantor);
+
+Returns the Y coordinate of a hex identified by the given cantor id.
+
+       int x = HL_cantor_x(5); /* x == 1 */
+
+=head3 int HL_hexbin(double scale, double cx, double cy, int *x, int *y);
+
+This function determines which hex a given cartesian coordinate falls
+in.  The width from side to side of a hex is given by scale.  The
+function returns the cantor id of the hex.  If x or y are not NULL, the
+x and y coordinates of the hex are placed into *x and *y.
+
+If you are inverting the Y coordinate for display, do so on the result
+of this function, not the display Y coordinate you pass in to this function.
+That is, do
+
+       HL_hexbin(scale, cx, cy, &x, &y);
+       y = -y;
+
+to get the Y coordinate of the hex.  Doing the following
+
+       HL_hexbin(scale, cx, -cy, &x, &y);
+
+will give the wrong result for odd numbered hex columns.  This
+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 Pathfinding
+
+The library provides an A* implementation for pathfinding on
+a hex grid.
+
+       struct HL_astar *state;
+       state = HL_astar_init(NULL);
+       state->start = HL_cantor_xy(1,1);
+       state->goal = HL_cantor_xy(4,14);
+       int length = HL_astar_findpath(state,0);
+
+The implementation uses three callbacks which can be
+set as members of the HL_astar struct.
+
+       double (*metric)(int hex, int hex2);
+       double (*heuristic)(int hex, int hex2);
+       int (*neighbor)(int hex, int n);
+
+The metric callback is used to get the actual cost to move from
+hex to hex2.  It will only be called on adjacent hexes.  HL_astar_init
+will set this to a default metric that returns 1.0 for any input.
+
+The heuristic callback is used to estimate the distance between
+two hexes, which may not be adjacent.  By default, a function is
+used that returns the hex grid distance between the hexes.
+It is important that the function not return a distance that is
+greater than the actual distance, as the A* algorithm depends on
+this.  If you have any hexes that cost less than 1.0 to traverse,
+the default heuristic won't work, so you'll need to provide your
+own.  You could also provide a metric that multiplied the actual
+cost by an amount sufficient to raise the minimum cost to 1.0.
+(e.g. if you had roads that reduced the move cost to 1/2, you
+could provide a metric returned twice the actual cost).
+
+The neighbor callback should return the id of the neighbor
+at the index given by n.  The callback should return 0 if
+there is no neighbor with the given index.  It's ok if
+a neighbor has more than one index.  That is, you can
+return the same neighbor for more than one index.  The
+first neighbor should be at index 0.  The HL_findpath
+function will call this method starting at index 0 and
+working up until the function returns 0, at which point
+it is assumed that there are no more neighbors.
+
+=head3 struct HL_astar *HL_astar_init(HL_astar *state);
+
+Initializes the given state.  You can either pass in a pointer to your
+own data structure, or pass in NULL, in which case the library
+will use malloc(3) to create a new structure.
+
+=head3 void HL_astar_free(HL_astar *state);
+
+Frees any memory allocated by the structure.
+
+=head1 OPTIONS
+
+=head1 DIAGNOSTICS
+
+=head1 BUGS
+
+=head1 AUTHOR
+
+This library was entirely written by Nathan Wagner.
+
+=head1 COPYRIGHT
+
+This library and the source code for it are released into
+the public domain.  The author asserts no copyright claim
+of any kind.
+
+=head1 
similarity index 99%
rename from hexmap.c
rename to hexagon.c
index baeb9610599e3b7831693b0808044be650cfd244..61bb47b0fd80429ddd49e57df1e72ebb450cfc12 100644 (file)
--- a/hexmap.c
+++ b/hexagon.c
@@ -1,4 +1,4 @@
-#include "hexmap.h"
+#include "hexagon.h"
 
 #include <math.h>
 #include <stdio.h>
 
 #include <math.h>
 #include <stdio.h>
similarity index 100%
rename from hexmap.h
rename to hexagon.h
diff --git a/hexagon.pod b/hexagon.pod
deleted file mode 100644 (file)
index 4b41483..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-=head1 NAME
-
-       hexlib - a library for hexagon grids
-
-=head1 SYNOPSIS
-
-       #include <hexagon.h>
-
-=head1 DESCRIPTION
-
-hexagon is a C library for performing calculations on hexagon grids.
-Hexagons are represented by their canonical ID, which is a single
-integer representation of their two dimensional coordinates.
-Algorithms and functions are provided for calculating hexagon
-centers, vertexes, and distances on the grid.  An A* algorithm
-is also implemented for pathfinding.
-
-The canonical integer id of a hexagon is computed from its
-two dimensional integer coordinates using a modified
-cantor pairing function.  The canonical integer ids are
-referred to as cantors.
-
-=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(int x, int y)
-
-Converts a two dimensional coordinate to a canonical id.
-
-       int hex;
-       hex = HL_cantor(1,1); /* hex == 5 */
-
-=head3 int HL_cantor_x(int cantor);
-
-Returns the X coordinate of a hex identified by the given cantor id.
-
-       int x = HL_cantor_x(5); /* x == 1 */
-
-=head2 Pathfinding
-
-The library provides an A* implementation for pathfinding on
-a hex grid.
-
-       struct HL_astar *state;
-       state = HL_astar_init(NULL);
-       state->start = HL_cantor_xy(1,1);
-       state->goal = HL_cantor_xy(4,14);
-       int length = HL_astar_findpath(state,0);
-
-
-=head3 struct HL_astar *HL_astar_init(HL_astar *state);
-
-Initializes the given state.  You can either pass in a pointer to your
-own data structure, or pass in NULL, in which case the library
-will use malloc(3) to create a new structure.
-
-=head3 void HL_astar_free(HL_astar *state);
-
-Frees any memory allocated by the structure.
-
-=head1 OPTIONS
-
-=head1 DIAGNOSTICS
-
-=head1 BUGS
-
-=head1 AUTHOR
-
-This library was entirely written by Nathan Wagner.
-
-=head1 COPYRIGHT
-
-This library and the source code for it are released into
-the public domain.  The author asserts no copyright claim
-of any kind.
-
-=head1 
index 95750cfb9b19425d41540a7e43d2355d388fa6ce..dc2c64fbb8c6bce8b3f89093783338570b82de7f 100644 (file)
@@ -1,4 +1,4 @@
-#include "hexmap.h"
+#include "hexagon.h"
 
 #include "tap.h"
 
 
 #include "tap.h"
 
index af88268d4212af1c23484843108e5aeeb4144d81..2ed40c07fc9072cf823c81306fc677d74d287751 100644 (file)
--- a/t/astar.c
+++ b/t/astar.c
@@ -1,6 +1,6 @@
 #include <stdio.h>
 
 #include <stdio.h>
 
-#include "hexmap.h"
+#include "hexagon.h"
 
 #include "tap.h"
 
 
 #include "tap.h"
 
@@ -27,10 +27,9 @@ void pcheck(struct HL_astar *p, int x1, int y1, int x2, int y2, int expect) {
 int neighbor55(int hex, int dir) {
        int neighbor;
 
 int neighbor55(int hex, int dir) {
        int neighbor;
 
-       neighbor = HL_adjacent_hex(hex, dir);
-       if (neighbor == HL_cantor_xy(5,5)) {
-               return -1;
-       }
+       do {
+               neighbor = HL_adjacent_hex(hex, dir++);
+       } while (neighbor == HL_cantor_xy(5,5));
 
        return neighbor;
 }
 
        return neighbor;
 }
index a2e929087ea859b07937a024b5889c6c3b3f40bc..0f179d20e1c724b8b06ecc8c63648f268d6727f3 100644 (file)
@@ -1,6 +1,6 @@
 #include <stdio.h>
 
 #include <stdio.h>
 
-#include "hexmap.h"
+#include "hexagon.h"
 
 #include "tap.h"
 
 
 #include "tap.h"
 
index 00fdff2a056c0d80a4c11f25134deb3fff9463b7..72499a3afd557512062cd76cde01e5b87544780e 100644 (file)
@@ -1,6 +1,6 @@
 #include <math.h>
 
 #include <math.h>
 
-#include "hexmap.h"
+#include "hexagon.h"
 
 #include "tap.h"
 
 
 #include "tap.h"
 
index 28d520e89c235ca0fbe1ca06c976aa1a872488eb..71d7079a0af5143f026865ee083279fe18abe26d 100644 (file)
@@ -1,4 +1,4 @@
-#include "hexmap.h"
+#include "hexagon.h"
 
 #include "tap.h"
 
 
 #include "tap.h"
 
index 784e4678d254dc82be3cdd844f2b2562116943a3..f4335fd291a412d9cc0cfd6879a6bcb8ff1694db 100644 (file)
@@ -1,4 +1,4 @@
-#include "hexmap.h"
+#include "hexagon.h"
 
 #include "tap.h"
 
 
 #include "tap.h"
 
index e486bc8f40442c636adb03a0406a4f112a33d582..5fc504d987a5e310ad4d89b09c8a543daebc902b 100644 (file)
@@ -1,4 +1,4 @@
-#include "hexmap.h"
+#include "hexagon.h"
 
 #include "tap.h"
 
 
 #include "tap.h"
 
index f7b219b9b5bd75decfd4168bd676b4929de88c07..2f4332838f0a86fa2b5e7809353feadb02356e7c 100644 (file)
@@ -4,7 +4,7 @@
 #include <limits.h>
 #include <math.h>
 
 #include <limits.h>
 #include <math.h>
 
-#include "hexmap.h"
+#include "hexagon.h"
 
 #include "tap.h"
 
 
 #include "tap.h"
 
index b40b86d66837b3356da9aa9ca4426bf10243d84d..7a449e61df386ab65bb84f977ecb9a77a5be917e 100644 (file)
--- a/t/range.c
+++ b/t/range.c
@@ -4,7 +4,7 @@
 #include <limits.h>
 #include <math.h>
 
 #include <limits.h>
 #include <math.h>
 
-#include "hexmap.h"
+#include "hexagon.h"
 
 #include "tap.h"
 
 
 #include "tap.h"
 
index 143b7a864df5b869f4fa35e4ceca1f393656d8f7..9294b8bede88a7fe55bd97ae3e9c3fe6f27b1503 100644 (file)
@@ -1,4 +1,4 @@
-#include "hexmap.h"
+#include "hexagon.h"
 
 #include "tap.h"
 
 
 #include "tap.h"