Hexagonal cell drawing

Ryan2003

New Member
Joined
Jul 12, 2020
Hi,

Here is code which will draw hexagonal cells and report which cell the mouse cursor is hovering over.
(ported from some FreeBasic code)

Cerberus X:
Import mojo

Class cHex    ' Class for one hexagonal cell
    
    ' pre-calc these recurring values
    Field cos000:float = Cos(0)
    Field sin000:float = Sin(0)
    Field cos060:float = Cos(60)
    Field sin060:float = Sin(60)
    Field cos120:float = Cos(120)
    Field sin120:float = Sin(120)
    Field cos180:float = Cos(180)
    Field sin180:float = Sin(180)
    Field cos240:float = Cos(240)
    Field sin240:float = Sin(240)
    Field cos300:float = Cos(300)
    Field sin300:float = Sin(300)
    Field cos030:float = Cos(30)
    
    Field size:Int
    
    Method New(_size:Int)
        Self.size = _size
    End
    
    Method GetSize:Int()
        Return Self.size
    End
    
    Method DrawHex(x:Int, y:Int)    ' draw the hex cell at cell location x,y
    
        Local h2:float = size / 2
        Local s:float = size / cos030 / 2
        Local tx:float = x * s * 1.5
        Local ty:float = y * size + (x Mod 2) * h2
        
        h2 = h2 * 1.16
        
        DrawLine(tx + cos000 * h2, ty + sin000 * h2, tx + cos060 * h2, ty + sin060 * h2)
        DrawLine(tx + cos060 * h2, ty + sin060 * h2, tx + cos120 * h2, ty + sin120 * h2)
        DrawLine(tx + cos120 * h2, ty + sin120 * h2, tx + cos180 * h2, ty + sin180 * h2)
        DrawLine(tx + cos180 * h2, ty + sin180 * h2, tx + cos240 * h2, ty + sin240 * h2)
        DrawLine(tx + cos240 * h2, ty + sin240 * h2, tx + cos300 * h2, ty + sin300 * h2)
        DrawLine(tx + cos300 * h2, ty + sin300 * h2, tx + cos000 * h2, ty + sin000 * h2)
        
    End Method

    
End ' Class cHex


Global myHex:cHex

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        myHex = New cHex(64)    ' create a new hex cell of size 64
    End Method
    Method OnUpdate()       
    
    
    End Method
    Method OnRender()
        Cls 0,0,0
        SetColor 255, 255, 255
        
        ' draw a hex map of x by y cells
        For Local x:Int = 0 To 6
                For Local y:Int = 0 To 6
                    myHex.DrawHex(x, y)
                Next
        Next

        ' Below determines which hex cell the mouse cursor is over
        Local mx:Int = MouseX()
        Local my:Int = MouseY()
    
        Local s:Float = myHex.GetSize() / Cos(30) / 2
        Local arrayx:float = Round(mx / s / 1.5)
        Local arrayy:float = Round( (my - (arrayx Mod 2) * myHex.GetSize() / 2) / myHex.GetSize())
        
        SetColor 255, 0, 0
        myHex.DrawHex(arrayx, arrayy)
        
        Print(string(arrayx) + "  " + string(arrayy))
        
    '    DrawText("Move the mouse across the window.", 10, 10)
        
    End Method
End Class


Function Main()
    New MyGame()
End Function

Function Round:Int(value:Float)
    Return (value + 0.5)
End
1596299608666.png
 
Top Bottom