3

私は最近、Diamond-Square プロシージャル生成アルゴリズムの実装を C# でまとめました。ただし、生成されたノイズには、処理される「正方形」の間に非常に明白な境界があります。擬似コードは次のようになります

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

アルゴリズムは、最初に画像全体の左上隅と右下隅から開始し、(深さ優先) 処理を進めます。

この記事を使用して、アルゴリズムを設計しました。彼らが与える例は次のようになります。

ここに画像の説明を入力

これが私の画像の1つです。

ここに画像の説明を入力

ここに私の完全なコードがあります:

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

私は少し前に同様の問題を抱えていましたが、解決策は実際にはかなり単純でした。間違いは、中点を囲む「ダイヤモンド」全体を平均するのではなく、中点を囲む角だけを平均していることです。したがって、正方形のアーティファクトが残るため、単純な解決策は、すでに平均化している2つのコーナーの平均と、左右の正方形の中心を見つけることです。左右に正方形がない場合は、それをそのままにするか、平均と乱数を追加することができます。さらに、genMapを4回呼び出しているため、ノードの一番下に到達してから次のノードに進みます。現在、これは問題ではありませんが、完全なダイヤモンド ステップを実装すると、一度に各レベルを計算しなくても、左または右の意味で決して隣接しないことがわかります。これを修正するには、関数を 1 回呼び出して、特定のサイズの各「セクター」を for ループで処理します。

于 2014-03-31T02:23:38.097 に答える