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

Comments

  • dave1707dave1707 Mod
    edited January 26 Posts: 6,787
    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
    
  • Posts: 2,015

    @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

  • Jmv38Jmv38 Mod
    Posts: 3,277

    @yojimbo2000 fantastic link, thanks! So clear.

Sign In or Register to comment.