菱形平方算法循环

时间:2018-08-11 20:12:35

标签: c++ algorithm

我一直在研究Diamond Square算法的实现,无法找出Diamond部分循环中的这个小错误。 https://en.wikipedia.org/wiki/Diamond-square_algorithm

这是我的主要功能:

  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);
}

第一个循环的输出是:

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

似乎总是错过第三列的最后一个0。我已经尝试过不同的方法来处理double for循环,但似乎找不到合适的解决方案。

是否有更好的方法来处理钻石台阶的循环条件?

0 个答案:

没有答案