1

I've been working on a Diamond Square algorithm implementation and can't figure out this small bug in the diamond part's loop. https://en.wikipedia.org/wiki/Diamond-square_algorithm

This is my main function:

  void diamondSquare(int Array[CHUNK_X][CHUNK_Z], int size)
  {
    int half = size / 2;
    if (half < 1)
        return ;
    cout << "size:" << size << " half: " << half << endl;

    cout << "Square steps:" << endl;
    for (int z = half; z < CHUNK_Z; z+=size)
    {
        for (int x = half; x < CHUNK_X; x+=size)
        {
            cout << x % CHUNK_X << " " << z % CHUNK_Z << " " << half << endl;
            squareStep(Array, x % CHUNK_X, z % CHUNK_Z, half);
            printArray(Array);
        }
    }

    cout << "Diamond steps:" << endl;
    int currentColumn = 0;
    for (int x = 0; x < CHUNK_X; x += half)
    {
        currentColumn++;
        //If this is an odd column.
        if (currentColumn % 2 == 1)
        {
            for (int z = half; z < CHUNK_Z; z += size)
            {
                cout << "odd col" << x % CHUNK_X << " " << z % CHUNK_Z << " " << half << endl;
                diamondStep(Array, x % CHUNK_X, z % CHUNK_Z, half);
                printArray(Array);
            }
        }
        else
        {
            for (int z = 0; z < CHUNK_Z; z += size)
            {
                cout << "even col" << x % CHUNK_X << " " << z % CHUNK_Z << " " << half << endl;
                diamondStep(Array, x % CHUNK_X, z % CHUNK_Z, half);
                printArray(Array);
            }
        }
    }

    cout << "New loop" <<  endl;
    diamondSquare(Array, size / 2);
}

And the output for the first loop is:

size:5 half: 2
Square steps:
2 2 2
0
64  0  0  0 64
 0  0  0  0  0
 0  0 64  0  0
 0  0  0  0  0
64  0  0  0 64


Diamond steps:
odd col0 2 2
0
64  0 64  0 64
 0  0  0  0  0
 0  0 64  0  0
 0  0  0  0  0
64  0  0  0 64


even col2 0 2
0
64  0 64  0 64
 0  0  0  0  0
64  0 64  0  0
 0  0  0  0  0
64  0  0  0 64


odd col4 2 2
0
64  0 64  0 64
 0  0  0  0  0
64  0 64  0  0
 0  0  0  0  0
64  0 64  0 64


New loop

It seems to always miss the last 0 in the third column. I've tried different approaches to the double for loop but can't seem to find a proper solution.

Is there a better approach to the loop conditions for the diamond step?

Nick
  • 35
  • 6
  • 1
    1) show code for `diamondStep` and `squareStep`. 2) the diamond step occurs first. 3) likely to be one of those "off-by-one" errors; step through code with a debugger. – meowgoesthedog Aug 11 '18 at 23:01
  • too lazy to debug your code but you can use mine working [C++ Diamond&Square Island generator with biomes](https://stackoverflow.com/a/36647622/2521214) for comparison ... – Spektre Aug 12 '18 at 06:27

0 Answers0