Tesselated hex grid routine - OldMoses - 06-03-2022
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
RE: Tesselated hex grid routine - vince - 06-07-2022
vroom
Code: (Select All) 'Moving hex grid
SCREEN _NEWIMAGE(480, 480, 32)
s = 35
sb = s / 2
ho = s + (s * COS(_D2R(60)))
vo = 2 * (s * SIN(_D2R(60)))
ymax = 20
DO
CLS
k = k - 1
IF k MOD vo = 0 THEN k = 0
FOR x = -10 TO 10
FOR y = 0 TO ymax
IF x MOD 2 = 0 THEN b = vo / 2 ELSE b = 0
x11 = x * ho - sb
y11 = y * vo + b + k
x12 = x * ho + s - sb
y12 = y * vo + b + k
p1 = 240 + x11 * 700 / (y11 + 250)
q1 = 200 * 700 / (y11 + 250)
p2 = 240 + x12 * 700 / (y12 + 250)
q2 = 200 * 700 / (y12 + 250)
'LINE (x11, y11)-(x12, y12)
LINE (p1, q1)-(p2, q2)
x21 = x * ho + s - sb
y21 = y * vo + b + k
x22 = x * ho + s + ho - s - sb
y22 = y * vo + b - (vo / 2) + k
p1 = 240 + x21 * 700 / (y21 + 250)
q1 = 200 * 700 / (y21 + 250)
p2 = 240 + x22 * 700 / (y22 + 250)
q2 = 200 * 700 / (y22 + 250)
'LINE (x21, y21)-(x22, y22)
LINE (p1, q1)-(p2, q2)
x31 = x * ho + s - sb
y31 = y * vo + b + k
x32 = x * ho + s + ho - s - sb
y32 = y * vo + b + (vo / 2) + k
p1 = 240 + x31 * 700 / (y31 + 250)
q1 = 200 * 700 / (y31 + 250)
p2 = 240 + x32 * 700 / (y32 + 250)
q2 = 200 * 700 / (y32 + 250)
'LINE (x31, y31)-(x32, y32)
LINE (p1, q1)-(p2, q2)
NEXT y
NEXT x
LINE (0, 0)-(480, 100), _RGB(0, 0, 0), BF
LINE (0, 101)-(480, 101)
_LIMIT 50
_DISPLAY
LOOP UNTIL _KEYDOWN(27)
SYSTEM
RE: Tesselated hex grid routine - bplus - 06-07-2022
Wow that's pretty cool, the land of chicken wire!
RE: Tesselated hex grid routine - Coolman - 06-07-2022
nice effect
RE: Tesselated hex grid routine - OldMoses - 06-08-2022
That is simply amazing to me Vince, perhaps one day I can begin to wrap my poor tender head around it.
|