0

My Grid
https://www.redblobgames.com/grids/hexagons/

I have this beautiful hexagonal pattern and I need to somehow implement it in java. I already have a Class "Tile" representing one hexagon and a class "Vector" for the coordinates of each Tile. Each Tile Object has its own vector.

For now I just want to add all the tiles to a list. But to do that I need to create all 37 of them. This is the point where I'm stuck right now. I could of course create all by hand, but not only would that be tedious and prone to error but also not very elegant.

Can anyone of you give me a clue on what I could do? :)

5
  • The easiest way seems to be to use two nested loops, one going over the rows, the other over the tiles within a row. Commented Mar 13, 2018 at 18:01
  • I'm not sure I'd start with a list. You want all the tiles within a certain distance (Manhattan distance) from the origin. I think you'll need to take into consideration the actual geometry of the shape. Commented Mar 13, 2018 at 18:01
  • I take that back. It's not Manhattan distance. You're just moving a maximum of three steps +-3 from the origin on each axis. Commented Mar 13, 2018 at 18:06
  • The problem with two nested loops seems to be, that I would need 7 times two nested loops. One for each row. Which for now seems to be the best option. But maybe the "perfect" solution is somewhere out there :) Commented Mar 13, 2018 at 18:16
  • Do you need further help with this? I can add to my answer to explicitly create all the Tiles (assigning x/y/z) if you wish? Commented Mar 21, 2018 at 14:51

2 Answers 2

1

I think the first thing you need to do is link up your Tiles. Give them six references, one for each potential neighbor - Northwest, Northeast, East, Southeast, Southwest, and West. Then, given any tile, you can easily traverse all of its neighbors. Once you've created this graph of Tile objects, assign them Vector instances.

Assuming the Vector for each Tile is unique, the next step is to create a function that maps each of those Vector instances to cartesian coordinates. At that point, you could draw the points to see if you're putting tiles in the correct spaces on the screen.

Judging by the link you posted, it sounds like you're trying to make some kind of game (which is cool!). If that's the case, you have two things left to do after you get your vector2Cart() function working:

  1. Render your hexagonal tiles "properly", not just as points or squares.

  2. Create the inverse cartToVector() method that takes cartesian coordinates and returns the Vector corresponding to the Tile that occupies that part of the screen. This way you can click on the screen and do stuff to the Tile.

Sign up to request clarification or add additional context in comments.

Comments

1

To build the grid, take advantage of the geometry/symmetry. Think of the grid in terms of depth, where the center is depth 0, and each successive layer has increasing depth. The grid above has depth = 3.
Each level has constant properties related to its depth (each has 6 sides, and the length of each side is proportional to the depth).

The following code will help build the grid:

public class HexagonGrid {

    public static void main(String[] args) {

       int size = 3;
       buildHexagonGrid(size);
   }

    private static void buildHexagonGrid(int size) {
       int totalTiles = 0;
       int depth = 0;
       // TODO:  Build the center tile here
       totalTiles++;    // Center
       System.out.printf("Current size (after depth=%d) = %d\n", depth, totalTiles);
       for (depth=1; depth<=size; depth++) {
          for (int side=1; side<=6; side++) {
             for (int tile=1; tile<=depth; tile++)
                // TODO: Build the Tiles along current side/depth
                // There is a clear relationship between x/y/z and depth/side/tile
                totalTiles++;
          } 
             System.out.printf("Current size (after depth=%d) = %d\n", depth, totalTiles);
       }
    }
}

Showing how many tiles are created at each layer:

Current size (after depth=0) = 1
Current size (after depth=1) = 7
Current size (after depth=2) = 19
Current size (after depth=3) = 37

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.