flauwekeul / honeycomb

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

Alternative Spiral Grid #60

Closed tomer953 closed 3 years ago

tomer953 commented 3 years ago

Hey, First thing I want to thank you, this module is just great and helped me a lot. I'm building some tool for the game "the settlers of catan",

in this game, when you arrange the tiles on the board, you need to arrange them in a spiral order, but in the following way: image

the "spiral" grid in the library is almost exactly what I need, but just almost (left image) when it move up to the next ring, It "jumps" up, instead of continue in the "spiral" naturally... (right image)

so after visiting "7" I wish to visit "19". for now, I first create the grid, assigning some id's to the board, and then travsere them again in the "correct" way. for the record, this is the excat way to visit catan board (according to the id's in the images of-course) [8, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 2, 7, 6, 5, 4, 3, 1];

can you share your thoughts ? many thanks

tomer953 commented 3 years ago

For now, I modified the original spiral code from this:

  return function spiral({ radius, center, onCreate = () => { } }) {
    center = Hex(center)

    let grid = new Grid()

    onCreate(center, grid)
    grid.push(center)

    for (let i = 1; i <= radius; i++) {
      grid = grid.concat(this.ring({ radius: i, center, onCreate });)
    }

    grid.radius = radius
    grid.center = center

    return grid
  }

to this:

  return function spiral({ radius, center, onCreate = () => { } }) {
    center = Hex(center)

    let grid = new Grid()

    onCreate(center, grid)
    grid.push(center)

    for (let i = 1; i <= radius; i++) {
      let nextRing = this.ring({ radius: i, center, onCreate });
       // shift right, for ring number i = shift i-1 elements 
      for (let j = i; j > 1; j--) {
        nextRing.unshift(nextRing.pop());
      }
      grid = grid.concat(nextRing)
    }

    grid.radius = radius
    grid.center = center

    return grid
  }

It worked for me, if in the future you will find time to make this as option, would be great. no need to comment, I'm closing this issue. thanks again <3

flauwekeul commented 3 years ago

Thanks for your question and kind words. I was working on a rewrite of honeycomb and already have a solution in mind that would solve your issue. I thought that feature might not be important, but your question confirms its need :+1: I hope to finish the rewrite this year.

tomer953 commented 3 years ago

Nice!

just for the record, I ended up with one shift to the left, instead shifting right:

for (let i = 1; i <= radius; i++) {
      let nextRing = Grid.ring({ radius: i, center });
      nextRing.push(nextRing.shift()); // shift left
      grid = grid.concat(nextRing)
    }

and for the rewrite part, is it going to break the current behaviour? small\big breaking changes? I wonder if I should wait in this specific project, since it is based on your library

flauwekeul commented 3 years ago

I think there'll be a lot of breaking changes. Especially on how grids are traversed. Also Grid won't inherit from Array anymore. It's hard to tell whether you should wait or not, also because I don't know when I'll finish. I'm planning on releasing an alpha version first to let people give feedback on the new API.

flauwekeul commented 3 years ago

Just to let you know: I've released an alpha version with a new API! Please check out the next branch.