我最近在 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);
}
该算法最初从整个图像的左上角和右下角开始,并一直通过(深度优先)。
我用这篇文章来设计算法。他们给出的示例如下所示:
这是我的一张图片的样子:
这是我的完整代码:
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();
}