Directions and Offsets

Name

Directions and Offsets -- Finding the Displacements Between Hexes

Synopsis


#include <hexmap.h>



enum        HexDirection;
typedef     HexDirectionSimple;

#define     HEX_DIRECTION_SIMPLE_GET_OPPOSITE(dir)
#define     HEX_DIRECTION_SIMPLE_ROTATE_RIGHT(dir)
#define     HEX_DIRECTION_SIMPLE_ROTATE_LEFT(dir)
#define     HEX_DIRECTION_SIMPLE_NUM        (i)
#define     HEX_DIRECTION_SIMPLE_GET_MOD    (dir)

void        hex_direction_simple_get_hex    (HexDirectionSimple direction,
                                             gint distance,
                                             gint old_x,
                                             gint old_y,
                                             gint *new_x,
                                             gint *new_y);
HexDirection hex_direction_rotate           (HexDirection direction,
                                             gint rot);

typedef     HexOffset;

void        hex_offset_get_hex              (HexOffset *offset,
                                             gint old_x,
                                             gint old_y,
                                             gint *new_x,
                                             gint *new_y);
void        hex_offset_rotate               (HexOffset *offset,
                                             gint rot);
void        hex_offset_add                  (HexOffset *off1,
                                             HexOffset *off2);
gboolean    hex_offset_equal                (HexOffset *off1,
                                             HexOffset *off2);
guint       hex_offset_get_distance         (HexOffset *offset);
void        hexes_get_offset                (gint x1,
                                             gint y1,
                                             gint x2,
                                             gint y2,
                                             HexOffset *offset);
guint       hexes_get_distance              (gint x1,
                                             gint y1,
                                             gint x2,
                                             gint y2);

Description

This section describes HexDirection, HexDirectionSimple, and HexOffset, which are useful for describing the orientations and relative positions of things on the grid. A function for calculating the distance between hexes is also included.

Details

enum HexDirection

typedef enum {
  HEX_DIRECTION_N	= 0x1,
  HEX_DIRECTION_NE	= 0x2,
  HEX_DIRECTION_SE	= 0x4,
  HEX_DIRECTION_S	= 0x8,
  HEX_DIRECTION_SW	= 0x10,
  HEX_DIRECTION_NW	= 0x20,
  HEX_DIRECTION_MASK	= 0x3f,
  HEX_DIRECTION_NONE	= 0
} HexDirection;

A set of bit flags for describing the faces of a hex


HexDirectionSimple

One of the six simple flags in HexDirection (i.e. one of N, NE, SE, S, SW, or NW) or HEX_DIRECTION_NONE


HEX_DIRECTION_SIMPLE_GET_OPPOSITE()

#define     HEX_DIRECTION_SIMPLE_GET_OPPOSITE(dir)

returns the opposite direction to dir

dir :

a HexDirectionSimple


HEX_DIRECTION_SIMPLE_ROTATE_RIGHT()

#define     HEX_DIRECTION_SIMPLE_ROTATE_RIGHT(dir)

returns the direction to the right of dir

dir :

a HexDirectionSimple


HEX_DIRECTION_SIMPLE_ROTATE_LEFT()

#define     HEX_DIRECTION_SIMPLE_ROTATE_LEFT(dir)

returns the direction to the left of dir

dir :

a HexDirectionSimple


HEX_DIRECTION_SIMPLE_NUM()

#define     HEX_DIRECTION_SIMPLE_NUM(i)

converts an integer to a direction, with 0 being HEX_DIRECTION_N, and increasing integers corresponding to clockwise rotation

i :


HEX_DIRECTION_SIMPLE_GET_MOD()

#define     HEX_DIRECTION_SIMPLE_GET_MOD(dir)

returns the modulus of dir, where dir is (1 << modulus)

dir :

a HexDirectionSimple, excluding HEX_DIRECTION_NONE


hex_direction_simple_get_hex ()

void        hex_direction_simple_get_hex    (HexDirectionSimple direction,
                                             gint distance,
                                             gint old_x,
                                             gint old_y,
                                             gint *new_x,
                                             gint *new_y);

Find the hex which lies a certain distance in a certain direction from a given hex

direction :

a HexDirectionSimple, pointing from the old to the new hex

distance :

the distance from the old to the new hex

old_x :

the x coordinate of the old hex

old_y :

the y coordinate of the old hex

new_x :

a location to return the x coordinate of the new hex

new_y :

a location to return the y coordinate of the new hex


hex_direction_rotate ()

HexDirection hex_direction_rotate           (HexDirection direction,
                                             gint rot);

rotate a HexDirection

direction :

a HexDirection

rot :

the angle to rotate the direction. Positive is clockwise, and a rotation of 6 returns the original direction

Returns :

the rotated HexDirection


HexOffset

A structure representing an offset on the grid. Essentially a two dimensional vector represented in grid coordinates


hex_offset_get_hex ()

void        hex_offset_get_hex              (HexOffset *offset,
                                             gint old_x,
                                             gint old_y,
                                             gint *new_x,
                                             gint *new_y);

Find the hex which lies a certain offset from a given hex

offset :

the offset from the old to the new hex

old_x :

the x coordinate of the old hex

old_y :

the y coordinate of the old hex

new_x :

a location to return the x coordinate of the new hex

new_y :

a location to return the y coordinate of the new hex


hex_offset_rotate ()

void        hex_offset_rotate               (HexOffset *offset,
                                             gint rot);

rotate a HexOffset

offset :

a HexOffset

rot :

the angle to rotate the offset. Positive is clockwise, and a rotation of 6 returns the original offset


hex_offset_add ()

void        hex_offset_add                  (HexOffset *off1,
                                             HexOffset *off2);

adds off1 and off2, storing the result in off1

off1 :

a HexOffset

off2 :

a HexOffset


hex_offset_equal ()

gboolean    hex_offset_equal                (HexOffset *off1,
                                             HexOffset *off2);

checks whether off1 and off2 are equivalent

off1 :

a HexOffset

off2 :

a HexOffset

Returns :

TRUE if the offsets are equal, FALSE otherwise


hex_offset_get_distance ()

guint       hex_offset_get_distance         (HexOffset *offset);

find the length of a HexOffset

offset :

a HexOffset

Returns :

the minimum distance, in hexes, from one end of offset to the other


hexes_get_offset ()

void        hexes_get_offset                (gint x1,
                                             gint y1,
                                             gint x2,
                                             gint y2,
                                             HexOffset *offset);

Find the offset between two hexes

x1 :

the x coordinate of the first hex

y1 :

the y coordinate of the first hex

x2 :

the x coordinate of the second hex

y2 :

the y coordinate of the second hex

offset :

a location to return the offset from the first hex to the second


hexes_get_distance ()

guint       hexes_get_distance              (gint x1,
                                             gint y1,
                                             gint x2,
                                             gint y2);

Find the minimum distance in hexes between two hexes

x1 :

the x coordinate of the first hex

y1 :

the y coordinate of the first hex

x2 :

the x coordinate of the second hex

y2 :

the y coordinate of the second hex

Returns :

the distance