I could not find a library that allows you to fill the matrix in a spiral, like this:
1 2 3
8 9 4
7 6 5
or vice versa – from the center outward.
Who knows how to do it yourself?
Answer 1, authority 100%
Use the getSpiralMatrix ()
method implemented below:
package com.example;
import java.util.stream.IntStream;
public class SpiralMatrix {
public int [] [] getSpiralMatrix (int n, boolean reverse) {
int [] [] matrix = new int [n] [];
IntStream.range (0, n) .forEach (i - & gt; matrix [i] = new int [n]);
int x = 0;
int y = 0;
int z = n;
for (int i = 0, j = n * n; i & lt; j;) {
matrix [x] [y] = reverse? j - i ++: ++ i;
if (x & lt; z + (n - z) / 2 - 1 & amp; & amp; y == (n - z) / 2) {
x ++;
} else if (x == z + (n - z) / 2 - 1 & amp; & amp; y & lt; z + (n - z) / 2 - 1) {
y ++;
} else if (x & gt; (n - z) / 2 & amp; & amp; y == z + (n - z) / 2 - 1) {
x--;
} else if (x == (n - z) / 2 & amp; & amp; y & gt; (n - z) / 2 + 1) {
y--;
} else {
x ++;
z - = 2;
}
}
return matrix;
}
public static void main (String [] args) {
SpiralMatrix sp = new SpiralMatrix ();
int n = 9;
int [] [] matrix = sp.getSpiralMatrix (n, true);
printMatrix (matrix);
System.out.println ("==========================");
matrix = sp.getSpiralMatrix (n, false);
printMatrix (matrix);
}
private static void printMatrix (int [] [] matrix) {
IntStream.range (0, matrix.length) .forEach (i - & gt; {
IntStream.range (0, matrix.length) .forEach (j - & gt; System.out.printf ("% 02d", matrix [j] [i]));
System.out.println ();
});
}
}
This demo code will print:
81 80 79 78 77 76 75 74 73
50 49 48 47 46 45 44 43 72
51 26 25 24 23 22 21 42 71
52 27 10 09 08 07 20 41 70
53 28 11 02 01 06 19 40 69
54 29 12 03 04 05 18 39 68
55 30 13 14 15 16 17 38 67
56 31 32 33 34 35 36 37 66
57 58 59 60 61 62 63 64 65
============================
01 02 03 04 05 06 07 08 09
32 33 34 35 36 37 38 39 10
31 56 57 58 59 60 61 40 11
30 55 72 73 74 75 62 41 12
29 54 71 80 81 76 63 42 13
28 53 70 79 78 77 64 43 14
27 52 69 68 67 66 65 44 15
26 51 50 49 48 47 46 45 16
25 24 23 22 21 20 19 18 17
Answer 2
// at PascalABC.net
// Matrix in a spiral from any corner or from the center, clockwise or counterclockwise
begin
var n: = 3;
var FromCenter: = 0; // 0 - from corner 1 - from Center
Var Center: = Ceil (n / 2) - 1; // The coordinate of the center of the matrix
var (i, j, Direction, nEven): = (0, 0, 1, n.IsEven? 1: 0); // from the top left
if FromCenter = 1 then (i, j, Direction, nEven): = (Center, Center, 1, 0); // from the center
var Matrix: = new integer [n, n];
for var Element: = 1 to n * n do
begin
Matrix [i, j]: = Element; {
[i, j] - from the top left clockwise; from the Center to the right clockwise
[j, i] - from the top left counterclockwise; from Center down counterclockwise
[n-1-i, j] - from the bottom left counterclockwise; from Center to the right counterclockwise
[n-1-j, i] - from the bottom left clockwise; from the center up clockwise
[j, n-1-i] - from the top right clockwise; from Center down clockwise
[i, n-1-j] - top right counterclockwise; from Center to the left counterclockwise
[n-1-i, n-1-j] - from the bottom right clockwise; from Center to the left clockwise
[n-1-j, n-1-i] - from the bottom right counterclockwise; from the center up clockwise
}
case Direction of
1: begin {right}
inc (j);
if (Center - i + FromCenter) = (j - Center - nEven) then Direction: = 2;
end;
2: begin {down}
inc (i);
IF i = j pin direction: = 3;
end;
3: Begin {Left}
DEC (J);
if (I - center) = (center - J + NEVEN) Then direction: = 4;
end;
4: Begin {up}
DEC (I);
if (i - 1) = (J - to the center) of the direction: = 1;
End.
end;
end;
Matrix.printLN;
end.