0
我目前正在嘗試實現鑽石正方形算法,雖然東西有點工作,但我被HeightMap弄糊塗了。意外鑽石正方形算法結果
這就是:
正如你所看到的廣場是由brighters值清楚地說明,而losanges由暗值概述。 我真的不明白爲什麼。我知道地圖的大小真的很小,但我不認爲這是預期的結果,而且我得到了更大尺寸的相同行爲。
這裏是我的代碼:
public class TerrainBuilder
{
private Terrain Terrain = null;
private Random r = new Random();
private int espace;
public void Init(Terrain _terrain)
{
Terrain = _terrain;
espace = Terrain.SIZE - 1;
}
public void DiamondAlgoritm()
{
if (Terrain == null)
return;
//Initialization
Terrain.HeightMap[0, 0] = r.Next() % 255;
Terrain.HeightMap[0, Terrain.SIZE - 1] = r.Next() % 255;
Terrain.HeightMap[Terrain.SIZE - 1, 0] = r.Next() % 255;
Terrain.HeightMap[Terrain.SIZE - 1, Terrain.SIZE - 1] = r.Next() % 255;
//randominess
int decalage = 128;
while (espace > 1)
{
int demiSpace = espace/2;
//diamond phase
for (int i = demiSpace; i < espace; i = i + espace)
{
for (int j = demiSpace; j < espace; j = j + espace)
{
var avg = Terrain.HeightMap[i + demiSpace, j + demiSpace] + Terrain.HeightMap[i + demiSpace, j - demiSpace] + Terrain.HeightMap[i - demiSpace, j + demiSpace] + Terrain.HeightMap[i - demiSpace, j - demiSpace];
avg /= 4;
Terrain.HeightMap[i, j] = Normalize(avg + r.Next() % decalage);
}
}
//carre phase
for (int i = 0; i < Terrain.SIZE; i += demiSpace)
{
int delay = 0;
if (i % espace == 0)
delay = demiSpace;
for (int j = delay; j < Terrain.SIZE; j += espace)
{
double somme = 0;
int n = 0;
if (i >= demiSpace)
somme = somme + Terrain.HeightMap[i - demiSpace, j];
n = n + 1;
if (i + demiSpace < Terrain.SIZE)
somme = somme + Terrain.HeightMap[i + demiSpace, j];
n = n + 1;
if (j >= demiSpace)
somme = somme + Terrain.HeightMap[i, j - demiSpace];
n = n + 1;
if (j + demiSpace < Terrain.SIZE)
somme = somme + Terrain.HeightMap[i, j + demiSpace];
n = n + 1;
Terrain.HeightMap[i, j] = Normalize(somme/n + r.Next() % decalage);
}
}
espace = demiSpace;
}
}
private double Normalize(double value)
{
return Math.Max(Math.Min(value, 255), 0);
}
}
我想一些幫助來理解這個問題。