意外的菱形平方算法结果 [英] Unexpected Diamond square Algorithm results
问题描述
我目前正在尝试实现Diamond Square算法,尽管该方法有些奏效,但我对HeightMap感到困惑.
I'm currently trying to implement the Diamond Square algorithm and while the thing is somewhat working, I'm confused by the HeightMap.
这是:
如您所见,正方形由较亮的值清楚地勾勒出,而洛杉矶由较暗的值勾勒出轮廓.我真的不明白为什么.我知道地图的尺寸确实很小,但是无论如何我都不认为这是预期的结果,并且在较大尺寸的地图上我也有相同的行为.
As you can see the squares are clearly outlined by brighters values, while the losanges are outlined by darker values. I really cant understand why. I know that the size of the map is really small but I don't think that's the expected result anyway, and I got the same behavior with larger sizes.
这是我的代码:
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);
}
}
我想要一些帮助来理解这个问题.
I would like some help to understand this problem.
推荐答案
在钻石阶段,您无需遍历整个地图.您只计算第一个平方(等于 espace
).
During the diamond phase, you don't iterate over the whole map. You only calculate the first square (equal to espace
).
像这样更改循环终止条件:
Change your loop termination conditions like this:
for (int i = demiSpace; i < Terrain.SIZE; i = i + espace)
{
for (int j = demiSpace; j < Terrain.SIZE; 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);
}
}
此外,您永远不要调整随机性变量( decalage
),当减小 espace
的大小时,该变量会变小.
Also, you never adjust your randomness variable (decalage
), which should get smaller as you reduce the size of espace
.
这篇关于意外的菱形平方算法结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!