Search code examples
delphidelphi-xe2hexagonal-tiles

Calculation of Hex grid


What i would like to do is create a hex grid like below.

Hex grid

I can draw the hex but unsure how to get them to stagger. So first row should be (0,0) (0,1) (0,2) (0,3) in the image and the second row should be (1,0) (1,1) (1,2) (1,3) ect Where the even hex in the row is always staggered down.

Currently I know the number of rows / columns to use hexRows hexColumns

and how i draw the hex is like this, i is just there to number the hex when its drawn. DrawSolidHex(x,y,i);

current code looks like this

 begin
 hexcolumns := c;
 hexrows := r;
 i:=1;             //first cube
 x := 1;           //default x,y,z values
 y := 1;
 z := 0;
   while hexrows>0 do
    begin
      columnssave := hexcolumns ;
      while hexcolumns >0 do
        begin
          DrawSoildHex(X,Y,i);
          i:=i+1;
          x := x + 1; 
          hexcolumns := hexcolumns -1;
        end;
      y:=y+1;
      x:= 1;
      z:=0;
      hexcolumns := columnssave;
      hexrows:= hexrows-1;
    end;
    totalhex := i;

With this code currently it will just draw the hex x,y in a stright line,(1,1) (2,1) (3,1)..ect I am unsure how to add the offsets ,calculate them, and code it so it would stagger the rows.


Solution

  • In a hexagon as follows, you need to know about the triangles that make up the edges:

        B                   B
        *--*                *
       /|   \              /|
      / |    \            / |
    A*--*C    *  ==>   c /  | a
      \      /          /   |
       \    /          /    |
        *--*         A*-----*C
                         b
    

    These triangles are known as 30-60-90 triangles because that's the angles making up the inner corners: C = 90, A = 60, B = 30.

    The ratios of the sides for those triangles is c = 2, b = 1 and a = sqrt(3).

    So, assuming you know c (it's the length of one of the hexagon sides), you can calculate the other two sides of that triangle without having to resort to trigonometry functions:

    a = c * sqrt(3) / 2
    b = c / 2
    

    Once we know those values, the hexagon offsets are relatively easy to calculate just by eyeballing your grid and using the leftmost corner (A in my diagram above) of each hexagon as the start and end points.

    The horizontal distance from (0,0) to (0,1) is b + c + b - b (heading northeast, east, southeast and southwest). That's the horizontal difference between all "adjacent" cells in a row and it simplifies to b + c, or 3 * c / 2 if you just want to use c.

    The vertical distance from (0,0) to (0,1) (half a row) is simply a (southeast then the irrelevant east). That's equivalent to sqrt(3) * c / 2 or probably close enough to c * 433 / 500 (an error of one part in 16,000) as to not have to involve even square roots.

    Although, if you'd prefer more accuracy, you could stick with the square root calculations - just remember to work out sqrt(3) / 2 once at the start and just use that as the multiplier.

    The vertical distance between rows is obviously double that, say for (0,0) to (1,0).

    So, for every full row, you simply move down c * 433 / 250 units. For every column, you move across c * 3 / 2 units and vertically c * 433 / 500 units (down if you're moving from an even index to an odd one, otherwise up).