doofesohr
doofesohr

Reputation: 187

Creating a hexagonal grid pattern

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? :)

Upvotes: 0

Views: 3284

Answers (2)

Ian Mc
Ian Mc

Reputation: 5829

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

Upvotes: 1

CodeBlind
CodeBlind

Reputation: 4569

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.

Upvotes: 1

Related Questions