Algorithms

Generating Hexagons

Generating a tile set of Hexagons is not as simple as generating squares. There is a very specific offset needed to place hexagons properly. It is helpful to measure our offsets in the lengths of a side of one hexagon. We will call this a hex side. We can also think of each hexagon as being made of of six equilateral triangles (three equal sides), each side of these triangles is one hex side long. If we have the hexagons oriented as they are below, then the vertical offset between each hexagon will be 1 1/2 hex sides.

The Horizontal offset is the problem child. If we go back to thinking of each hexagon as being six equilateral triangles, we can find the horizontal offset by creating a right triangle and using the pythagorean theorem to find our missing side. It comes out to the square root of 3/4 which is also equal to the square root of 3 over 2. It also has another name which just rolls off of the tongue.

0.866025403784

I will refer to this number as Yoff from now on to save us the trouble.

Here are the offsets required to reach every adjacent hexagon.

Cell Generation

Our generation is split up into different chunks which we call combs, in which there are smaller, individual cells. It is treated as a 2D Array. The size of each comb is set to 10×10. New chunks will be added on when the camera is in range and chunks will be deleted when they are out of range.

 public void generate(Random rand)
        {
            for(int y = 0;y<SIZEY;y++)
            {
                for (int x = 0; x < SIZEX; x++)
                {
                    comb[y,x] = new Hex();
                    int rng = rand.Next(randSIZE);
                    if (rng > 5)
                    {
                        if (rng == 7 || rng == 6)
                            comb[y,x].setType(1);
                        else if (rng == 8)
                            comb[y, x].setType(2);
                        else
                            comb[y, x].setType(3);
                    }
                }
            }    

The setType quantifier sets individual cells to have different attributes randomly., such as having larvae, honey or nectar. Below are the results of the type generation with seeds 9, 10 and 15 (left to right). To place the cells, their array coordinates are multiplied by the offsets to reach their spot.

2(Yoff)(x-coord) for horizontal placement if y-coord is even

2(Yoff)(x-coord) + Yoff for horizontal placement if y-coord is odd

1.5(y-coord) for vertical placement

css.php