/// <summary>
        /// This will create a number spiral
        /// </summary>
        /// <param name="maxNumber">The highest number to go to
        /// in the spiral</param>
        private NumberSpiral(int maxNumber)
        {
            this.maxNumber = maxNumber;

            //Initialize the size of the array
            arrayLength = (int)Math.Floor(Math.Sqrt(maxNumber) + 1);
            spiralArray = new int[arrayLength, arrayLength];

            //Initialize the array with vales of -1.  This way it can be known what
            //array elements are unoccupied and will assist with printing the spiral.
            for (int y = 0; y < arrayLength; y++)
                for (int x = 0; x < arrayLength; x++)
                    spiralArray[x, y] = -1;

            //Find the starting indices
            X_Index = (int)Math.Ceiling((double)arrayLength / 2) - 1;
            Y_Index = X_Index;

            //Initialize movement directions
            X_Direction = X_Movement.Right;
            Y_Direction = Y_Movement.NotMoving;

            //Build the array
            for (int currentNumber = 0; currentNumber <= maxNumber; currentNumber++)
            {
                spiralArray[X_Index, Y_Index] = currentNumber;
                Move(currentNumber);
            }
        }
Beispiel #2
0
        /// <summary>
        /// This method moves the array indices to the next correct spot in
        /// the spiral array
        /// </summary>
        /// <param name="currentNumber">The current number being written to
        /// the spiral array</param>
        private void Move(int currentNumber)
        {
            //If not at a turning point, simply move to the next
            //position in the array
            if (currentNumber != X_Multiplier * Y_Multiplier)
            {
                X_Index += (int)X_Direction;
                Y_Index += (int)Y_Direction;
                return;
            }

            //Otherwise this is a turning point - decide what to do next

            //if moving in X, stop moving in X and change directions in Y
            if (Y_Direction == 0)
            {
                if (X_Direction == X_Movement.Right)
                {
                    Y_Direction = Y_Movement.Down;
                }
                else
                {
                    Y_Direction = Y_Movement.Up;
                }

                X_Direction = X_Movement.NotMoving;
                Y_Index    += (int)Y_Direction;
                X_Multiplier++;
                return;
            }

            //Otherwise, stop moving in Y and change
            //directions in X
            if (Y_Direction == Y_Movement.Down)
            {
                X_Direction = X_Movement.Left;
            }
            else
            {
                X_Direction = X_Movement.Right;
            }

            Y_Direction = Y_Movement.NotMoving;
            X_Index    += (int)X_Direction;
            Y_Multiplier++;
        }
        /// <summary>
        /// This method moves the array indices to the next correct spot in 
        /// the spiral array
        /// </summary>
        /// <param name="currentNumber">The current number being written to 
        /// the spiral array</param>
        private void Move(int currentNumber)
        {
            //If not at a turning point, simply move to the next
            //position in the array
            if (currentNumber != X_Multiplier * Y_Multiplier)
            {
                X_Index += (int)X_Direction;
                Y_Index += (int)Y_Direction;
                return;
            }

            //Otherwise this is a turning point - decide what to do next

            //if moving in X, stop moving in X and change directions in Y
            if (Y_Direction == 0)
            {
                if (X_Direction == X_Movement.Right)
                    Y_Direction = Y_Movement.Down;
                else
                    Y_Direction = Y_Movement.Up;

                X_Direction = X_Movement.NotMoving;
                Y_Index += (int)Y_Direction;
                X_Multiplier++;
                return;
            }

            //Otherwise, stop moving in Y and change
            //directions in X
            if (Y_Direction == Y_Movement.Down)
                X_Direction = X_Movement.Left;
            else
                X_Direction = X_Movement.Right;

            Y_Direction = Y_Movement.NotMoving;
            X_Index += (int)X_Direction;
            Y_Multiplier++;
        }