gpt4 book ai didi

c# - Diamond-Square 算法不产生 "smooth"噪声

转载 作者:行者123 更新时间:2023-11-30 18:30:13 27 4
gpt4 key购买 nike

我最近用 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来设计算法。他们给出的示例如下所示:

enter image description here

这是我的一张图片的样子:

enter image description here

这是我的完整代码:

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;
}
}

对正方形之间共享的中点取平均值后:

enter image description here

更新的迭代代码:

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();
}

最佳答案

这是我的猜测,因为我现在无法访问 ide。

您正在将值重写为此处新方 block 之间的点:

    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);

这些中点由多个方 block 共享,不应由最后一个调用该方法的方 block 重置,而应取平均值。

关于c# - Diamond-Square 算法不产生 "smooth"噪声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21918931/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com