3

我最近在 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();
    }
4

2 回答 2

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

这些中点在几个方格之间共享,不应由最后一个方格重置以调用该方法,而是取平均值。

于 2014-02-20T21:13:37.673 回答
0

不久前我遇到了类似的问题,解决方案实际上相当简单。错误在于您仅对围绕中点的角进行平均,而不是对围绕它的整个“菱形”进行平均。因此,它将正方形伪影留在后面,因此简单的解决方案是找到您已经平均的两个角的平均值以及左右正方形的中心,如果左侧或右侧没有正方形,那么您可以让它成为或添加平均值加上一个随机数,另外你调用 genMap 四次,然后导致它到达一个节点的底部,然后继续下一个,目前这不是问题,但是一旦您实施了完整的菱形步骤,您会发现它们永远不是左右感觉的邻居,您不会一次计算每个级别。要解决这个问题,只需调用一次函数并让 for 循环遍历特定大小的每个“扇区”。

于 2014-03-31T02:23:38.097 回答