#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Hexagonal grid

in General Posts: 50

How would I make a hexagonal grid?
This is my code

``````function setup()
tab={}
end
function calcvert(center,i)
angle_deg = 60 * i
angle_rad = math.pi / 180 * angle_deg
corx = center.x + 40 * math.cos(angle_rad)
cory = center.y + 40 * math.sin(angle_rad)
return corx,cory
end
function makehex(x,y)
center=vec2(x,y)
for i = 1,5 do
vert1x,vert1y = calcvert(center,i)
vert2x,vert2y = calcvert(center,i+1)
b = line(vert1x,vert1y,vert2x,vert2y)
end

end

function draw()
background(136, 136, 138, 255)
pushMatrix()
makehex(100,200)
makehex(160,233)

popMatrix()
end
``````

• Mod
edited January 2018 Posts: 8,496
``````displayMode(FULLSCREEN)

function setup()
size=20
end

function draw()
background(95, 179, 228, 255)
stroke(255)
strokeWidth(2)
z,t=0,0
for x=size,WIDTH-size,size*1.5 do
for y=size,HEIGHT-size*2,size*1.5 do
hex(x,y+z)
end
z=z+size*.75
if z==size*1.5 then
z=0
end
end
end

function hex(x,y)
line(x+size,y,x+size/2,y+size*.75)
line(x+size/2,y+size*.75,x-size/2,y+size*.75)
line(x-size/2,y+size*.75,x-size,y)
line(x-size,y,x-size/2,y-size*.75)
line(x-size/2,y-size*.75,x+size/2,y-size*.75)
line(x+size/2,y-size*.75,x+size,y)
end
``````
• Mod
Posts: 2,020

@dmoeller if you're making a hexagonal tile based game, make sure you check out Amit Patel's page on hexagonals, it's an incredible resource. The interactive diagrams are insane:

https://www.redblobgames.com/grids/hexagons/

His whole site is a treasure trove for gaming algorithms:

https://www.redblobgames.com

• Mod
Posts: 3,297

@yojimbo2000 fantastic link, thanks! So clear.