Rotate matrix by 90º

Asked

Viewed 6,933 times

5

I need to create a C algorithm to rotate a 10x10 matrix by 90 degrees, however I cannot use an auxiliary matrix for this.

Simplifying what was asked to try to find some pattern and use it to solve the problem I used a 3x3 matrix and compared in a table the position that each element occupies in the original matrix and that will occupy in the rotated matrix.

ORIGINAL     | ROTACIONADA
0x0 0x1 0x2  | 0x2 1x2 2x2
1x0 1x1 1x2  | 0x1 1x1 2x1
2x0 2x1 2x2  | 0x0 1x0 2x0

You can notice a pattern in which for the original matrix the value of the line remains constant at each cycle of 3 positions and the value of the column is added at 1 in the same cycle. For the rotated version of the matrix the row and column values correspond respectively to the column and row. Despite that I do not know how to take advantage of this information, since transferring the values to the positions corresponding to the rotated version will lose values that I will still use.
For example:
Transferring the value from 0X0 to 2X0 will lose the original 2x0 content that would need to transfer to 2x2 later.

  • 3

    What have you done? And you don’t need an extra matrix, just an auxiliary variable inside a for...

  • In fact what I was able to do was just the previous reasoning, I could not make an algorithm precisely by limiting the loss of content. And as for the auxiliary variable I did some tests and I realized that I would need many "auxiliary variables", because in each cycle of three positions I need to save three values for the next cycles.

  • Dude, I edited your question to try to make it more visual with a 3x3 matrix, I could check if that’s what should really happen?

  • That’s exactly what should happen. Thank you!

  • Dude, the logic is pretty simple, you’re going to have to change the value between 4 positions, all of which follow a very simple logic. I’m kind of running now, but then I publish the answer. (:

  • I’ll post a possible response, if you really need to change the matrix let me know that I complete.

Show 1 more comment

3 answers

4

The simplest way to do this is to rotate the screen when printing. For a 3x3 integer matrix, i are the lines and j are the columns, you can display it "tipped" so:

for(j = 2; j >= 0; j--)       
{
        for(i = 0; i < 3; i++)
        {
               printf("%d      ", mat[i][j]);
        }
        printf("%c", 10);
}

This way you print, from right to left, columns as if they were rows, "rotating" their matrix at 90°.

4


To change the value of 2 variables it is usual to use a temporary variable.

// troca a e b
int tmp = a;
a = b;
b = tmp;

In case you run the matrix, you have to do the same but with 4 variables

// roda 4 variaveis
int tmp = a;
a = b;
b = c;
c = d;
d = tmp;

Now the problem is to select variables!

But it is not difficult to calculate which elements should rotate for each of the elements of the 5x5 sub-matrix from top to left.

The element in mat[0][0] must run with mat[9][0], mat[9][9], and mat[0][9].

The element in mat[1][3] must run with mat[3][8], mat[8][6], and mat[6][1].

The element in mat[row][col] must run with mat[col][10 - row - 1], mat[10 - row - 1][10 - col - 1], and mat[10 - col - 1][row].

Note: the algorithm has nothing to do with C.

1

In addition to the @Daviaragao response, you might want to save the matrix to it and rotate more than once, well follow the pattern to rotate an array with an auxiliary variable:

Each element of the matrix always varies between 4 positions (It would be equivalent to, in radians, 0, pi, pi/2 and 3*pi/2), when it completes the cycle, it returns to the initial position. This can be checked in the following image:

Posições de rotação de elementos de uma borda de uma matriz 4x4

This obeys rotation rules, which you must subtract from N (being N the size in the matrix, in the case of C would be N-1) the value of the element column (which in the case of C will start from 0) and invert row with column. The order you define these operations will define the direction in which the matrix is rotated:

  • Swap row and column and then subtract = Clockwise
  • Subtract and then swap row and column = Counterclockwise (same as image)

In summary you will have the following meta-algorithm (this for a Nxn square matrix and clockwise rotation):

para i = 0, i < N/2 e i = i + 1:
    para j = 0, j < N/2 e j = j + 1:
        original = matriz[i][j]
        linha = i
        coluna = j
        para l = 0, l < 3 e l = l + 1:
            se l % 2 = 0:
                matriz[linha][coluna] = matriz[linha][N-coluna]
            senao:
                matriz[linha][coluna] = matriz[N-linha][coluna]
            fimse
            aux = linha
            linha = coluna
            coluna = N-aux
        fimpara
        matriz[linha][coluna] = original
    fimpara
fimpara

This algorithm serves to rotate clockwise, for the counterclockwise simply change the order of the auxiliary variable and the line. It is worth remembering that the complexity of this algorithm is of O(n²), maybe there is a better way to solve this problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.