【发布时间】:2014-01-31 18:50:32
【问题描述】:
所以我想解决我上一篇文章中描述的问题:Terrain/Mountain algorithm not working as intended。问题的副本如下:
我想使用一个非常基本的原理创建一个上面有山的地形,如下图所示:
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 2 1 0 0 0 0
0 0 0 1 2 3 2 1 0 0 0
0 0 1 2 3 4 3 2 1 0 0
0 0 0 1 2 3 2 1 0 0 0
0 0 0 0 1 2 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
它从height = 4 的随机点开始,然后逐渐减少它的邻居。
递归的思路很简单,我开始一个点,用height - 1(在这个例子中)递归到上/下/左/右,只有还没有遇到时,我才设置它们的值。
所以我继续使用 BFS 实现它:
private void createMountain(final float[][] heightMapping, final float startHeight) {
boolean[][] traversed = new boolean[width][depth];
boolean positive = (startHeight >= 0f);
int x = random.nextInt(width);
int z = random.nextInt(depth);
PriorityQueue<QueueElement> priorityQueue = new PriorityQueue<>((o1, o2) -> (int)Math.signum(o1.height - o2.height));
priorityQueue.offer(new QueueElement(x, z, startHeight));
while (!priorityQueue.isEmpty()) {
QueueElement current = priorityQueue.poll();
if (current.x < 0 || current.x >= width || current.z < 0 || current.z >= depth) {
continue;
}
if (traversed[current.x][current.z]) {
continue;
}
if ((positive && current.height <= 0f) || (!positive && current.height >= 0f)) {
continue;
}
heightMapping[x][z] = current.height;
priorityQueue.offer(new QueueElement(x, z - 1, calculateNewHeight(current.height, positive)));
priorityQueue.offer(new QueueElement(x, z + 1, calculateNewHeight(current.height, positive)));
priorityQueue.offer(new QueueElement(x - 1, z, calculateNewHeight(current.height, positive)));
priorityQueue.offer(new QueueElement(x + 1, z, calculateNewHeight(current.height, positive)));
}
}
private class QueueElement {
public int x, z;
public float height;
public QueueElement(final int x, final int z, final float height) {
this.x = x;
this.z = z;
this.height = height;
}
}
private float calculateNewHeight(final float startHeight, final boolean positive) {
float delta = startHeight / maxDecayFactor;
return (positive) ? startHeight - delta : startHeight + delta;
}
现在代码永远不会停止,我尝试调试了几次,但要么没有得到有用的结果,要么地形完全平坦。
我目前唯一真正的线索是priorityQueue.size() 一直在增加 3。
有人知道发生了什么吗?
更新:即使在修复之后,它仍然无法按预期工作。
0.6 0.5 0.4 0.3 0.3 0.2 0.2 0.1 0.1 0.1 0.1 0.1 0.0 0.0 0.0 0.0
0.8 1.0 1.2 1.6 1.9 2.4 3.0 3.8 4.7 5.9 7.4 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
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