void DiamondSquare() { /* * Uses the diamond square algorithm * Sets the 4 corners of the grid to a random value * Each time the code iterates through the i variable, there will be 2^i squares that the program will have to iterate through * Each square is passed throught the Step method, which will then set the designated height values */ // Set the heights of the corners Vector2[] vs = new[] { new Vector2(0, 0), new Vector2(gridSize - 1, 0), new Vector2(gridSize - 1, gridSize - 1), new Vector2(0, gridSize - 1) }; foreach (Vector2 v in vs) { verts.SetHeight(v, RandomInitialHeight()); if (verts.GetHeight(v) > currentMaxHeight) { currentMaxHeight = verts.GetHeight(v); } } LowerHeight(); for (int i = 0; i < nVal; i++) { int sqrtSquares = (int)Math.Pow(2, i); float divided = (gridSize - 1) / sqrtSquares; for (int j = 0; j < sqrtSquares; j++) { for (int k = 0; k < sqrtSquares; k++) { Vector2 p1 = new Vector2(k, j) * divided; Vector2 p3 = new Vector2(k + 1, j + 1) * divided; Step(p1, p3); } } LowerHeight(); } }
void MedianFilter() { var window = new List <float>(); float newHeight; int checkWindowOffsetX; int checkWindowOffsetY; int adjustedWindowWidthX; int adjustedWindowWidthY; HeightGrid copy = verts.Copy(); for (int x = 0; x < gridSize; x++) { adjustedWindowWidthX = GetAdjustedWindowWidth(x); checkWindowOffsetX = GetWindowOffset(x); for (int y = 0; y < gridSize; y++) { adjustedWindowWidthY = GetAdjustedWindowWidth(y); checkWindowOffsetY = GetWindowOffset(y); for (int fx = 0; fx < adjustedWindowWidthX; fx++) { for (int fy = 0; fy < adjustedWindowWidthY; fy++) { window.Add(copy.GetHeight(new Vector2(x + fx - checkWindowOffsetX, y + fy - checkWindowOffsetY))); } } window.Sort(); newHeight = window[adjustedWindowWidthX * adjustedWindowWidthY / 2]; verts.SetHeight(new Vector2(x, y), newHeight); window.Clear(); } } }