All pastes #693806 Raw Edit

spiral...

public text v1 · immutable
#693806 ·published 2007-09-12 13:18 UTC
rendered paste body
static void Main()
{
    int N = 13, res, first, theNum = 0, index;
    N += 1 - N & 1;
    for ( int i = 0 ; i < N ; i++ )
    {
        for ( int j = 0 ; j < N ; j++ )
        {
            int row = i, col = j;

            if ( row > N / 2 )                    // if it's the cottomside of the square
                row = N - N % 2 - row;           //  make it the top index of the current square
            if ( col > N / 2 )                  //   if it's the right side of the square
                col = N - N % 2 - col;         //    make it the left inedx of the current square
            res = N / 2 - Math.Min(row, col); //     the index of the current square, the most center is 0.

            SetColor(res);                  // make the hole square the same color

            if ( res != 0 )
            {
                index = 1 + 2 * ( res - 1 ); // this is a temp variable...user later simply to save time...
                first = index * index;      //  calculate the first number in the current square...
            }
            else
            {
                first = 0;  // the first square, doesn't work with the (continued...)
                index = 0; //  above calculations so i made them statically...
            }
            if ( index == 0 ) // first square
                theNum = 0;  //  this is the only numebr in the first square...
            else if ( row == N / 2 - res ) // if this is the top or bottom line of the square...
            {                             //
                if ( row == i )          //   if this is the top line
                {
                    theNum = first + index;     // the first number in the top line (most right of it)
                    theNum += N / 2 + res - j; //  plus how much this is lefter than the first number
                }
                else                                    // it's the bottom line of the square
                {                                      //  
                    theNum = first + index + res * 4; //   the first number in the last line (the most left of it)
                    theNum += j + res - N / 2;       //    plus how much to add
                }
            }
            else                                             // this is a left or right line of the square...
            {                                               //  
                if ( col == j )                            //   left line...
                {                                         //
                    theNum = first + index + res * 2;    //     the first number in the line (top)
                    theNum += i - N / 2 + res;          //      plus how much to add
                }                                      //
                else                                  //        right side...
                {                                    //
                    theNum = first;                 //          the first number in the line (bottom)
                    theNum += res + N / 2 - i - 1; //           plus how much to add
                }                                 //
            }                                    //
            Console.Write("{0,4} ", theNum);
        }
        Console.WriteLine();
    }
    Console.BackgroundColor = ConsoleColor.Black;
}

private static void SetColor(int res)
{
    Console.BackgroundColor = (ConsoleColor)res;
    if ( res > 6 )
        Console.ForegroundColor = ConsoleColor.Black;
    else
        Console.ForegroundColor = ConsoleColor.Gray;
}