Java – rotate the matrix into position
•
Java
I'm solving the problem of rotating NxN matrix
It seems that my code is rotating, but it leaves an X on the image. So I guess it is rotating the edge incorrectly I attached two images as sample input and output
What's wrong with my code:
public static void rotateRight(float[][] img){ for (int i=0; i<N/2; i++){ for (int j=i; j<N-i; j++){ int J_COMP = N-j-1; //complement of J int LEFT = i; int RIGHT = N-i-1; int TOP = i; int BOTTOM = N-i-1; float temp = img[J_COMP][LEFT]; img[J_COMP][LEFT] = img[BOTTOM][J_COMP]; img[BOTTOM][J_COMP] = img[j][RIGHT]; img[j][RIGHT] = img[TOP][j]; img[TOP][j] = temp; } } }
Solution
You are rotating the main diagonal twice
Fix inner loop (see "fix" comments)
package tests.StackOverflow; public class Question_20773692 { private static int N; public static void main(String[] args) { float[][] img; int count; N=3; count = 0; img = new float[N][N]; for(int i=0; i<N; ++i) { for(int j=0; j<N; ++j) { img[i][j] = count++; } } printImg(img); rotateRight(img); printImg(img); } public static void printImg(float[][] img) { for(int j=0; j<N; ++j) { System.out.print("-"); } System.out.println(); for(int i=0; i<N; ++i) { for(int j=0; j<N; ++j) { System.out.print((int)(img[i][j])); } System.out.println(); } for(int j=0; j<N; ++j) { System.out.print("-"); } System.out.println(); } public static void rotateRight(float[][] img){ for (int i=0; i<N/2; i++){ for (int j=i; j<N-i; j++){ //for (int j=i+1; j<N-i; j++){ //fix int J_COMP = N-j-1; //complement of J int LEFT = i; int RIGHT = N-i-1; int TOP = i; int BOTTOM = N-i-1; float temp = img[J_COMP][LEFT]; img[J_COMP][LEFT] = img[BOTTOM][J_COMP]; img[BOTTOM][J_COMP] = img[j][RIGHT]; img[j][RIGHT] = img[TOP][j]; img[TOP][j] = temp; } } } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码