java - How to generate concentric layers in a matrix -


i need generate matrix aspect:

enter image description here

but more layers , can't find way it. understand, each color has n layers (in example, n=2), , there can m colors (in example, m=3). inner matrix, green one, should follow same spiral pattern others, in image wrong. next color, yellow in case, needs start "surrounding" previous matrix starting in top left, filling 1 layer , continuing next "layer" in top left too, , on. colors aren't important, important numbers in each cell.

any ideas?

ps: forget 10 , 34 in green, modifications.

ps2: example filled hand, can size of matrix, 256x256 impossible.

one strategy start innermost layer, , fill them while going outwards. way, core loop becomes particularly simple, because can walk through relevant part of matrix, , fill fields not filled yet.

the "relevant" part of matrix can computed within loops on colors layers: each layer, total size (width , height) of rectangle covered 1 layer increases 2. when desired number of layers has been filled, "counter" used fill matrix reset zero, indicate new color starts.

an 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;         (int color=0; color<colors; color++)         {             int coloroffset = (colors - color - 1) * layers;             int counter = 1;             (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;                 (int r=r0; r<r1; r++)                 {                     (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[][])     {         (int r=0; r<matrix.length; r++)         {             (int c=0; c<matrix[r].length; c++)             {                 system.out.printf("%4d", matrix[r][c]);             }             system.out.println();         }         system.out.println();     }  } 

for case depicted in question, 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 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -