Tesselated hex grid routine
#1
Back on the old forum I did a program for overlaying square and hex grids over maps & images. The square grid was easy peasy, the hex grid posed a somewhat greater challenge. In revisiting that program, I felt the need to make the hex grid more versatile and modular. Here's a demo of what I came up with.

Code: (Select All)
'Tesselated Hex Grid Demo- OldMoses
SCREEN _NEWIMAGE(1024, 512, 32)
test& = _NEWIMAGE(512, 256, 32)
_DEST test&
CLS , &HFFFF0000
_DEST 0

in% = -1
hsiz = 50
xoff = 0
yoff = 0
DO
    DO
        k$ = INKEY$
        IF k$ <> "" THEN
            SELECT CASE LCASE$(k$)
                CASE IS = "t" '                                 "t" tilt grid 90 degrees
                    t = NOT t
                CASE IS = "+" '                                 "+" increase grid size
                    hsiz = hsiz + 1
                CASE IS = "-" '                                 "-" decrease grid size
                    hsiz = hsiz - 1: IF hsiz < 6 THEN hsiz = 6
                CASE IS = "e" '                                 "e" bias grid up
                    yoff = yoff - 1
                CASE IS = "x" '                                 "x" bias grid down
                    yoff = yoff + 1
                CASE IS = "s" '                                 "s" bias grid to left
                    xoff = xoff - 1
                CASE IS = "d" '                                 "d" bias grid to right
                    xoff = xoff + 1
                CASE IS = "q" '                                 "q" return to system
                    SYSTEM
            END SELECT
            in% = -1
        END IF
        _LIMIT 30
    LOOP UNTIL in%
    in% = 0
    CLS , &HFFB0B0B0
    _DEST test&
    CLS , &HFFFF0000
    _DEST 0
    Hex_Grid hsiz * 2, 0, xoff, yoff, t, &HFF000000 '           draw double sized grid on screen
    Hex_Grid hsiz, test&, xoff, yoff, NOT t, &HFF000000 '       draw base sized, rotated grid on test insert image
    _PUTIMAGE (256, 64), test&, 0
    _DISPLAY
LOOP


'Tessellated hex grid drawing subroutine
'siz = distance in pixels between opposite sides
'img = destination image for grid
'h = horizontal offset
'v = vertical offset
'vert = {0} opposite vertices horizontal  {-1} opposite vertices vertical
SUB Hex_Grid (siz AS INTEGER, img AS LONG, h AS INTEGER, v AS INTEGER, vert AS _BYTE, cl AS _UNSIGNED LONG)

    old& = _DEST
    grd& = _NEWIMAGE(_WIDTH(img), _HEIGHT(img), 32) '           temporary grid image
    _DEST grd&

    'Base geometric algorithm
    side_len = siz / SQR(3) '                                   length of side
    side_hlf = _SHR(side_len, 1) '                              1/2 length of side (for slope line offsets)
    siz_hlf = _SHR(siz, 1) '                                    length from center to orthogonal side
    a = side_len: b = 0 '                                       orthogonal line offsets
    c = side_hlf: d = -siz_hlf '                                left angled line offsets
    e = side_hlf: f = siz_hlf '                                 right angled line offsets
    IF vert THEN
        offA = side_len + side_hlf + _SHR(side_hlf, 1) '        column spacing (x) when vertical opposite vertices
        offB = siz - _SHR(side_hlf, 1) '                        row spacing (y) when vertical
        SWAP a, b: SWAP c, d: SWAP e, f '                       rotate figure end points 90 degrees around (x, y)
    ELSE
        offA = _SHL(side_len + side_hlf, 1) '                   column spacing (x) when horizontal
        offB = _SHR(siz, 1) '                                   row spacing (y) when horizontal
    END IF
    row = -1
    DO
        column = -1
        y = row * offB + v '                                    set origin point y
        of = -(_SHR(offA, 1)) * (row MOD 2 = 0) '               bias alternating rows by half
        DO
            x = column * offA + h + of '                        set origin point x
            LINE (x, y)-(x - a, y - b), cl '                    display orthogonal line
            LINE (x, y)-(x + c, y + d), cl '                    draw angled right/up
            LINE (x, y)-(x + e, y + f), cl '                    draw angled left/down
            column = column + 1 '                               move to next column position
        LOOP UNTIL (column - 1) * offA > _WIDTH(grd&)
        row = row + 1
    LOOP UNTIL (row - 1) * offB > _HEIGHT(grd&)
    'end base geometric algorithm

    _PUTIMAGE , grd&, img '                                     overlay grid to img
    _FREEIMAGE grd&
    _DEST old&

END SUB 'Hex_Grid
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply


Messages In This Thread
Tesselated hex grid routine - by OldMoses - 06-03-2022, 12:42 AM
RE: Tesselated hex grid routine - by vince - 06-07-2022, 10:45 AM
RE: Tesselated hex grid routine - by bplus - 06-07-2022, 10:54 AM
RE: Tesselated hex grid routine - by Coolman - 06-07-2022, 11:18 AM
RE: Tesselated hex grid routine - by OldMoses - 06-08-2022, 11:54 PM



Users browsing this thread: 3 Guest(s)