Java Array Find findAllOrientations(int[][] matrix)

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

Description

find all orientations of any matrix or piece provided

License

Open Source License

Parameter

Parameter Description
matrix a parameter

Declaration

public static List<int[][]> findAllOrientations(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   w  ww  . jav a  2  s  .co  m
     * find all orientations of any matrix or piece provided
     * @param matrix
     * @return
     */
    public static List<int[][]> findAllOrientations(int[][] matrix) {
        List<int[][]> allPossibleRotations = new ArrayList<>();
        allPossibleRotations.add(matrix);
        allPossibleRotations.add(flipInPlace(matrix));
        allPossibleRotations.add(mirror(matrix[0].length, matrix.length, matrix));

        allPossibleRotations.addAll(findAllPossibleRightRotations(allPossibleRotations.get(0)));

        allPossibleRotations.addAll(findAllPossibleRightRotations(allPossibleRotations.get(2)));

        return allPossibleRotations;
    }

    /**
     * returns the flipping of entire block/piece
     * @param theArray the original block content as matrix
     * @return
     */
    public static int[][] flipInPlace(int[][] theArray) {
        for (int i = 0; i < (theArray.length / 2); i++) {
            int[] temp = theArray[i];
            theArray[i] = theArray[theArray.length - i - 1];
            theArray[theArray.length - i - 1] = temp;
        }
        return theArray;
    }

    /**
     * returns the mirror of cube
     * @param width columns
     * @param height number of rows
     * @param theArray 
     * @return 
     */
    public static int[][] mirror(int width, int height, int[][] theArray) {
        int[][] out = new int[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                out[i][width - j - 1] = theArray[i][j];
            }
        }
        return out;
    }

    /**
     * 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. findAll(int[] arr1, int[] arr2)
  2. findAllArgumentPermutations(Object[][] allArguments)
  3. getIndex(double income, String[] scopes)
  4. getIndex(String[] array, String value)
  5. getIndexObject(Object[] data, Object object)
  6. getIndexOf(int i, int[] array)