1

I was searching for am algorithm to generate a HoneyComb of hexagons like this one using axial coordinates :

this has a radius of 4 But I only managed to find generators in Cube coordinates.

I made this question only to share my solution .

Don Roby
  • 40,677
  • 6
  • 91
  • 113
nemo9955
  • 126
  • 1
  • 8

2 Answers2

2

This generates all r*(r-1)*3+1 tiles of a hexagonal spiraling pattern . A drawback of the method is that it assumes you are placing the shape at (0,0) .

   public void makeHoneyComb(int radius){

    makeCell(ta, 0, 0);
    for (int r = 0; r > -radius; r--)
        for (int q = -r - 1; q > -radius - r; q--)
            makeCell( q, r);

    for (int r = 1; r < radius; r++)
        for (int q = 0; q > -radius; q--)
            makeCell( q, r);

    for (int q = 1; q < radius; q++)
        for (int r = -q; r < radius - q; r++)
            makeCell( q, r);
    }

This is based on the fact that a shape like that cam be split in 3 similar Rectagles and the center piece .

nemo9955
  • 126
  • 1
  • 8
1

I wanted something similar in JavaScript so I modified the code here and now here is the JS version:

function makeHoneyComb(rings) {

  var cells = [];

  cells.push({
    c: 0,
    r: 0,
  });

  for (r = 0; r > -rings; r--) {
    for (c = -r - 1; c > -rings - r; c--) {
      cells.push({ c: c, r: r });
    }
  }

  for (r = 1; r < rings; r++) {
    for (c = 0; c > -rings; c--) {
      cells.push({ c: c, r: r });
    }
  }

  for (c = 1; c < rings; c++) {
    for (r = -c; r < rings - c; r++) {
      cells.push({ c: c, r: r });
    }
  }

  return cells;
}

var honeyCombArray = makeHoneyComb(3);
Fasani
  • 5,141
  • 1
  • 23
  • 24