flauwekeul / honeycomb

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

Question: Drawing a line along side of 2 hexes and getting both hexes #79

Closed davout1806 closed 2 years ago

davout1806 commented 2 years ago

In my use case, when I wish to get the hexes between 2 hexes, where the line would run exactly along side of 2 hexes, I want to get both those hexes. For example from hex 0,0 to hex 2,3 I'd like to get hexes: (0,1), (1,0), (1,1), (1,2), and (2,2).

Would I be correct in saying I could get this if I implemented my own hexesBetween() where I performed the same loop 3 times each time using a different epsilon nudge: { q: 1e-6, r: 1e-6, s: -2e-6 }, { q: -2e-6, r: 1e-6, s: 1e-6 }, { q: 1e-6, r: -2e-6, s: 1e-6 } and taking only unique hexes. From several tests it would appear so. But maybe I'm missing some edge case. Thanks.

hexes .

flauwekeul commented 2 years ago

Hi. I assume you're using v3 of the lib. I think your algorithm pretty good, haven't tested it though. The only possible improvement could be to run hexesBetween twice instead of 3 times. In your example of going from (0,0) to (2,3), you only need to go twice, because you need 2 "columns" of hexes. The trick is in determining which 2 out of 3 "nudges" you need. And then it's still a question if determining that results in a significant performance gain...

davout1806 commented 2 years ago

... to run hexesBetween twice instead of 3 times. The trick is in determining which 2 out of 3 "nudges" you need. ...

Ah yes. good point. Thanks.