我最近在C#中整合了Diamond-Square程序生成算法的实现。然而,产生的噪声在"正方形之间具有非常明显的边界。与之合作。伪代码看起来像这样
gen()
{
This takes the two outer corners (upper left and lower right) as parameters (i.e. (0,0) and (4,4))
Change center point of square using average of outer four corners and a random weight change.
Change four "diamond point" midpoints of the four sides of the square using the same idea.
gen(topRightCorner, centerPoint);
gen(topMidpoint,rightMidpoint);
gen(leftMidpoint,bottomMidpoint);
gen(centerPoint, bottomRightCorner);
}
算法最初从整个图像的左上角和右下角开始,然后一直工作(深度优先)。
我使用this article来设计算法。他们给出的例子如下:
以下是我的一张图片:
这是我的完整代码:
public class _3DMapGenerator
{
public _3DMapGenerator(int powerOf2)
{
sideLength = (int)Math.Pow(2, powerOf2) + 1;
for (int x = 0; x < sideLength; x++)
{
for (int y = 0; y < sideLength; y++)
{
data.Add(new Point(x, y), 0.5M);
}
}
}
int sideLength;
Random r = new Random();
public Dictionary<Point, decimal> data = new Dictionary<Point, decimal>();
public void genMap(Point p1 = null,Point p2 = null)
{
if(p1 == null || p2 == null)
{
p1 = new Point(0, 0);
p2 = new Point(sideLength - 1, sideLength - 1);
}
Point centerPoint = new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
if (p2.x - p1.x < 2 || p2.y - p1.y < 2)
{
return;
}
decimal swing = ((decimal)(1+p2.x - p1.x))/sideLength;
Point p1_2 = new Point(p2.x, p1.y);
Point p2_1 = new Point(p1.x, p2.y);
Console.WriteLine("Points: " + p1 + " " + p1_2 + " " + p2_1 + " " + p2);
//Console.ReadLine();
data[centerPoint] = ((decimal)(data[p1] + data[p2] + data[p1_2] + data[p2_1])) / 4 + ((decimal)r.NextDouble() * swing) - (swing / 2);
Point mP1 = Point.getMidpoint(p1, p1_2);
Point mP2 = Point.getMidpoint(p1, p2_1);
Point mP3 = Point.getMidpoint(p1_2, p2);
Point mP4 = Point.getMidpoint(p2_1, p2);
swing /= 2;
data[mP1] = ((decimal)(data[p1] + data[p1_2])) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2);
data[mP2] = ((decimal)(data[p1] + data[p2_1])) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2);
data[mP3] = ((decimal)(data[p1_2] + data[p2])) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2);
data[mP4] = ((decimal)(data[p2_1] + data[p2])) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2);
genMap(p1, centerPoint);
genMap(mP1, mP3);
genMap(mP2, mP4);
genMap(centerPoint, p2);
}
public void printToImage(string fileName)
{
Bitmap bmp = DrawFilledRectangle(sideLength,sideLength);
foreach(var o in data)
{
bmp.SetPixel(o.Key.x, o.Key.y, Color.FromArgb((int)(255 * o.Value), (int)(255 * o.Value), (int)(255 * o.Value)));
}
bmp.Save(fileName);
}
private static Bitmap DrawFilledRectangle(int x, int y)
{
Bitmap bmp = new Bitmap(x, y);
using (Graphics graph = Graphics.FromImage(bmp))
{
Rectangle ImageSize = new Rectangle(0, 0, x, y);
graph.FillRectangle(Brushes.White, ImageSize);
}
return bmp;
}
}
对平方之间共享的中点上的值进行平均:
更新了迭代代码:
public Dictionary<Point, List<decimal>> data = new Dictionary<Point, List<decimal>>();
static Random r = new Random();
public int sideLength;
public void genMap()
{
for (int sideLen = sideLength; sideLen >= 3; sideLen = sideLen / 2 + 1)
{
for (int yOff = 0; yOff + sideLen < sideLength + 1; yOff += sideLen - 1)
{
for (int xOff = 0; xOff + sideLen < sideLength + 1; xOff += sideLen - 1)
{
Point upL = new Point(xOff, yOff);
Point upR = new Point(xOff + sideLen - 1, yOff);
Point lowL = new Point(xOff, yOff + sideLen - 1);
Point lowR = new Point(xOff + sideLen - 1, yOff + sideLen - 1);
Point centerPoint = new Point((upL.x + lowR.x) / 2, (upL.y + lowR.y) / 2);
Point mPTop = Point.getMidpoint(upL, upR);
Point mPLeft = Point.getMidpoint(upL, lowL);
Point mPRight = Point.getMidpoint(upR, lowR);
Point mPBottom = Point.getMidpoint(lowL, lowR);
decimal swing = ((decimal)(1 + sideLen)) / (2 * sideLength);
set(mPTop, ((decimal)(get(upL) + get(upR))) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2));
set(mPLeft, ((decimal)(get(upL) + get(lowL))) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2));
set(mPRight, ((decimal)(get(upR) + get(lowR))) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2));
set(mPBottom, ((decimal)(get(lowL) + get(lowR))) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2));
swing *= 2;
set(centerPoint, ((decimal)(get(upL) + get(upR) + get(lowL) + get(lowR))) / 4 + ((decimal)r.NextDouble() * swing) - (swing / 2));
}
}
}
}
void set(int x, int y, decimal d)
{
set(new Point(x, y), d);
}
void set(Point p, decimal d)
{
data[p].Add(d);
}
Decimal get(int x, int y)
{
return get(new Point(x, y));
}
Decimal get(Point p)
{
if (data[p].Count == 0)
{
Console.WriteLine("No elements.");
return 0;
}
return data[p].Average();
}
答案 0 :(得分:0)
这是我的猜测,因为我现在无法访问ide。
您正在将值重写为新方块之间的点:
data[mP1] = ((decimal)(data[p1] + data[p1_2])) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2);
data[mP2] = ((decimal)(data[p1] + data[p2_1])) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2);
data[mP3] = ((decimal)(data[p1_2] + data[p2])) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2);
data[mP4] = ((decimal)(data[p2_1] + data[p2])) / 2 + ((decimal)r.NextDouble() * swing) - (swing / 2);
这些中点在几个正方形中共享,不应该被最后一个调整方法重置,而是平均调整。
答案 1 :(得分:0)
我很久以前遇到过类似的问题,解决方案实际上相当简单。错误在于你只是平均围绕中点的角落,而不是平均整个&#34;钻石&#34;环绕它。因此,它留下方形伪影,所以简单的解决方案是找到你已经平均的两个角的平均值和左边和右边的正方形的中心,如果左边或右面没有正方形那么你可以让它成为或添加平均值加一个随机数,此外你调用genMap四次导致它到达一个节点的底部然后继续到下一个,目前这不是问题但是一旦你实现完整钻石步骤你会发现他们从不是左或右的邻居,你不会一次计算每个等级。要解决此问题,只需调用该函数一次,然后让for循环遍历每个&#34;扇区&#34;具体大小。