I wrote this a while ago after I took interest in a project Pincho Paxton was working on. It generates a screenfull of hexagons... cool, huh? It's easy generating the point cloud, but figuring out what to hook up to what had me stumped. Got it tho:
type hex
a as integer
b as integer
c as integer
x as float
y as float
xvel as float
yvel as float
rowtype as boolean
endtype
global arraysize=1000
dim hexagon(arraysize) as hex
global startx as float = 1.0
global starty as float = 1.0
global sidelength as float = 30.0
curx as float = startx-sidelength
cury as float = starty-sidelength
switch as integer = 0
switch2 as boolean = 0
global exn as float = 700.0
cursor as integer
cursor=40
press as boolean = 0
retrieve_hexagonal_grid()
do
cls
for a=1 to arraysize
dx#=0
dy#=0
if spacekey()
f#=1/sqrt((hexagon(a).x-mousex())^2+(hexagon(a).y-mousey())^2)`*rnd(1)
hexagon(a).xvel=(hexagon(a).x-mousex())*f#*-25
hexagon(a).yvel=(hexagon(a).y-mousey())*f#*-25
endif
next a
for a=1 to arraysize
`dot hexagon(a).x+dx#,hexagon(a).y+dy#
`if (a mod 2)
q=hexagon(a).a
r=hexagon(a).b
e=hexagon(a).c
if q>0 then line hexagon(a).x+hexagon(a).xvel,hexagon(a).y+hexagon(a).yvel,hexagon(q).x+hexagon(q).xvel,hexagon(q).y+hexagon(q).yvel
if r>0 then line hexagon(a).x+hexagon(a).xvel,hexagon(a).y+hexagon(a).yvel,hexagon(r).x+hexagon(r).xvel,hexagon(r).y+hexagon(r).yvel
if e>0 then line hexagon(a).x+hexagon(a).xvel,hexagon(a).y+hexagon(a).yvel,hexagon(e).x+hexagon(e).xvel,hexagon(e).y+hexagon(e).yvel
`endif
next a
sync
loop
function get_length(width,length)
a = width/(3.0*length)
b# = (width/(3.0*length)-a)*3
a=a*2
if b#>=0 then inc a
if b#>=1 then inc a
endfunction a
function retrieve_hexagonal_grid()
exn=screen width()
startx=1
starty=1
curx = startx
cury = starty
switch = 0
switch2 = 1
hexagon(1).x=startx
hexagon(1).y=starty
for a=2 to arraysize
if switch=1
switch=0
inc curx,2*sidelength
else
switch=1
inc curx, sidelength
endif
if curx>exn
if switch2=0
switch2=1
switch=0
inc cury,sidelength*sqrt(3)/2
curx=startx
else
switch=0
switch2=0
inc cury,sidelength*sqrt(3)/2
curx=startx+sidelength*1.5
endif
endif
hexagon(a).x=curx
hexagon(a).y=cury
hexagon(a).rowtype=switch2
next a
for a=1 to arraysize
hexagon(a).a=getAdjA(a)
hexagon(a).b=getAdjB(a)
hexagon(a).c=getAdjC(a)
next a
endfunction
function getLeftPoint(index)
if hexagon(index).rowtype
ret=index-get_length(hexagon(index).x,sidelength)+1
else
ret=index-get_length(hexagon(index).x-1.5*sidelength,sidelength)+1
endif
endfunction ret
function getLeftDelta(index)
a=getLeftPoint(index)
ret=index-a
endfunction ret
function getRightPoint(index)
if hexagon(index).rowtype
ret=index+get_length(exn-hexagon(index).x,sidelength)-1
else
ret=index+get_length(exn-hexagon(index).x+1.5*sidelength,sidelength)-2
endif
endfunction ret
function getRightDelta(index)
a=getRightPoint(index)
ret=a-index
endfunction ret
function getAdjA(index)
l=get_length(exn,sidelength)
v=index-l+1
if v<0 then exitfunction 0
if hexagon(index).rowtype
dec v
if v<0 then exitfunction 0
endif
if hexagon(index).rowtype=hexagon(v).rowtype then exitfunction 0
endfunction v
function getadjB(index)
bool= getLeftDelta(index) mod 2
if hexagon(a).rowtype
if bool
ret=index+1
else
ret=index-1
endif
else
if bool
ret=index-1
else
ret=index+1
endif
endif
if ret<0 then exitfunction 0
if ret>arraysize then exitfunction 0
if hexagon(ret).rowtype<>hexagon(index).rowtype then exitfunction 0
endfunction ret
function getAdjc(index)
l=get_length(exn,sidelength)
v=index+l
if v>arraysize then exitfunction 0
if hexagon(index).rowtype
dec v
if v>arraysize then exitfunction 0
endif
if hexagon(index).rowtype=hexagon(v).rowtype then exitfunction 0
endfunction v
Not particularly usable as is.
The lag (press space to see it redrawing every frame, as well as another kinda kool effect) is because of the line function, no other code. replacing the line function with d3d_line brought me up to ~100 fps.