分形地形总是在底部有一座山

时间:2015-05-14 21:49:17

标签: java algorithm

我正在开展一个项目,我几乎完成了地形生成,但我遇到了一个小问题。地形的底部总是一座山。我没有尝试过任何工作。任何效率改进也是受欢迎的。

这是我的算法:

private void generateTerrain() {
    Random rand = new Random();
    int seed = rand.nextInt(panel.maxSeed - panel.minSeed) + panel.minSeed;
    int offset = 10000;
    int sideLength = panel.mapSize - 1;
    int halfSideLength;
    int average;
    int yTop;
    int yBottom;

    panel.map[0][0] = seed;
    panel.map[panel.mapSize - 1][0] = seed;
    panel.map[0][panel.mapSize - 1] = seed;
    panel.map[panel.mapSize - 1][panel.mapSize - 1] = seed;

    while (sideLength > 0) {
        halfSideLength = sideLength / 2;

        for (int x = 0; x < panel.mapSize - 1; x += sideLength) {
            for (int y = 0; y < panel.mapSize - 1; y += sideLength) {
                average = panel.map[x][y]
                        + panel.map[x + sideLength][y]
                        + panel.map[x][y + sideLength]
                        + panel.map[x + sideLength][y + sideLength];
                average /= 4;

                if (rand.nextBoolean()) {
                    average += rand.nextInt(offset);
                } else {
                    average -= rand.nextInt(offset);
                }

                panel.map[x + halfSideLength][y + halfSideLength] = average;

                yTop = y - halfSideLength;
                yBottom = y + halfSideLength;

                if (yTop < 0) yTop = y + halfSideLength;
                if (yBottom > panel.mapSize - 1) yBottom = y - halfSideLength;

                average = panel.map[x][y]
                        + panel.map[x + sideLength][y]
                        + panel.map[x + halfSideLength][yTop]
                        + panel.map[x + halfSideLength][yBottom];
                average /= 4;


                panel.map[x + halfSideLength][y] = average;
            }
        }

        for (int x = 0; x < panel.mapSize; x += sideLength) {
            for (int y = 0; y < panel.mapSize - 1; y += sideLength) {
                int xRight = x + halfSideLength;

                if (xRight > panel.mapSize - 1) xRight = x - halfSideLength;

                average = panel.map[x][y]
                        + panel.map[x][y + sideLength]
                        + panel.map[xRight][y + halfSideLength]
                        + panel.map[xRight][y + halfSideLength];
                average /= 4;

                panel.map[x][y + halfSideLength] = average;

            }
        }

        offset *= 0.5;
        sideLength /= 2;
        if (offset < 1) {
            offset = 1;
        }
    }

    for (int x = 0; x < panel.mapSize - 1; x++) {
        for (int y = 0; y < panel.mapSize - 1; y++) {
            panel.map[x][y] = (int) ((panel.colors.length - 1) - panel.map[x][y] / 500);
            if (panel.map[x][y] < 0) {
                panel.map[x][y] = 0;
            } else if (panel.map[x][y] > 20) {
                panel.map[x][y] = 20;
            }
        }
    }
    panel.drawImage();
} 

1 个答案:

答案 0 :(得分:0)

我知道您已将我对original code提出的一些建议纳入其中。

我提出的解决方案有三个循环。您加入了前两个循环,但循环的限制是不同的。这意味着您不需要为底线设置值。 (这些可能初始化为零,因此它们在最终地图中显示为山脉。)

这是一个fence-post error。举例说明:

+-----+-----+-----+-----+
|     |     |     |     | 
|     |     |     |     | 
+-----+-----+-----+-----+
|     |     |     |     | 
|     |     |     |     | 
+-----+-----+-----+-----+
|     |     |     |     | 
|     |     |     |     | 
+-----+-----+-----+-----+
|     |     |     |     | 
|     |     |     |     | 
+-----+-----+-----+-----+

在这张4×4地图中,

  • 第一个循环处理4×4正方形的中心点,
  • 第二个循环处理4×5水平线的中点和
  • 第三个循环处理5×4垂直线的中点。

另一个问题是从高度值计算颜色索引的最后一个循环应该包括右边框和底部边框上的地图值,但是你可能只需要它们作为地形创建的辅助值。