# Visibility for Hexes

This topic is 5410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm having trouble digging up resources for determining which hexes touch/intersect the line between the center of two other given hexes. Been trying the problem by myself for most of the summer, and not a single stroke of genius has struck. So, here I am to wave the lightning rod around. Any ideas? Links?

##### Share on other sites
i am certain there are better ways, but i once did it like this:

treat your hex grid as if it were an image (likely just as they are on the screen)
create a vector pointing from the starting hex to the end hex.
check the angle of that vector and go to the next hex (if its between -30° and +30° go up, 30°-90° up/right etc)
start again from the new hex until you reach your destination

(don't have any code anymore, hope i got it right)

##### Share on other sites
that sounds like some sort of modified bresenham line algorithm

##### Share on other sites
Hmm... that might actually work well enough. I'll give it a shot.

If anyone has any other ideas, please do post. A very searchable forum is a good forum.

##### Share on other sites
Well, I've got a quick test working, hashed up in Visual Basic... but I've hit an odd problem. Very odd. It won't 'scan' sideways. I'm using flat-top hexes. When the line between two hexes is between up-right and down-right, or between up-left and down-left, the scanner just won't move in that direction. I'd post a screenshot if I had a server to put it on.

Here's my code so far...
Public Sub visCast(sX As Integer, sY As Integer, eX As Integer, eY As Integer)  Dim rtX As Double, rtY As Double, reX As Double, reY As Double  Dim dX As Double, dY As Double, dL As Double  Dim aX As Integer, aY As Integer  Dim aTrace As Boolean    ' Visibility Flag  aTrace = True    ' Location Tracker  aX = sX  aY = sY    ' Destination hex true center  reX = (eX * 1#)  reY = (eY * 1#) + (0.5 * (eY Mod 2))    Do While aX <> eX And aY <> eY And ptInMap(aX, aY)    ' Set visibility    If MapData(aX, aY) Then Exit Sub ' aTrace = False    VisData(aX, aY) = aTrace        ' Current eval. hex true center    rtX = (aX * 1#)    rtY = (aY * 1#) + (0.5 * (aY Mod 2))        ' Get delta to next hex    dX = reX - rtX    dY = reY - rtY    dL = 1 / Sqr(dX * dX + dY * dY)    dX = dX * dL    dY = dY * dL        ' 0.86602540378444 = sin(PI/3)    If dY <= -0.86602540378444 Then      ' Straight Up      aY = aY - 1    ElseIf dY >= 0.86602540378444 Then      ' Straight Down      aY = aY + 1    ElseIf dY <= 0 Then      ' Upward      If dX >= 0 Then        ' Up-Right        If (aX Mod 2) Then          aX = aX + 1        Else          aX = aX + 1          aY = aY - 1        End If      Else        ' Up-Left        If (aX Mod 2) Then          aX = aX - 1        Else          aX = aX - 1          aY = aY - 1        End If      End If    ElseIf dY > 0 Then      ' Downward      If dX >= 0 Then        ' Down-Right        If (aX Mod 2) Then          aX = aX + 1          aY = aY + 1        Else          aX = aX + 1        End If      Else        ' Down-Left        If (aX Mod 2) Then          aX = aX - 1          aY = aY + 1        Else          aX = aX - 1        End If      End If    End If      Loop    If MapData(aX, aY) Then Exit Sub ' aTrace = False  VisData(aX, aY) = aTrace  End Sub

The if blocks based on (aX mod 2) is for accounting for the staggered columns (odd columns are shifted down by a halfhex).

Did I do something fundamentally wrong?

##### Share on other sites
Answer; yup. Cor, am I an idiot...

This line here was my problem.
' The old nonfunctional:  Do While aX <> eX And aY <> eY And ptInMap(aX, aY)' What it should've been:  Do While (aX <> eX Or aY <> eY) And ptInMap(aX, aY)

When we have aY == eY, then we have (False And True And True), and we quit.

So. Got a decent algorithm here, although it's still subject to some odd spots that should be visible, and some odd spots that shouldn't (mostly involving the fact I always blindly step to the 'middle' of hexes; I could add some fudging to get cleaner results).

Thanks for the help!

• 13
• 18
• 29
• 11