Create / create hexagonal grid in C

So, I'm trying to make a hexagonal grid in C for the game. I'm really dumb, based on where to start. Everyone has ideas.

EDIT: I need about 15-20 capture-shaped hexagons, all connected, kind of like a playing field. for the game I'm working on. Sorry for the ambiguity.

0
source share
2 answers

That's right. Despite their odd shape, the hexagons can still be contained in your usual multidimensional array for future use (I suppose you'll want to put things in your hexagons). As for drawing them, it's simple. The sum of the angles = (6 - 2) * 180 = 4 * 180 = 720. One angle is 720/6 = 120 degrees. First, calculate the leftmost angular position of Y equal to √(hexagonSide - hexagonWidth * hexagonWidth) . I'm sure you can understand hexagonWidth , right? So, now the position of X relative to the last one will be 0. You will need to compensate for the position of Y half the height of the hexagon in front of it, up or down, depending on whether row * col even or odd. Since you know the width of the hexagon, you have the coordinates of the opposite corner. Turn 120 ° and repeat.

Before continuing, should it be in the console? Or is it real graphics?

+1
source

So, let's get it straight, will the game play on the console? That's right, now you will need to configure your data structures, the most obvious is the nodes.

Knots

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 node structure

Imagine the following hexagon:

  /\ | | \/ 

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

 // 0 1 2 3 // 4 5 6 7 8 // 9 ABC 

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

Connection nodes

First of all, let me point out that every even line (0, 2, 4, ...) will have width-1 elements. But there is more, each element (x, y) in this line will refer to the next 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 of other lines, such as { 4, 5, 6, 7, 8 }, will have width elements, where the element (x, y) refers 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 associate (x1, y1) with (x2, y2), make sure that 0 <= x < width and 0 <= y < height .

Remember ...

Your array contains one unused element at the end of every two lines (line [0], line [2], etc.). You can also provide them to everyone with a label or index so you can reference them. You can mark them as pairs (x, y) or numerically by their index, it all depends on you. The pair (x, y) is very simple for you, as it will map directly to the array in which they are stored.

+1
source

All Articles