/// <summary> /// Sets the initial values of the square by putting values in just the 4 corners of the array. /// </summary> /// <param name="maxSize"></param> /// <param name="square"></param> /// <param name="squareCoords"></param> static void initSquare(int maxSize, int[,] square, Square squareCoords) { square[squareCoords.UpperLeft.x, squareCoords.UpperLeft.y] = spike.RandSpike; // TL square[squareCoords.LowerLeft.x, squareCoords.LowerLeft.y] = spike.RandSpike; // BL square[squareCoords.UpperRight.x, squareCoords.UpperRight.y] = spike.RandSpike; // TR square[squareCoords.LowerRight.x, squareCoords.LowerRight.y] = spike.RandSpike; // BR }
/// <summary> /// Finds midpoint of current square, calculated from 4 corners /// </summary> /// <param name="numOfSteps"></param> /// <param name="square"></param> /// <param name="squareCoords"></param> static void diamondStep(int numOfSteps, int[,] square, Square squareCoords) { // Find the midpoint by doing vector math and dividing by 4 int centerX = (squareCoords.UpperLeft.x + squareCoords.UpperRight.x + squareCoords.LowerLeft.x + squareCoords.LowerRight.x) / 4; int centerY = (squareCoords.UpperLeft.y + squareCoords.UpperRight.y + squareCoords.LowerLeft.y + squareCoords.LowerRight.y) / 4; Point diamondPoint = new Point(centerX, centerY); // Add to the array at above calculated locations square[diamondPoint.x, diamondPoint.y] = spike.RandSpike; squareStep(numOfSteps - 1, square, squareCoords, diamondPoint); }
static void Main(string[] args) { int userInput; // Take user input, for array size, and force into an acceptible size Console.Write("Enter a value for n in (2^n) + 1 to calculate array size: "); userInput = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(); int maxSize = Convert.ToInt32(Math.Pow(2, userInput)) + 1; int[,] square = new int[maxSize, maxSize]; int[,] finalSquare = new int[maxSize, maxSize]; int numOfSteps = 2 * userInput; // Holds the max number of iterations for algorithm Square squareCoordinates = new Square(maxSize); initSquare(maxSize, square, squareCoordinates); diamondStep(numOfSteps - 1, square, squareCoordinates); displayArray(maxSize, square); Console.ReadLine(); }
/// <summary> /// Performs the square step. Which is composed of finding the midpoint of each edge of the square. /// </summary> /// <param name="numOfSteps"></param> /// <param name="square"></param> /// <param name="squareCoords"></param> /// <param name="middlePoint"></param> /// <returns></returns> static int[,] squareStep(int numOfSteps, int[,] square, Square squareCoords, Point middlePoint) { // Perform calculations of new squares in temp variables to prevent destructive writes Point leftMidpoint = new Point((squareCoords.UpperLeft.x + squareCoords.LowerLeft.x) / 2, (squareCoords.UpperLeft.y + squareCoords.LowerLeft.y) / 2); Point rightMidpoint = new Point((squareCoords.UpperRight.x + squareCoords.LowerRight.x) / 2, (squareCoords.UpperRight.y + squareCoords.LowerRight.y) / 2); Point topMidpoint = new Point((squareCoords.UpperLeft.x + squareCoords.UpperRight.x) / 2, (squareCoords.UpperLeft.y + squareCoords.UpperRight.y) / 2); Point bottomMidpoint = new Point((squareCoords.LowerLeft.x + squareCoords.LowerRight.x) / 2, (squareCoords.LowerLeft.y + squareCoords.LowerRight.y) / 2); // Set values in array square[leftMidpoint.x, leftMidpoint.y] = spike.RandSpike; square[rightMidpoint.x, leftMidpoint.y] = spike.RandSpike; square[bottomMidpoint.x, bottomMidpoint.y] = spike.RandSpike; square[topMidpoint.x, topMidpoint.y] = spike.RandSpike; //create smaller squares generated by points //current square should create 4 new squares every time diamond and square are completed Square topLeftSquare = new Square(squareCoords.UpperLeft, topMidpoint, leftMidpoint, middlePoint); Square topRightSquare = new Square(topMidpoint, squareCoords.UpperRight, middlePoint, rightMidpoint); Square bottomLeftSquare = new Square(leftMidpoint, middlePoint, squareCoords.LowerLeft, bottomMidpoint); Square bottomRightSquare = new Square(middlePoint, rightMidpoint, bottomMidpoint, squareCoords.LowerRight); // Perform until there are no more free spaces in array. if (numOfSteps != 0) { diamondStep(numOfSteps - 1, square, topLeftSquare); diamondStep(numOfSteps - 1, square, topRightSquare); diamondStep(numOfSteps - 1, square, bottomLeftSquare); diamondStep(numOfSteps - 1, square, bottomRightSquare); } return square; }