mushi
mushi

Reputation: 21

generating/creating hexagon grid in C

So i m trying to make hexagonal grid in C for a game. I am really dumb founded on where to start on it. ANyone have any ideas.

EDIT: I need about 15-20 hexagons in a grip shape all joined,something like a game board. for a game i m working on. Sorry for not being clear

Upvotes: 1

Views: 4611

Answers (2)

Keldon Alleyne
Keldon Alleyne

Reputation: 2130

So let's get this straight, the game will be played on the console? Right, well now you will need to set up your data structures, the most obvious is with nodes.

The nodes

Each hexagon is a node with six edges.

typedef struct Node {
  void *object; /* Pointer to object */
  Node *node_array; /* Pointer to node_array with 'node_count' nodes */
  int node_count; /* size of node_array */
} Node;

How to initialize and connect the node structure

Imagine the following hexagon:

 /\
|  |
 \/

It has the following edges, NORTHEAST, EAST, SOUTHEAST, SOUTHWEST, WEST and NORTHWEST. Next observe how they will be arranged (10, 11 and 12 were represented in Hex so that they can fit in one space):

//  0 1 2 3
// 4 5 6 7 8
//  9 A B C

So 0 will link to 5 through it's SOUTHEAST link, and 4 through it's SOUTHWEST link. Also notice how the rows alternate between odd and even numbers of elements. Let's call {0, 1, 2, 3} row[0], and {4, 5, 6, 7, 8} row[1]. And let's call this a 5x3 hexmap. The easiest way to create this array is with malloc(sizeof(Node) * width * height).

Connecting the nodes

First of all let me point out that every even row (0, 2, 4, ...) will have width-1 elements. But there's more, each element (x, y) on this row will link to the following element in your array:

  • (x+1, y-1) - NORTHEAST
  • (x+1, y) - EAST
  • (x+1, y+1) - SOUTHEAST
  • (x, y+1) - SOUTHWEST
  • (x-1, y) - WEST
  • (x, y-1) - NORTHWEST

Elements on the other rows, such as {4, 5, 6, 7, 8} will have width elements, where element (x, y) links to the following:

  • (x, y-1) - NORTHEAST
  • (x+1, y) - EAST
  • (x, y+1) - SOUTHEAST
  • (x-1, y+1) - SOUTHWEST
  • (x-1, y) - WEST
  • (x-1, y-1) - NORTHWEST

When trying to link (x1,y1) with (x2, y2), ensure that 0 <= x < width and 0 <= y < height.

Remember ...

Your array contains one unused element at the end of every two rows (row[0], row[2], etc.). Also you might want to provide them all with some sort of label or index so that you can refer the player to them. You could label them as (x,y) pairs, or numerically by their index, it's all up to you. The (x, y) pair is very easy for you since that will map directly to the array they are stored in.

Upvotes: 1

Ry-
Ry-

Reputation: 224904

Absolutely. Despite their odd shape, hexagons can still be contained in your usual multidimensional array, for future use (I assume you'll want to put things in your hexagons). As for drawing them, it's simple. Sum of angles = (6 - 2) * 180 = 4 * 180 = 720. One angle is 720 / 6 = 120 degrees. Calculate first the leftmost angle's Y position, which is equal to √(hexagonSide - hexagonWidth * hexagonWidth). I'm sure you can figure out hexagonWidth, right? Okay, now the X position relative to the last one will be 0. You'll need to offset the Y position by half the height of the hexagon before it, up or down depending on whether row * col is even or odd. Since you know the hexagon's width you have the coordinates of the opposite angle. Rotate by 120° and repeat.

Before I continue, is this supposed to be in the console? Or is it real graphics?

Upvotes: 2

Related Questions