Advertisement
Guest User

Untitled

a guest
Jan 9th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. package HomeTasksTwoStars.Arrays;
  2.  
  3. import static myPrograms.ShowMatrixMethod.showMatrix;
  4.  
  5. /**
  6. * Created by Slava Provalov on 29.12.2015.
  7. */
  8. public class SpiralCreator {
  9. public static void main(String[] args) {
  10. int rows = 5;
  11. int cols = 7;
  12. showMatrix(createSpiral(rows, cols));
  13. }
  14.  
  15. public static int[][] createSpiral(int rows, int cols) {
  16. int[][] spiralMatrix = new int[rows][cols];
  17. int row = 0;
  18. int col = 0;
  19. int colsCounter = 1;
  20. int rowsCounter = 0;
  21. int dirChangesCounter = 0;
  22. int wayEndCounter = cols;
  23. for (int i = 1; i <= rows * cols; i++) {
  24. spiralMatrix[row][col] = i;
  25. wayEndCounter--;
  26. if (wayEndCounter == 0) {
  27. wayEndCounter = cols * (dirChangesCounter % 2) + rows * ((dirChangesCounter + 1) % 2) - (dirChangesCounter / 2 - 1) - 2;
  28. dirChangesCounter++;
  29. int temp = colsCounter;
  30. colsCounter = -rowsCounter;
  31. rowsCounter = temp;
  32. }
  33. col += colsCounter;
  34. row += rowsCounter;
  35. }
  36. return spiralMatrix;
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement