HEXAGÖN - A library for drawing hexagonal grids

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Eldy
Prole
Posts: 7
Joined: Mon Oct 26, 2015 10:06 pm

HEXAGÖN - A library for drawing hexagonal grids

Post by Eldy »

This is a small library that I wrote as a proof of concept for a project.

Basically everything is in the README.md file. It is not very developped, but I like to believe it has a solid foundation. Maybe it will be useful for someone else?

Check out the demo included in the repository!

https://github.com/olivier-grech/hexagon
User avatar
keharriso
Citizen
Posts: 92
Joined: Fri Nov 16, 2012 9:34 pm

Re: HEXAGÖN - A library for drawing hexagonal grids

Post by keharriso »

Looks like a cool little library. You should really look into using local variables, though. By default, Lua variables are global, even inside of functions. It looks like your helper functions (toHexagonCoordinatesHorizontal, toHexagonCoordinatesVertical, distanceBetween) are also global. You might want to make them local and stick them at the top.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
User avatar
Eldy
Prole
Posts: 7
Joined: Mon Oct 26, 2015 10:06 pm

Re: HEXAGÖN - A library for drawing hexagonal grids

Post by Eldy »

Thanks for your answer.

Is sticking

Code: Select all

local
in front of the variable/function enough, or is there something else I should be aware of?
User avatar
keharriso
Citizen
Posts: 92
Joined: Fri Nov 16, 2012 9:34 pm

Re: HEXAGÖN - A library for drawing hexagonal grids

Post by keharriso »

Eldy wrote: Mon Mar 18, 2019 10:44 am Thanks for your answer.

Is sticking

Code: Select all

local
in front of the variable/function enough, or is there something else I should be aware of?

Code: Select all

local
is all you should need.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
User avatar
Eldy
Prole
Posts: 7
Joined: Mon Oct 26, 2015 10:06 pm

Re: HEXAGÖN - A library for drawing hexagonal grids

Post by Eldy »

I have taken your remarks into account.
User avatar
keharriso
Citizen
Posts: 92
Joined: Fri Nov 16, 2012 9:34 pm

Re: HEXAGÖN - A library for drawing hexagonal grids

Post by keharriso »

There's still:
  • offset
  • xA
  • xB
  • xC
  • yA
  • yB
  • yC
  • distanceToA
  • distanceToB
  • distanceToC
  • hexagonA
  • hexagonB
  • hexagonC
  • possibleHexagons
  • distances
  • closerHexagon
  • resultX
  • resultY
  • hx
  • hy
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
User avatar
Eldy
Prole
Posts: 7
Joined: Mon Oct 26, 2015 10:06 pm

Re: HEXAGÖN - A library for drawing hexagonal grids

Post by Eldy »

Thanks a lot for your help. This should be done now, hopefully.
User avatar
Eldy
Prole
Posts: 7
Joined: Mon Oct 26, 2015 10:06 pm

Re: HEXAGÖN - A library for drawing hexagonal grids

Post by Eldy »

Hello everyone!

I did some refactoring on my library. I belive it makes more sense now. You can check out the demo, but here's a really basic case as an example.

You can put this in the love.load() method for example:

Code: Select all

-- Create a 5*5 grid
demoGrid = hexagon.grid(5, 5, 50, true, false)

-- Create a canvas on which to draw the grid
demoCanvas = love.graphics.newCanvas(800, 600)
And this would be in the love.draw() method:

Code: Select all

-- Draw the demonstration grid on the canvas
hexagon.drawGrid(demoGrid, demoCanvas)
That's it.

As a design note, the method that was used to draw a single hexagon was annoying for me from an architectural point of view. So I made it a private method, and refactored the method to draw a grid so it is more intuitive to use.

If you want to draw a single hexagon, you can still draw a 1*1 grid. :megagrin:

On the future evolution of this library, here's what I would like to do:
  • Add the ability to pass a two-dimensional array to draw a grid. Each elements of the array would store, for example, the color of an hexagon.
  • Handle the case where the grid is drawn with an offset on the canvas (so it does not mess up with the coordinates calculation). This was somewhat handled in the previous version, but not every case was taken into account. So I removed it until I find a satisfactory solution.
  • Add more comments. :ehem: In my defense, I added some comments in the demo file along with the refactor.
Enjoy!
Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests