我需要编写一个方法,将矩阵(对象)旋转90度,两个方向。
例如 -
( 0,0,0) (0,0,0) (0,0,0) (0,0,0)
( 1,1,1) (1,1,1) (1,1,1) (1,1,1)
( 2,2,2) (2,2,2) (2,2,2) (2,2,2)
成为 -
(2,2,2) (1,1,1) (0,0,0)
(2,2,2) (1,1,1) (0,0,0)
(2,2,2) (1,1,1) (0,0,0)
(2,2,2) (1,1,1) (0,0,0)
我无法克服“ArrayIndexOutOfBoundsException”错误。
这是我目前的工作 -
private RGBColor [][] _picture;
private int _row;
private int _col;
public RGBImage(RGBColor[][] pixels)
{
_picture = new RGBColor [pixels.length][pixels[0].length];
_row = pixels.length;
_col = pixels[0].length;
for(int i = 0 ; i < pixels.length ; i++)
{
for(int j = 0 ; j < pixels[0].length ; j++)
{
_picture[i][j] = pixels[i][j];
}
}
}
public double[][] toGrayscaleArray ( )
{
double [][] copyToGrayscale = new double [_row][_col];
for(int i = 0 ; i < _row ; i++)
{
for(int j = 0 ; j < _col ; j++)
{
copyToGrayscale [i][j] = _picture[i][j].convertToGrayscale();
}
}
return copyToGrayscale;
}
public void rotateCounterClockwise ( )
{
RGBImage copy = new RGBImage(_col,_row);
for(int i=0 ; i < _col ; i++)
{
for(int j=_row-1 ; j >0 ; j--)
{
copy._picture[i][j]=_picture[j][i];
}
}
_picture = copy.toRGBColorArray();
}
我该怎么办? 感谢。
答案 0 :(得分:0)
尝试:
for(int i = amount_columns-1; i>=0; i--) {
for(int j = amount_rows-1; j>=0; j--) {
new image[(amount_cols-1)-j][(amount_rows-1)-i] = old image[j][i];
}
}
这段代码应该执行以下操作:
amount_colums = 4
amount_row = 3
new image[(amount_cols-1)-j][(amount_rows-1)-i] = old image[j][i]
i=3;j=2 --> new image[4-1-3 = 0][3-1-2 = 0] = old image [2][3];
i=3;j=1 --> new image[4-1-3 = 0][3-1-1 = 1] = old image [1][3];
i=3;j=0 --> new image[4-1-3 = 0][3-1-0 = 2] = old image [0][3];
i=2;j=2 --> new image[4-1-2 = 1][3-1-2 = 0] = old image [2][2];
i=2;j=1 --> new image[4-1-2 = 1][3-1-1 = 1] = old image [1][2];
i=2;j=0 --> new image[4-1-2 = 1][3-1-0 = 2] = old image [0][2];
i=1;j=2 --> new image[4-1-1 = 2][3-1-2 = 0] = old image [2][1];
... and so on.
如果它不起作用,我现在无法测试。
答案 1 :(得分:0)
我认为这会奏效:
int[][] arr = new int[][]{
{0,0,0},
{1,1,1},
{2,2,2},
{3,3,3},
{4,4,4},
};
int[][] rotated = new int[arr[0].length][arr.length];
for (int row = arr.length -1; row >= 0; row--){
int destCol = arr.length -1 - row;
int destRow = 0;
for(int col = 0; col < arr[0].length; col++){
rotated[destRow++][destCol] = arr[row][col];
}
}
for(int row = 0; row < rotated.length; row++){
for(int col = 0; col < rotated[0].length; col++) {
System.out.print(rotated[row][col] + ",");
}
System.out.println();
}
根据您的需要,更好地使用jai API。或者也许从java Graphic2D类转换为affini。
答案 2 :(得分:0)
import java.util.Scanner;
class Rotate90
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the row ");
byte row=sc.nextByte();
System.out.println("enter the column");
byte column=sc.nextByte();
int arr[][]=new int[row][column];
// enterMatrixData(scan, matrix, matrixRow, matrixCol);
System.out.println("row entered is: "+row+",\ncolumn entered is: "+column);
System.out.println("now enter the maitix data");
for(int j=0;j<arr.length;j++)
{
for(int i=0;i<arr.length;i++)
{
arr[j][i]=sc.nextInt();
}
}
for(int j=0;j<arr.length;j++)
{
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[j][i]+"\t");
}
System.out.println("");
}
System.out.println("");
System.out.println("After 90* rotation");
for(int j=0;j<arr.length;j++) // logic for clock-wise 90* rotation
{
for(int i=arr.length-1;i>=0;i--)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println("");
}
}
}