Dwarrowdelf.TerrainGen.DiamondSquare.GetGridValue C# (CSharp) Method

GetGridValue() static private method

Get value from the grid, or if the point is outside the grid, a value averaged between two corners
static private GetGridValue ( Context ctx, IntVector2 p ) : double
ctx Context
p IntVector2
return double
        static double GetGridValue(Context ctx, IntVector2 p)
        {
            var grid = ctx.Grid;
            var corners = ctx.Corners;

            double v1, v2;
            double len;
            int pos;

            if (p.X < 0)
            {
                len = grid.Height;
                v1 = corners.SW;
                v2 = corners.NW;
                pos = p.Y;
            }
            else if (p.Y < 0)
            {
                len = grid.Width;
                v1 = corners.SW;
                v2 = corners.SE;
                pos = p.X;
            }
            else if (p.X >= grid.Width)
            {
                len = grid.Height;
                v1 = corners.SE;
                v2 = corners.NE;
                pos = p.Y;
            }
            else if (p.Y >= grid.Height)
            {
                len = grid.Width;
                v1 = corners.NW;
                v2 = corners.NE;
                pos = p.X;
            }
            else
            {
                return grid[p];
            }

            var m = (v2 - v1) / len;
            var h = m * pos + v1;

            return h;
        }