Untitled

 avatar
unknown
plain_text
2 years ago
1.9 kB
3
Indexable
using System;

class GridIterator
{
    static void Main()
    {
        int gridSize = 5; // Adjust this value to change the size of the grid
        int centerX = 2;   // Adjust this value to change the x-coordinate of the center
        int centerY = 2;   // Adjust this value to change the y-coordinate of the center

        IterateGridInRings(gridSize, centerX, centerY);
    }

    static void IterateGridInRings(int gridSize, int centerX, int centerY)
    {
        int ringCount = (gridSize + 1) / 2;

        for (int ring = 0; ring < ringCount; ring++)
        {
            int minX = centerX - ring;
            int maxX = centerX + ring;
            int minY = centerY - ring;
            int maxY = centerY + ring;

            for (int x = minX; x <= maxX; x++)
            {
                int y1 = centerY - ring;
                int y2 = centerY + ring;

                if (IsValidCoordinate(gridSize, x, y1))
                {
                    Console.WriteLine($"Ring {ring + 1}: ({x}, {y1})");
                }

                if (y1 != y2 && IsValidCoordinate(gridSize, x, y2))
                {
                    Console.WriteLine($"Ring {ring + 1}: ({x}, {y2})");
                }
            }

            for (int y = minY + 1; y < maxY; y++)
            {
                int x1 = centerX - ring;
                int x2 = centerX + ring;

                if (IsValidCoordinate(gridSize, x1, y))
                {
                    Console.WriteLine($"Ring {ring + 1}: ({x1}, {y})");
                }

                if (x1 != x2 && IsValidCoordinate(gridSize, x2, y))
                {
                    Console.WriteLine($"Ring {ring + 1}: ({x2}, {y})");
                }
            }
        }
    }

    static bool IsValidCoordinate(int gridSize, int x, int y)
    {
        return x >= 0 && x < gridSize && y >= 0 && y < gridSize;
    }
}
Editor is loading...