BoardGameGeek
»
Forums »
Gaming Related »
Wargaming
Hex-grid LOS math *general solution*
Hello all - I'm sure I'm reinventing the wheel here, but I'm not sure the solution I've found public so far
http://www-cs-students.stanford.edu/~amitp/Articles/HexLOS.h... is always correct. That one depends on a modified Bresenham line-drawing algorithm, and the Line of Sight may cross a hex even if it's not enough to warrant getting an ON pixel after the rounding (the idea of the algorithm for square grids).
Superpose a rectangular x,y grid onto the hex grid, such as one direction of hex-centers coincides with the x axis. So, the centers of hexes along it have x,y coordinates (0,0) - the observer/firer, (1,0), (2,0) ...
My hex-grid coordinates are what wargamers use 'by eye', and are different from the two systems proposed in that article:
D -> distance in hexes
L -> 'laterality' i.e. how many hex rows away from the x axis
A hex of hex-coordinates (D,L) moves from the origin: D-L hexes along the axis, and L hexes to 60 degrees (suppose 'up')
The rectangular coordinates of hex-coords (1,1) are (1/2, sqrt(3)/2)
So, the rectangular coordinates of the center of a hex (say the Target) are:
Xt = Dt - Lt/2
Yt = Lt * sqrt(3)/2
The coordinates of the lowest corner of the Obstacle hex are:
Xo = Do - Lo/2
Yo = Lo * sqrt(3)/2 - sqrt(3)/3
(Note: the lowest corner, and not e.g. the right-lower corner is the critical one if-and-only-if Dt >= 2* Lt. Else, look at it from another angle - rotate the X axis 60 degrees and mirror the image)
There is LOS to the target if the slope of the Firer-Obstacle line is not lower than the slope of the Firer-Target line:
So = Yo / Xo = (Lo * sqrt(3)/2 - sqrt(3)/3) / (Do - Lo/2)
St = Yt / Xt = (Lt * sqrt(3)/2) / (Dt - Lt/2)
So >= St
simplify by sqrt(3)/6
(3*Lo - 2) / (Do - Lo/2) >= 3*Lt / (Dt - Lt/2)
so there results the
Minimum distance to the target to have LOS, given the other 3 variables (distance to obstacle, laterality of target and obstacle):
Dt min = Lt/2 + (Do - Lo/2) * 3*Lt / (3*Lo - 2)not easy to apply, but cannot be further simplified meaningfully.
A particular case is when both target and obstacle are 1 hex lateral from the axis: Lt=1, Lo=1. Then the "3* X-distance" rule applies:
Dt min = 0.5 + (Do - 0.5) * 3Example in image: if the obstacle is 2 hexes away, 1 lateral (Do=2, Lo=1), the nearest hex that is 1 lateral that is (barely) visible is (5,1):
Dt min = 0.5 + (2 - 0.5) * 3 = 0.5 + 4.5 = 5