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?