Finding line of sight on a hex grid using only coordinates

44 Views Asked by At

I have a flat top hex grid where each hex has coordinates [x,y,z] where the first element is the column increasing left to right, the second is the / diagonal increasing from left to right, and the third is the \ diagonal increasing from right to left. So for example the first column is 0,0,0 ; 0,1,1 ; 0,2,2 ; 0,3,3 etc. the next is 1,1,0 ; 1,2,1 ; 1,3,2 etc. and so on for the other columns.

My goal is to write a function (C#) that can find the coordinates of all the hexes that lie on a line between two end hexes, including any hexes that the line runs along the border of.

I have figured out how to do it by hand, but I'm having trouble translating it into code. Here is what I have using hexes 0,2,2 and 3,3,0:

Find the greatest difference between corresponding x, y, or z coordinates. In this case it is 3. This means there are 4 hexes in the path and 3 'jumps' between hexes.
For each pair of x, y, and z coordinates, step from a to b distributing the steps symmetrically over the 'jumps'. In this example: 0>1>2>3, 2>2>3>3, 2>1>1>0.
From this, the path is 0,2,2 ; 1,2,1 ; 2,3,1 ; 3,3,0

If there is no symmetrical way to distribute the steps, use all the ways to distribute them as symmetrically as possible.
e.g. 0,2,2 to 2,3,1 where the line passes exactly between two hexes Biggest coordinate difference is 2 so there are 3 hexes involved (not really but for now just pretend).
For the x coordinates it is simple: 0>1>2.
But y: 2>2>3 or 2>3>3.
And z: 2>2>1 or 2>1>1.

Taking all the possibilities, the middle hex could be 1,2,2 ; 1,2,1 ; 1,3,2 or 1,3,1.
There are no hexes with coordinates 1,2,2 or 1,3,1 , so the line passes along the border of the other two.

I haven't tested this method super thoroughly but everything I have tested works.

I'm not necessarily asking for a direct translation into C#, but I genuinely have no idea how to proceed so any help would be much appreciated.

Thanks.

0

There are 0 best solutions below