Matrix Rotation 90 degrees (keep it up)

Source: Internet
Author: User

An image is represented as a nxn matrix. each pixel in the image is 4 bytes. Write a function to rotate the image 90 degrees. Can you perform operations in the same place? (That is, do not open up additional storage space)

The first question is the element of an exchange matrix:

For example, 3*3 Matrices

1 2 3
4 5 6
7 8 9
First, process the first line and rotate four elements counterclockwise at a time. The following is a second operation.
3 2 9 3 6 9
4 5 6 2 5 8
1 8 7 1 4 7
The first second time

For a matrix of 5*5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Here is the change after the first line.
5 2 3 4 25
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 22 23 24 21
1st times

5 10 3 4 25
6 7 8 9 24
11 12 13 14 15
2 17 18 19 20
1 22 23 16 21
2nd times

5 10 15 4 25
6 7 8 9 24
3 12 13 14 23
2 17 18 19 20
1 22 11 16 21
3rd Times

5 10 15 20 25
4 7 8 9 24
3 12 13 14 23
2 17 18 19 22
1 6 11 16 21
4th times


After the first line is processed
What is not processed is a 3*3 matrix.
7 8 9
12 12 13
17 18 19
The result is obtained after processing in 3*3.
5 10 15 20 25
4 9 14 19 24
3 8 13 18 23
2 7 12 17 22
1 6 11 16 21
However, when writing code, this encoding method is troublesome. It is very troublesome to handle array subscript calculation.

The following describes another method.
Let's look at the 4*4 matrix.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
We wire the elements on both sides of the main diagonal line:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Then the result is obtained by swapping the elements of row I and row n-i-1.
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13

The following code is used:

# Define N 4 void swap (Int & vleft, Int & vright) {int temp = vleft; vleft = vright; vright = temp ;} // rotate void rotate_matrix (INT vmat [N] [N]) counterclockwise {// first swap the elements on both sides of the main diagonal line. If it is clockwise, for (INT I = 0; I <n ;++ I) {for (int K = I + 1; k <n ;++ K) {swap (vmat [I] [K], vmat [k] [I]) ;}// element for (INT I = 0; I <n/2; ++ I) {for (int K = 0; k <n; ++ K) {swap (vmat [I] [K], vmat [N-i-1] [k]) ;}} void print_matrix (INT vmat [N] [N]) {for (INT I = 0; I <n; ++ I) {for (int K = 0; k <n; ++ K) {printf ("% 3d", vmat [I] [k]);} printf ("\ n") ;}} int main () {int a [n] [N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; print_matrix (a); printf ("\ n"); rotate_matrix (); print_matrix (a); System ("pause"); Return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.