块状菱形方法算法

时间:2014-08-13 19:36:09

标签: c# algorithm

对于我的项目,我已经实施了Diamond Square算法为我创建了一个高度图,但是我在代码中无法识别一些错误。

似乎高度图顶部的一个值,应该是由第一个方形迭代设置的,不会被设置,导致黑点,我无法弄清楚是什么导致地图的块状外观。不幸的是,由于我没有10个声望,我无法发布高度图的图像。

感谢任何帮助,提高性能的提示也将是一个很大的帮助。

代码:

public static float[,] DiamondSquare(int x, int y)
    {
        //Get the square + 1 size for our algorithm, to be cut down to x,y size at end.
        Point squareSize = FindSquareSize(x,y);

        float[,] squareValues = new float[squareSize.x, squareSize.y];

        //Set the corner values.
        squareValues[0, 0] = 0.5f;
        squareValues[squareSize.x - 1, 0] = 0.5f;
        squareValues[0, squareSize.y - 1] = 0.5f;
        squareValues[squareSize.x - 1, squareSize.y - 1] = 0.5f;


        float variation = 0.5f; //Roughness of the map. Decreases as the algorithm creates the map.
        int level = squareSize.x; //The current chunk the algorithm is working on. Whole, half, quaters, eighths, etc.
        //Debug.Log("Initial level is: " + level);

        //Do the algorithm here.
        while(level > 1)
        {
            int halfstep = level / 2;
            //Debug.Log("Half Step is: " + halfstep);

            //Do Diamond.
            for (int i = halfstep; i < squareSize.x; i += level)
            {
                for (int j = halfstep; j < squareSize.x; j += level)
                {
                    //Bottom left is (0,0)
                    float a = squareValues[i - halfstep, j - halfstep]; //Bottom left.
                    float b = squareValues[i + halfstep, j - halfstep]; //Bottom Right.
                    float c = squareValues[i - halfstep, j + halfstep]; //Top Left.
                    float d = squareValues[i + halfstep, j + halfstep]; //Top Right.

                    float e = ((a + b + c + d) / 4f) + Random.Range(0f, 1f) * variation; //Center

                    squareValues[i, j] = e;

                    //Debug.Log("Did a Diamond!");
                }
            }

            int currentColumn = 0;

            //Do Square.
            for (int i = 0; i < squareSize.x; i += halfstep)
            {
                currentColumn++;
                //If this is an odd column.
                if (currentColumn % 2 == 1)
                {
                    for (int j = halfstep; j < squareSize.y; j += level)
                    {
                        float a, b, c, d, e;

                        bool threePoints = false;

                        try { a = squareValues[i, j + halfstep]; } //Top
                        catch { a = 0; threePoints = true; }

                        try { b = squareValues[i, j - halfstep]; } //Bottom
                        catch { b = 0; threePoints = true; }

                        try { c = squareValues[i - halfstep, j]; } //Left
                        catch { c = 0; threePoints = true; }

                        try { d = squareValues[i + halfstep, j]; } //Right
                        catch { d = 0; threePoints = true; }

                        if (threePoints)
                        {
                            e = ((a + b + c + d) / 3f) + Random.Range(0f, 1f) * variation;
                        }
                        else
                        {
                            e = ((a + b + c + d) / 4f) + Random.Range(0f, 1f) * variation;
                        }

                        squareValues[i, j] = e;

                        //Debug.Log("Did a Square!");
                    }
                }
                //Else this is an even column.
                else
                {
                    for (int j = 0; j < squareSize.y; j += level)
                    {
                        float a, b, c, d, e;

                        bool threePoints = false;

                        try { a = squareValues[i, j + halfstep]; } //Top
                        catch { a = 0; threePoints = true; }

                        try { b = squareValues[i, j - halfstep]; } //Bottom
                        catch { b = 0; threePoints = true; }

                        try { c = squareValues[i - halfstep, j]; } //Left
                        catch { c = 0; threePoints = true; }

                        try { d = squareValues[i + halfstep, j]; } //Right
                        catch { d = 0; threePoints = true; }

                        if (threePoints)
                        {
                            e = ((a + b + c + d) / 3f) + Random.Range(0f, 1f) * variation;
                        }
                        else
                        {
                            e = ((a + b + c + d) / 4f) + Random.Range(0f, 1f) * variation;
                        }

                        squareValues[i, j] = e;

                        //Debug.Log("Did a Square!");
                    }
                }
            }

            //Reduce range and variation.
            level /= 2;
            variation /= 2;

        }

0 个答案:

没有答案