菱形方法算法创建对角线切割

时间:2017-05-22 13:32:15

标签: c++ algorithm game-engine terrain

我在我的空闲时间正在处理的事情上遇到了一些问题: 我想通过DSA在c ++中创建一个高度图。但它没有按照我的计划运作。 我的代码是(请忽略两个字符注释):

void DiamondSquareAlgorithm(int x1, int y1, int x2, int y2, float range, unsigned level)
{
    //level = size - 1 when called the first time

    if (level < 1) return;
    float a;
    float b;
    float c;
    float d;
    float e;
    for (int i = x1; i < x2; i += level)
    {
        for (int j = y1; j < y2; j += level)
        {
            //diamond
            a = startArr[i + j * (x2 - x1 + 1)]; //lo
            b = startArr[(i + level) + j * (x2 - x1 + 1)]; //ro
            c = startArr[i + (j + level) * (x2 - x1 + 1)]; //lu
            d = startArr[(i + level) + (j + level) * (x2 - x1 + 1)]; //ru
            e = startArr[(i + level / 2) + (j + level / 2) * (x2 - x1 + 1)] = (a + b + c + d) / 4;
        }
    }

    for (int i = x1; i < x2; i += level)
    {
        for (int j = y1; j < y2; j += level)
        {
            a = startArr[i + j * (x2 - x1 + 1)]; //lo
            b = startArr[(i + level) + j * (x2 - x1 + 1)]; //ro
            c = startArr[i + (j + level) * (x2 - x1 + 1)]; //lu
            d = startArr[(i + level) + (j + level) * (x2 - x1 + 1)]; //ru
            e = startArr[(i + level / 2) + (j + level / 2) * (x2 - x1 + 1)] = (a + b + c + d) / 4;

            //square
            startArr[(i + level / 2) + j * (x2 - x1 + 1)] = (a + b + e) / 3; //o
            startArr[(i + level) + (j + level / 2) * (x2 - x1 + 1)] = (b + d + e) / 3; //r
            startArr[(i + level / 2) + (j + level) * (x2 - x1 + 1)] = (d + c + e) / 3; //u
            startArr[i + (j + level / 2) * (x2 - x1 + 1)] = (a + c + e) / 3; //l
        }
    }
    DiamondSquareAlgorithm(x1, y1, x2, y2, range / 2, (level / 2));
};

结果始终如下:

Terrain

有人可以帮助我找到我在代码中犯下的明显重大错误吗?

1 个答案:

答案 0 :(得分:0)

我终于发现了我的代码问题。除了一件小事之外,上面的大部分内容都是正确的:它不应该是(x2 - x1 + 1)。这会在数组中进一步移动每个值一个空格。这发生在每一步,所以这条对角线出现;正确且有效的代码是

void DiamondSquareAlgorithm(int x1, int y1, int x2, int y2, float range, unsigned level)
{
    //level = size - 1 when called the first time
    if (level <= 1) return;
    float a;
    float b;
    float c;
    float d;
    float e;
    int width = x2 - x1;
    for (int y = x1; y < x2; y += level)
    {
        for (int x = y1; x < y2; x += level)
        {
            //diamond
            a = startArr[x + y * width]; //lo
            b = startArr[(x + level) + y * width]; //ro
            c = startArr[x + (y + level) * width]; //lu
            d = startArr[(x + level) + (y + level) * width]; //ru
            e = startArr[(x + level / 2) + (y + level / 2) * width] = Fit(((a + b + c + d) / 4) + Random(-1, 1) * range);
        }
    }
    for (int y = x1; y < x2; y += level)
    {
        for (int x = y1; x < y2; x += level)
        {
            a = startArr[x + y * width]; //lo
            b = startArr[(x + level) + y * width]; //ro
            c = startArr[x + (y + level) * width]; //lu
            d = startArr[(x + level) + (y + level) * width]; //ru
            e = startArr[(x + level / 2) + (y + level / 2) * width];

            //square
            startArr[(x + level / 2) + y * width] = Fit(((a + b + e) / 3) + Random(-0.5, 0.5) * range); //o
            startArr[(x + level) + (y + level / 2) * width] = Fit(((b + d + e) / 3) + Random(-0.5, 0.5) * range); //r
            startArr[(x + level / 2) + (y + level) * width] = Fit(((d + c + e) / 3) + Random(-0.5, 0.5) * range); //u
            startArr[x + (y + level / 2) * width] = Fit(((a + c + e) / 3) + Random(-0.5, 0.5) * range); //l
        }
    }
    DiamondSquareAlgorithm(x1, y1, x2, y2, range / 2, (level / 2));
};

float Fit(float x)是一个采用浮点数并将其与0和1进行比较的方法,因此没有大于1的值且没有小于0的值; float Random(float a, float b)只是在两个花车之间随机浮动。 heightfield