Search code examples
c#unity-game-engine

Alternating Odd Numbers


Is there a method or formula to get check if an odd number is an alternate odd number.

Explainer

1  
3 alt  
5  
7 alt  
9  
11 alt  
13  
15 alt...

I have tried Googling for an answer but only able to find modulo for odd/even or arrays with the values held to check against.

int half = (gridsize) / 2;
int row = 0;

for (int x = 0; x < gridsize; x++)
{
    int tilesThisRow = gridsize - (half - row);
    int drop = gridsize - tilesThisRow;

    int frontDrop;
    int backDrop;

    if(drop % 2 != 0)
    {
        //3 front
        //5 back
        //7 front
        //9 back
        //11 fron
        //13 back
        //15 front
        //17 back

        // it's this i need to alternate depending on the size
        frontDrop = (drop / 2) + 1;
        backDrop = (drop / 2);
    }
    
    for (int y = 0; y < gridsize; y++)
    {
        if (y >= frontDrop && y < gridsize - backDrop)
        {
            AddHexTile(x, y, gridOffsets);
        }
    }

    if (x < half)
    {
        row++;
    }
    else
    {
        row--;
    }
}

It's for a method i'm using to generate a hexagon hex tile grid which breaks depending on the size being used.

Pictures for clarification.

Size 7
enter image description here

Size 9
enter image description here


Solution

  • You could e.g. check for

    value % 4 == 3
    

    while using value % 4 == 1 for the others.

    see fiddle

    0
    1 normal odd
    2
    3 alt odd
    4
    5 normal odd
    6
    7 alt odd
    8
    9 normal odd
    10
    11 alt odd
    12
    13 normal odd
    14
    15 alt odd
    16
    17 normal odd
    18
    19 alt odd
    20
    21 normal odd
    22
    23 alt odd
    ...