Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package HomeTasksTwoStars.Arrays;
- import static myPrograms.ShowMatrixMethod.showMatrix;
- /**
- * Created by Slava Provalov on 29.12.2015.
- */
- public class SpiralCreator {
- public static void main(String[] args) {
- int rows = 5;
- int cols = 7;
- showMatrix(createSpiral(rows, cols));
- }
- public static int[][] createSpiral(int rows, int cols) {
- int[][] spiralMatrix = new int[rows][cols];
- int row = 0;
- int col = 0;
- int colsCounter = 1;
- int rowsCounter = 0;
- int dirChangesCounter = 0;
- int wayEndCounter = cols;
- for (int i = 1; i <= rows * cols; i++) {
- spiralMatrix[row][col] = i;
- wayEndCounter--;
- if (wayEndCounter == 0) {
- wayEndCounter = cols * (dirChangesCounter % 2) + rows * ((dirChangesCounter + 1) % 2) - (dirChangesCounter / 2 - 1) - 2;
- dirChangesCounter++;
- int temp = colsCounter;
- colsCounter = -rowsCounter;
- rowsCounter = temp;
- }
- col += colsCounter;
- row += rowsCounter;
- }
- return spiralMatrix;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement