Advertisement
stefanpu

Print spiral matrix

Feb 16th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.94 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Loops__Homework;
  4.  
  5. namespace _14.SpiraleMatrix
  6. {
  7.     class SpiraleMatrix
  8.     {
  9.         private static uint[,] spiraleMatrix;
  10.         private static uint colStart;
  11.         private static uint rowStart;
  12.         private static uint colEnd;
  13.         private static uint rowEnd;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             uint matrixDimension = Functions.ValidateInput("matrix dimension", 20);
  18.             spiraleMatrix = new uint[matrixDimension, matrixDimension];
  19.  
  20.             GenerateSpirale(matrixDimension);
  21.             //PrintMatrix(spiraleMatrix);
  22.            
  23.         }
  24.  
  25.         private static void GenerateSpirale(uint matrixDimension)
  26.         {
  27.  
  28.             colStart = 0;
  29.             rowStart = 0;
  30.             colEnd = matrixDimension - 1;
  31.             rowEnd = matrixDimension - 1;
  32.  
  33.             uint counter = 1;
  34.  
  35.             uint matrixTotalLength = (uint)Math.Pow(matrixDimension, 2);
  36.  
  37.             //In  case of invalid inputs, the drawing should begin after the input text
  38.             // and in the beginning of the console.
  39.             int consoleTopPos = ++Console.CursorTop;
  40.             Console.CursorLeft=0;
  41.  
  42.             while (counter <= matrixTotalLength)
  43.             {
  44.                 //Left-to-Right
  45.                 counter = LeftToRight(counter, consoleTopPos);
  46.  
  47.                 //Top-to-bottom
  48.                 counter = TopToBottom(counter, consoleTopPos);
  49.  
  50.                 //Right-to-left
  51.                 counter = RightToLeft(counter, consoleTopPos);
  52.  
  53.                 // Bottom-to-top
  54.                 counter = BottomToTop(counter, consoleTopPos);
  55.             }
  56.             //This places the "Press any key..." text below the matrix, regardess of matrix size.
  57.             Console.CursorTop += (int)matrixDimension * 3 / 2 + 1;
  58.         }
  59.  
  60.         private static uint TopToBottom(uint counter, int consoleTopPos)
  61.         {
  62.             for (int i = (int)rowStart; i <= rowEnd; i++)
  63.             {
  64.                 // FOR DRAWING PURPOSES ONLY!!!
  65.                 //-----------------------------------------------------------
  66.                 Console.SetCursorPosition(6 * (int)colEnd + 1, 3 * i + consoleTopPos);
  67.                 Console.Write(counter);
  68.                 Thread.Sleep(150);
  69.                 //-----------------------------------------------------------
  70.                 spiraleMatrix[i, colEnd] = counter;
  71.                 counter++;
  72.             }
  73.             colEnd--;
  74.             return counter;
  75.         }
  76.  
  77.         private static uint LeftToRight(uint counter, int consoleTopPos)
  78.         {
  79.             for (int i = (int)colStart; i <= colEnd; i++)
  80.             {
  81.                 // FOR DRAWING PURPOSES ONLY!!!
  82.                 //-----------------------------------------------------------
  83.                 Console.SetCursorPosition(6 * i + 1, 3 * (int)rowStart + consoleTopPos);
  84.                 Console.Write(counter);
  85.                 Thread.Sleep(150);
  86.                 //-----------------------------------------------------------
  87.                 spiraleMatrix[rowStart, i] = counter;
  88.                 counter++;
  89.             }
  90.             rowStart++;
  91.             return counter;
  92.         }
  93.  
  94.         private static uint RightToLeft(uint counter, int consoleTopPos)
  95.         {
  96.             for (int i = (int)colEnd; i >= colStart; i--)
  97.             {
  98.                 // FOR DRAWING PURPOSES ONLY!!!
  99.                 //-----------------------------------------------------------
  100.                 Console.SetCursorPosition(6 * i + 1, 3 * (int)rowEnd + consoleTopPos);
  101.                 Console.Write(counter);
  102.                 Thread.Sleep(150);
  103.                 //-----------------------------------------------------------
  104.                 spiraleMatrix[rowEnd, i] = counter;
  105.                 counter++;
  106.             }
  107.             rowEnd--;
  108.             return counter;
  109.         }
  110.  
  111.         private static uint BottomToTop( uint counter, int consoleTopPos)
  112.         {
  113.             for (int i = (int)rowEnd; i >= rowStart; i--)
  114.             {
  115.                 // FOR DRAWING PURPOSES ONLY!!!
  116.                 //-----------------------------------------------------------
  117.                 Console.SetCursorPosition(6 * (int)colStart + 1, 3 * i + consoleTopPos);
  118.                 Console.Write(counter);
  119.                 Thread.Sleep(150);
  120.                 //-----------------------------------------------------------
  121.                 spiraleMatrix[i, colStart] = counter;
  122.                 counter++;
  123.             }
  124.             colStart++;
  125.             return counter;
  126.         }
  127.  
  128.         static void PrintMatrix<T>(T[,] matrix)
  129.         {
  130.             for (int row = 0; row < matrix.GetLength(0); row++)
  131.             {
  132.                 for (int col = 0; col < matrix.GetLength(1); col++)
  133.                 {
  134.                     Console.Write("{0,3}", matrix[row,col]);
  135.                 }
  136.                 Console.WriteLine();                
  137.             }
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement