Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for creating cells by spiral on the hexagonal field

Help to find an algorithm for creating cells by spiral on the hexagonal field.

Look at the image:

alt text

Let's imagine an dimensionless 2d array. The X axis is the blue line, Y is horizontal, spiral is red.

I need to add cells from the central point x0y0 to point N by spiral

Tell me the way to solve the problem, please. Thank you!

like image 347
Coyod Avatar asked Jan 26 '10 20:01

Coyod


2 Answers

I'd suggest changing the cells numbering sligtly, so that X remains the same when you go down and right (or up and left). Then simple algorithm like the following should work:

  int x=0, y=0;   
  add(x, y); // add the first cell
  int N=1 
  for( int N=1; <some condition>; ++N ) {
    for(int i=0; i<N; ++i) add(++x, y);  // move right
    for(int i=0; i<N-1; ++i) add(x, ++y); // move down right. Note N-1
    for(int i=0; i<N; ++i) add(--x, ++y); // move down left
    for(int i=0; i<N; ++i) add(--x, y); // move left
    for(int i=0; i<N; ++i) add(x, --y); // move up left
    for(int i=0; i<N; ++i) add(++x, --y); // move up right
  }

This generates the points as follows:

Plot of generated points

After a transformation we get:

Transformation of the generated points into a hex grid

like image 157
shura Avatar answered Nov 16 '22 13:11

shura


enter image description here (the circles have a diameter of 1)

Here's a function to get position i:

  void getHexPosition( int i, ref double x, ref double y )
  {
     if ( i == 0 ) { x = y = 0; return; }

     int layer = (int) Math.Round( Math.Sqrt( i/3.0 ) );

     int firstIdxInLayer = 3*layer*(layer-1) + 1;
     int side = (i - firstIdxInLayer) / layer; // note: this is integer division
     int idx  = (i - firstIdxInLayer) % layer;                  
     x =  layer * Math.Cos( (side - 1) * Math.PI/3 ) + (idx + 1) * Math.Cos( (side + 1) * Math.PI/3 );
     y = -layer * Math.Sin( (side - 1) * Math.PI/3 ) - (idx + 1) * Math.Sin( (side + 1) * Math.PI/3 );
  }

Scaling the result by Math.Sqrt(.75) gives

enter image description here

If you're interested in the skewed coordinates like in shura's answer:

  int[] h = { 1, 1, 0, -1, -1, 0, 1, 1, 0 };
  void getHexSkewedPosition( int i, ref int hx, ref int hy )
  {
     if ( i == 0 ) { hx = hy = 0; return; }

     int layer = (int) Math.Round( Math.Sqrt( i/3.0 ) );

     int firstIdxInLayer = 3*layer*(layer-1) + 1;
     int side = (i - firstIdxInLayer) / layer;
     int idx  = (i - firstIdxInLayer) % layer;

     hx = layer*h[side+0] + (idx+1) * h[side+2];
     hy = layer*h[side+1] + (idx+1) * h[side+3];
  }

  void getHexPosition( int i, ref double hx, ref double hy )
  {
     int x = 0, y = 0;
     getHexSkewedPosition( i, ref x, ref y );
     hx = x - y * .5;
     hy = y * Math.Sqrt( .75 );
  }
like image 44
Tom Sirgedas Avatar answered Nov 16 '22 12:11

Tom Sirgedas