flauwekeul / honeycomb

Create hex grids easily, in node or the browser.
https://abbekeultjes.nl/honeycomb
MIT License
638 stars 59 forks source link

How to generate a grid from a rectangle with fixed size for the hexagon? #31

Closed khamaileon closed 5 years ago

khamaileon commented 5 years ago

I need to generate a hexagonal grid in my JS frontend that perfectly matches the one I generate from my Python backend because I use the centroid of the hexagon as an ID.

This is the Python code / library I use now:

https://github.com/stephanh42/hexutil#converting-between-hexagonal-grid-coordinates-and-screen-coordinates

hex_width = 100
hex_height = 58
grid = HexGrid(width=hex_width, height=hex_height)
hexes = grid.hexes_in_rectangle([x_min, y_min, width, height])

Is there a way to fix the height and width of a hexagon with "honeycomb"?

flauwekeul commented 5 years ago

If I understand you correctly you want to have a different width and height for your hexes. I'm afraid this is not possible currently with honeycomb ☚ī¸

I can image this is an important feature and I'll look into adding it. Please keep in mind this might take some time though...

flauwekeul commented 5 years ago

Update: I've added the possibility to set a separate width and height for hexes, but Hex#fromPoint() (and Grid.pointToHex()) is now broken. I'm struggling with the math, but once I've got it, I need to update the tests and I could release an alpha version. Hopefully within a week.

redblobgames commented 5 years ago

One way to think about Hex#fromPoint is that you now have a separate size for x and y axes. Whereas before, you might calculate hex width and height from size, you now have them as input constraints, and can calculate size from them (separately for x and y).

flauwekeul commented 5 years ago

Thanks for the input Amit :blush: I know how I'm going to do it, didn't found the time to finish it though :pensive:

flauwekeul commented 5 years ago

I've just released v2.0.0 of honeycomb. It's a major release because I've removed some methods. I hoped the auto generated changelog would show the breaking changes too, but no 😞

In this new version it's possible to have "stretched" hexes. See the docs for more info.

I've actually used a (the?) python hex library to "borrow" the math, thanks for that 👍