Hexagon Grid - return neighbors

Asked

Viewed 213 times

12

I am writing a simulator of a game called Iso-Path. It consists of a hexagonal grid, being itself formed of hexagons

You can see what I’ve done on this link

But I stopped in time to catch the neighbors of a certain cell.

For example, cell 5 has 6 neighbors (0, 1, 4, 6, 10, 11)

A grade

So let’s go to the code

var tabuleiro = "4567654";

podeMover(5, 0); // => true
podeMover(5, 1); // => true
podeMover(5, 4); // => true
podeMover(5, 7); // => false (Não são vizinhos)

A cell on the horizontal edge would also have as neighbor the cell on the other horizontal edge. Soon...

podeMover(22, 27); // true

Vizinhos + teletransporte

I might as well create an array with each neighbor manually, but I would like to know how to do this procedurally.

The board would always be a hexagon composed of smaller hexagons, so valid boards would be represented by strings such as (where each number describes the amount of smaller hexagons in a row of the larger hexagon, indicated from left to right as they appear from top to bottom in the figure):

"3456543"
"4567654" // Padrão Iso-path
"567898765" // ... etc
  • 2

    I know it’s not the answer you’re looking for, but when I’ve been reading a little bit about this topic, I found this site very useful, a lot of information about game development, sending the link to the neighbors part, might have something to help you find the https solution://www.redblobgames.com/grids/hexagons/#Neighbors (in English)

  • 3

    If you can tell me what the structure that stores the hexagon looks like, I might even try to help. For example, if it is line x position, if it is left or center aligned. etc.(remembering that in most cases a map of hexagons is the same thing as a square, with the only difference that at each row or column an axis is moved in 50% of the unit)

  • @But in this case each line has a size. And as the goal is to catch the neighbors, I believe it is important that hexagons have more edges (therefore, more neighbors).

  • @bfavaretto example of checkered with 6 neighbors, coordinates X and Y only: https://i.stack.Imgur.com/Twfvy.png (the "secret" is the offset). So I asked for the structure, because depending on the way it stores it has different ways of doing it. Depending on the case the length of the line can make a difference, depending not. Now, of course I also find important an answer that considers only the number of the square, for it to be canonical (still, in this ideal case, somewhere we have to store the size of the hexagon, either by the side).

  • 1

    These rules hinder a little, because we do not know if it is only horizontal, vertical, and runs away from what would be natural (go to the opposite side, and not "mirror"): "A cell on the horizontal edge would also have the cell on the other horizontal edge. Soon..."

2 answers

1

I think there are two possible solutions;

1) find a mathematical formula that, based on the size of the super-hexagon, returns the numbers of the neighboring hexagons. It would be very fast, but not very flexible.

2) Use a structure similar to a double-chained list, but with 6 links instead of two:

class Hexagono {
     int numero;
     Hexagono *uma_hora;
     Hexagono *tres_horas;
     Hexagono *cinco_horas;
     Hexagono *sete_horas;
     Hexagono *nove_horas;
     Hexagono *onze_horas;
}

(I used C++ to illustrate the fact that Hexagon is an object and that the links from the hexagon to the neighbors must be references, not values, but the idea is readily applicable to any other language.)

It would take a certain amount of work to create the hexagons, because it would be necessary to assign their "neighbors", but once created, it is easy to know how many and which neighbors each have.

0

So, I don’t know if you noticed, but the neighbor’s identification is pretty simple, in the case of the chosen cell, the neighbor’s value will always be the
value_cell - 5, value_cell - 4, value_cell - 1, value_cell + 1, cell + 5 and cell + 6.
I don’t know how your cells are inserted, but if you are using double-chained rows, it would be easy for you to take the cells that have no neighbor, making a comparison like se existe(valor_da_célula - 1) resulta em falso are it will replace the position of the cell that would be in that position by the next cell that has no cell in the position valor_da_célula + 1.
I sincerely hope to have helped you in some way. If not, explain how the cells are being inserted so we can help you better. Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.