Macros

Name

Macros -- Macros for Determining the Dimensions of a Hex

Synopsis


#include <hexmap.h>



#define     HEX_SQRT3
#define     HEX_HALF_HEIGHT                 (width)
#define     HEX_HEIGHT                      (width)
#define     HEX_QUARTER_WIDTH               (width)
#define     HEX_MIDDLE_HALF_WIDTH           (width)
#define     HEX_THREE_QUARTER_WIDTH         (width)

#define     HEX_DRAW_X                      (x, width)
#define     HEX_DRAW_Y                      (x, y, width, height)

#define     HEX_TOP_LEFT_X_OFFSET           (width)
#define     HEX_BOTTOM_LEFT_X_OFFSET        (width)
#define     HEX_TOP_RIGHT_X_OFFSET          (width)
#define     HEX_BOTTOM_RIGHT_X_OFFSET       (width)
#define     HEX_MIDDLE_LEFT_X_OFFSET        (width)
#define     HEX_MIDDLE_RIGHT_X_OFFSET       (width)

#define     HEX_TOP_LEFT_Y_OFFSET           (height)
#define     HEX_BOTTOM_LEFT_Y_OFFSET        (height)
#define     HEX_MIDDLE_LEFT_Y_OFFSET        (height)
#define     HEX_TOP_RIGHT_Y_OFFSET          (height)
#define     HEX_BOTTOM_RIGHT_Y_OFFSET       (height)
#define     HEX_MIDDLE_RIGHT_Y_OFFSET       (height)

typedef     HexPoint;
gboolean    hex_point_in                    (guint width,
                                             gint x,
                                             gint y);
void        hex_get_hex_from_coords         (guint hex_width,
                                             gint x,
                                             gint y,
                                             gint *x_hex,
                                             gint *y_hex);
guint       hex_set_points                  (HexPoint *points,
                                             guint width);

Description

These macros are used to determine the dimensions of a hex from its width. The width was chosen instead of the height because, while the height is always even, there is no such constraint on the width. The most important dimensions are displayed in Figure 1.

When generating images of hexes, the utility programs hex_sizes and hex_blank are useful. hex_sizes prints the height of a hex of a given width. It takes either one or two command line arguments, printing either a single width or a range of widths. hex_blank prints an xpm with a hexagonal mask to stdout. It takes one argument, the width of the hex.

Figure 1. Distances on a Hex Given by Various Macros

Details

HEX_SQRT3

#define     HEX_SQRT3

A defined constant equal to the square root of 3.


HEX_HALF_HEIGHT()

#define     HEX_HALF_HEIGHT(width)

A macro which computes half of the height of a hex from its width. Due to graphical constraints, the heights of the top and bottom halves of a hex are always equal.

This macro involves floating point math.

width :

the width of the hex


HEX_HEIGHT()

#define     HEX_HEIGHT(width)

A macro which computes the height of a hex from its width. Due to graphical constraints, the height is always even.

This macro involves floating point math.

width :

the width of the hex


HEX_QUARTER_WIDTH()

#define     HEX_QUARTER_WIDTH(width)

A macro which computes the width of the left- and right-most quarters of a hex from its full width. Due to graphical constraints, these widths are always equal.

width :

the width of the hex


HEX_MIDDLE_HALF_WIDTH()

#define     HEX_MIDDLE_HALF_WIDTH(width)

A macro which computes the width of the center half of a hex from its full width.

width :

the width of the hex


HEX_THREE_QUARTER_WIDTH()

#define     HEX_THREE_QUARTER_WIDTH(width)

A macro which computes the combined width of the middle half and one of the left or right quarters of a hex from its full width.

width :

the width of the hex


HEX_DRAW_X()

#define     HEX_DRAW_X(x, width)

A macro which computes the x coordinate of the clip box of a hex placed in a particular location on a grid.

x :

the x coordinate of the hex in the grid

width :

the width of the hex


HEX_DRAW_Y()

#define     HEX_DRAW_Y(x, y, width, height)

A macro which computes the y coordinate of the clip box of a hex placed in a particular location on a grid.

x :

the x coordinate of the hex in the grid

y :

the y coordinate of the hex in the grid

width :

the width of the hex

height :

the height of the hex, computed by HEX_HEIGHT()


HEX_TOP_LEFT_X_OFFSET()

#define     HEX_TOP_LEFT_X_OFFSET(width)

A macro which computes the x offset from the upper left corner of a hex's clip box to the top left corner of the hex.

width :

the width of the hex


HEX_BOTTOM_LEFT_X_OFFSET()

#define     HEX_BOTTOM_LEFT_X_OFFSET(width)

A macro which computes the x offset from the upper left corner of a hex's clip box to the bottom left corner of the hex.

width :

the width of the hex


HEX_TOP_RIGHT_X_OFFSET()

#define     HEX_TOP_RIGHT_X_OFFSET(width)

A macro which computes the x offset from the upper left corner of a hex's clip box to the top right corner of the hex.

width :

the width of the hex


HEX_BOTTOM_RIGHT_X_OFFSET()

#define     HEX_BOTTOM_RIGHT_X_OFFSET(width)

A macro which computes the x offset from the upper left corner of a hex's clip box to the bottom right corner of the hex.

width :

the width of the hex


HEX_MIDDLE_LEFT_X_OFFSET()

#define     HEX_MIDDLE_LEFT_X_OFFSET(width)

A macro which computes the x offset from the upper left corner of a hex's clip box to the middle left corner of the hex.

width :

the width of the hex


HEX_MIDDLE_RIGHT_X_OFFSET()

#define     HEX_MIDDLE_RIGHT_X_OFFSET(width)

A macro which computes the x offset from the upper left corner of a hex's clip box to the middle right corner of the hex.

width :

the width of the hex


HEX_TOP_LEFT_Y_OFFSET()

#define     HEX_TOP_LEFT_Y_OFFSET(height)

A macro which computes the y offset from the upper left corner of a hex's clip box to the top left corner of the hex.

height :

the height of the hex, computed by HEX_HEIGHT()


HEX_BOTTOM_LEFT_Y_OFFSET()

#define     HEX_BOTTOM_LEFT_Y_OFFSET(height)

A macro which computes the y offset from the upper left corner of a hex's clip box to the top bottom corner of the hex.

height :

the height of the hex, computed by HEX_HEIGHT()


HEX_MIDDLE_LEFT_Y_OFFSET()

#define     HEX_MIDDLE_LEFT_Y_OFFSET(height)

A macro which computes the y offset from the upper left corner of a hex's clip box to the top middle corner of the hex.

height :

the height of the hex, computed by HEX_HEIGHT()


HEX_TOP_RIGHT_Y_OFFSET()

#define     HEX_TOP_RIGHT_Y_OFFSET(height)

A macro which computes the y offset from the upper left corner of a hex's clip box to the top right corner of the hex.

height :

the height of the hex, computed by HEX_HEIGHT()


HEX_BOTTOM_RIGHT_Y_OFFSET()

#define     HEX_BOTTOM_RIGHT_Y_OFFSET(height)

A macro which computes the y offset from the upper left corner of a hex's clip box to the bottom right corner of the hex.

height :

the height of the hex, computed by HEX_HEIGHT()


HEX_MIDDLE_RIGHT_Y_OFFSET()

#define     HEX_MIDDLE_RIGHT_Y_OFFSET(height)

A macro which computes the y offset from the upper left corner of a hex's clip box to the middle right corner of the hex.

height :

the height of the hex, computed by HEX_HEIGHT()


HexPoint

A point in two dimensions


hex_point_in ()

gboolean    hex_point_in                    (guint width,
                                             gint x,
                                             gint y);

check if a point is inside a hex

width :

The width of a hex in pixels. The height of the hex is given by HEX_HEIGHT().

x :

The x coordinate of a point which might lie in the hex

y :

The y coordinate of a point which might lie in the hex

Returns :

TRUE if the point is inside, FALSE otherwise


hex_get_hex_from_coords ()

void        hex_get_hex_from_coords         (guint hex_width,
                                             gint x,
                                             gint y,
                                             gint *x_hex,
                                             gint *y_hex);

Find the coordinates of the hex that a pixel is located in.

hex_width :

The width of a hex in pixels. The height of the hex is given by HEX_HEIGHT().

x :

The distance in pixels from the left side of the grid.

y :

The distance in pixels from the top of the grid.

x_hex :

A location to return the x coordinate of the hex.

y_hex :

A location to return the y coordinate of the hex.


hex_set_points ()

guint       hex_set_points                  (HexPoint *points,
                                             guint width);

Sets points to the coordinates of the corners of the hex, starting at the upper left and going clockwise.

points :

An array of HexPoint of size 6

width :

The width of the hex

Returns :

The height of the hex. If this is zero, points was not set.