How to create concentric layers in a matrix

I need to create a matrix with this aspect:

enter image description here

But with a lot of layers and I can't seem to find a way to do it. To understand, each color has n

layers (in this example n=2

), and there can be colors m

(in this example m=3

). The inner matrix, green, must follow the same spiral pattern as the others in the image are wrong. The next color, yellow in this case, should start "encircling" the previous matrix, starting at the upper left corner, filling one layer and continuing with the next layer in the upper left corner, and so on. The colors themselves are not important, the numbers in each cell are important.

Any ideas?

PS: Forget about 10 and 34 green, these are just modifications.

PS2: This example was filled in by hand, what can I do for this matrix size, but for 256x256 it would be impossible.

+3


source to share


1 answer


One strategy would be to start with the innermost layer and fill them in as you go outside. This makes the kernel loop especially easy because you can simply walk through the relevant part of the matrix and only fill in the fields that are not yet filled.

The "corresponding" part of the matrix can be easily calculated inside loops over the colors of the layers: with each layer, the total size (width and height) of the rectangle, which is covered by one layer, increases by 2. When the required number of layers has been filled, the "counter" used for matrix fill is reset to zero to indicate that a new color starts.

Example:



public class LayeredMatrix
{
    public static void main(String[] args)
    {
        test(1,1);
        test(2,2);
        test(3,3);
        test(2,3);
    }

    private static void test(int layers, int colors)
    {
        System.out.println(layers+" layers, "+colors+" colors");
        print(generate(layers, colors));
    }

    private static int[][] generate(int layers, int colors)
    {
        int size = layers * colors * 2;
        int matrix[][] = new int[size][size];
        int layerSize = 2;
        for (int color=0; color<colors; color++)
        {
            int colorOffset = (colors - color - 1) * layers;
            int counter = 1;
            for (int layer = 0; layer < layers; layer++)
            {
                int layerOffset = layers - layer - 1;
                int r0 = colorOffset + layerOffset;
                int c0 = colorOffset + layerOffset;
                int r1 = r0 + layerSize;
                int c1 = c0 + layerSize;
                for (int r=r0; r<r1; r++)
                {
                    for (int c=c0; c<c1; c++)
                    {
                        if (matrix[r][c] == 0)
                        {
                            matrix[r][c] = counter;
                            counter++;
                        }
                    }
                }
                layerSize += 2;
            }
        }
        return matrix;
    }

    private static void print(int matrix[][])
    {
        for (int r=0; r<matrix.length; r++)
        {
            for (int c=0; c<matrix[r].length; c++)
            {
                System.out.printf("%4d", matrix[r][c]);
            }
            System.out.println();
        }
        System.out.println();
    }

}

      

In the case pictured in the question, it prints

2 layers, 3 colors
  37  38  39  40  41  42  43  44  45  46  47  48
  49   1   2   3   4   5   6   7   8   9  10  50
  51  11  21  22  23  24  25  26  27  28  12  52
  53  13  29   1   2   3   4   5   6  30  14  54
  55  15  31   7   5   6   7   8   8  32  16  56
  57  17  33   9   9   1   2  10  10  34  18  58
  59  19  35  11  11   3   4  12  12  36  20  60
  61  21  37  13  13  14  15  16  14  38  22  62
  63  23  39  15  16  17  18  19  20  40  24  64
  65  25  41  42  43  44  45  46  47  48  26  66
  67  27  28  29  30  31  32  33  34  35  36  68
  69  70  71  72  73  74  75  76  77  78  79  80

      

+1


source







All Articles