Java Array Rotate findAllPossibleRightRotations(int[][] matrix)

Here you can find the source of findAllPossibleRightRotations(int[][] matrix)

Description

returns all 360 rotations in clock-wise in given matrix

License

Open Source License

Parameter

Parameter Description
matrix a parameter

Declaration

public static List<int[][]> findAllPossibleRightRotations(int[][] matrix) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//from ww w.  j av a 2 s  . c o  m
     * returns all 360 rotations in clock-wise in given matrix
     * @param matrix
     * @return
     */
    public static List<int[][]> findAllPossibleRightRotations(int[][] matrix) {
        List<int[][]> allPossibleRotations = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            matrix = rotateClockWise(matrix);
            allPossibleRotations.add(matrix);
        }
        return allPossibleRotations;
    }

    /**
     * rotates the matrix to clock wise
     * @param pixels
     * @return
     */
    public static int[][] rotateClockWise(int[][] theArray) {

        int[][] rotate = new int[theArray[0].length][theArray.length];

        for (int i = 0; i < theArray[0].length; i++) {
            for (int j = 0; j < theArray.length; j++) {
                rotate[i][theArray.length - 1 - j] = theArray[j][i];
            }
        }
        return rotate;
    }
}

Related

  1. rotateArray(Object[] array, int amt)
  2. rotateArrayRange(int[] array, int from, int to, int n)