意外的菱形平方算法结果

时间:2017-03-19 17:41:25

标签: c# algorithm heightmap

我目前正在尝试实施Diamond Square算法,虽然事情有点奏效,但我对HeightMap感到困惑。

这是:

heightmap result

正如你所看到的那样,正方形由明亮的价值观清晰地勾勒出来,而洛杉矶则由更暗的价值勾勒出来。 我真的不明白为什么。我知道地图的大小确实很小,但我不认为这是预期的结果,而且我的尺寸更大也有相同的行为。

这是我的代码:

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

我想帮助理解这个问题。

1 个答案:

答案 0 :(得分:1)

在钻石阶段,您不会遍历整个地图。您只计算第一个方块(等于espace)。

更改循环终止条件,如下所示:

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的大小,随机变量变小。