Advertisement
dmilicev

matrix_rotation_v1.c

Jan 17th, 2024
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.05 KB | None | 0 0
  1. /*
  2.  
  3.     matrix_rotation_v1.c        by Dragan Milicev
  4.  
  5. https://www.facebook.com/groups/3092811364081112/permalink/7581588861869984/
  6. https://www.geeksforgeeks.org/turn-an-image-by-90-degree/
  7. https://www.geeksforgeeks.org/inplace-rotate-square-matrix-by-90-degrees/
  8. https://prepinsta.com/c-program/rotate-a-matrix-by-90-degree-in-clockwise-direction/
  9.  
  10.     Matrix rotation:
  11.  
  12. - 90 degrees to the right, three versions
  13. - 90 degrees to the left
  14. - around the horizontal axis, two versions
  15. - around the vertical axis, two versions
  16.  
  17.  
  18. You can find all my C programs at Dragan Milicev's pastebin:
  19.  
  20. https://pastebin.com/u/dmilicev
  21.  
  22. https://www.facebook.com/dmilicev
  23.  
  24. */
  25.  
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h> // for exit()
  29.  
  30. #define MAX_SIZE 15
  31.  
  32.  
  33. // Displays the matrix M[r][k] that has r rows and k columns
  34. void print_matrix( char *text, int M[][MAX_SIZE], int r, int k )
  35. {
  36.     int i, j;
  37.  
  38.     printf("\n%s\n\n",text);
  39.  
  40.     for(i=0;i<r;i++) {
  41.         for(j=0;j<k;j++)
  42.             printf("%4d",M[i][j]);
  43.  
  44.         printf("\n\n");
  45.     }
  46.     printf("\n");
  47. }
  48.  
  49.  
  50. // Create the matrix M[r][k] that has r rows and k columns
  51. // Elements are ordinal numbers.
  52. void create_matrix( int M[][MAX_SIZE], int r, int k )
  53. {
  54.     int i, j;
  55.  
  56.     for(i=0;i<r;i++)
  57.         for(j=0;j<k;j++)
  58.             M[i][j] = i*k + j+1;
  59. }
  60.  
  61.  
  62. // Functions that do not change the matrix M, just displays rotated matrix
  63.  
  64.  
  65. // Displays a matrix rotated around a horizontal axis.
  66. void display_matrix_rotated_around_horizontal_axis( int M[][MAX_SIZE], int r, int k )
  67. {
  68.    int i,j;
  69.  
  70.     for(i=r-1;i>=0;i--){
  71.         for(j=0;j<k;j++){
  72.             printf(" %4d",M[i][j]);
  73.         }
  74.         printf("\n\n");
  75.     }
  76. }
  77.  
  78.  
  79. // Displays a matrix rotated around a horizontal axis, version 1.
  80. void display_matrix_rotated_around_horizontal_axis_v1( int M[][MAX_SIZE], int r, int k )
  81. {
  82.    int i,j,ip=r-1,jp;       // ip and jp are indexes for displaying M[ip][jp]
  83.  
  84.     for(i=0;i<r;i++){
  85.         jp=0;
  86.         for(j=0;j<k;j++)
  87.             printf(" %4d",M[ip][jp++]);
  88.         ip--;
  89.         printf("\n\n");
  90.     }
  91. }
  92.  
  93.  
  94. // Displays a matrix rotated around a vertical axis, version 1
  95. void display_matrix_rotated_around_vertical_axis_v1( int M[][MAX_SIZE], int r, int k )
  96. {
  97.    int i,j,ip=0,jp;     // ip and jp are indexes for displaying M[ip][jp]
  98.  
  99.     for(i=0;i<r;i++){
  100.         jp=k-1;
  101.         for(j=0;j<k;j++)
  102.             printf(" %4d",M[ip][jp--]);
  103.         ip++;
  104.         printf("\n\n");
  105.     }
  106. }
  107.  
  108.  
  109. // Displays a matrix rotated around a vertical axis
  110. void display_matrix_rotated_around_vertical_axis( int M[][MAX_SIZE], int r, int k )
  111. {
  112.    int i,j;
  113.  
  114.     for(i=0;i<r;i++){
  115.         for(j=k-1;j>=0;j--)
  116.             printf(" %4d",M[i][j]);
  117.         printf("\n\n");
  118.     }
  119. }
  120.  
  121.  
  122. // Displays the matrix rotated to the right by 90 degrees, version 2.
  123. // The first is the bottom left element and its column is displayed backwards as the first row.
  124. // Columns become rows and are displayed backwards.
  125. void display_matrix_rotated_right_90_degrees_v2( int M[][MAX_SIZE], int r, int k )
  126. {
  127.    int i,j,ip,jp=0;     // ip and jp are indexes for displaying M[ip][jp]
  128.  
  129.     for(i=0;i<r;i++){
  130.         ip=r-1;
  131.         for(j=0;j<k;j++)
  132.             printf(" %4d",M[ip--][jp]);
  133.         jp++;
  134.         printf("\n\n");
  135.     }
  136. }
  137.  
  138.  
  139. // Displays the matrix rotated to the right by 90 degrees, version 1.
  140. // The first is the bottom left element and its column is displayed backwards as the first row.
  141. // Columns become rows and are displayed backwards.
  142. void display_matrix_rotated_right_90_degrees_v1( int M[][MAX_SIZE], int r, int k )
  143. {
  144.     int i,j;
  145.  
  146.     for(j=0;j<k;j++){
  147.         for(i=r-1;i>=0;i--)
  148.             printf(" %4d",M[i][j]);
  149.         printf("\n\n");
  150.     }
  151. }
  152.  
  153.  
  154. // Displays the matrix rotated to the right by 90 degrees.
  155. // The first is the bottom left element and its column is displayed backwards as the first row.
  156. // Columns become rows and are displayed backwards.
  157. void display_matrix_rotated_right_90_degrees( int M[][MAX_SIZE], int r, int k )
  158. {
  159.     int i,j;
  160.  
  161.     for(i=0;i<k;i++){
  162.         for(j=k-1;j>=0;j--)
  163.             printf(" %4d",M[j][i]); // ATTENTION, indexes replaced places M[j][i]
  164.         printf("\n\n");
  165.     }
  166. }
  167.  
  168.  
  169. // Displays the matrix rotated to the left by 90 degrees.
  170. // The first is the top right element and its first right column is displayed as the first row.
  171. // Columns become rows.
  172. void display_matrix_rotated_left_90_degrees( int M[][MAX_SIZE], int r, int k )
  173. {
  174.    int i,j,ip,jp=k-1;   // ip and jp are indexes for displaying M[ip][jp]
  175.  
  176.     for(i=0;i<r;i++){
  177.         ip=0;
  178.         for(j=0;j<k;j++)
  179.             printf(" %4d",M[ip++][jp]);
  180.         jp--;
  181.         printf("\n\n");
  182.     }
  183. }
  184.  
  185.  
  186. // Copies matrix M to matrix M1, r is the number of rows, k is the number of columns
  187. void copy_matrix( int M1[][MAX_SIZE], int M[][MAX_SIZE], int r, int k )
  188. {
  189.     int i,j;
  190.  
  191.     for(i=0;i<r;i++)
  192.         for(j=0;j<k;j++)
  193.             M1[i][j] = M[i][j];
  194. }
  195.  
  196.  
  197. // Functions that change the matrix M when rotating
  198.  
  199.  
  200. // Rotates the matrix to the right 90 degrees.
  201. void rotate_matrix_right_90_degrees( int M[][MAX_SIZE], int r, int k )
  202. {
  203.     int M1[MAX_SIZE][MAX_SIZE];     // declare a new matrix M1
  204.     int i,j;
  205.  
  206.     copy_matrix(M1,M,r,k);          // copy matrix M to matrix M1
  207.  
  208.     // Form the matrix M again by taking from the matrix M1
  209.     // certain elements and put them in the matrix M.
  210.     for(i=0;i<r;i++)
  211.         for(j=0;j<k;j++)
  212.             M[i][j] = M1[k-1-j][i];
  213. }
  214.  
  215. // auxiliary function
  216. void swap(int *x, int *y){
  217.     int temp = *x;
  218.     *x = *y;
  219.     *y= temp;
  220. }
  221.  
  222. // Rotation the matrix to the right 90 degrees without auxiliary matrix,
  223. // ONLY FOR SQUARE MATRICES !
  224. void rotate_matrix_right_90_degrees_v1( int M[][MAX_SIZE], int r, int k )
  225. {
  226.     int i,j;
  227.  
  228.     //Rotate the matrix about the main diagonal
  229.     for(int i=0; i<r; i++)
  230.      for(int j=0; j<i; j++)
  231.          swap(&M[i][j], &M[j][i]); // The above auxiliary function for swapping places
  232.  
  233.     //Rotate the matrix about middle column
  234.     for(int i=0; i<r; i++)
  235.      for(int j=0; j<r/2; j++)
  236.         swap(&M[i][j], &M[i][r-j-1]); // The above auxiliary function for swapping places
  237. }
  238.  
  239.  
  240. // Rotates the matrix to the left 90 degrees.
  241. void rotate_matrix_left_90_degrees( int M[][MAX_SIZE], int r, int k )
  242. {
  243.     int M1[MAX_SIZE][MAX_SIZE];     // declare a new matrix M1
  244.     int i,j;
  245.  
  246.     copy_matrix(M1,M,r,k);          // Copy matrix M to matrix M1
  247.  
  248.     // Form the matrix M again by taking from the matrix M1
  249.     // certain elements and put them in the matrix M.
  250.     for(i=0;i<r;i++)
  251.         for(j=0;j<k;j++)
  252.             M[i][j] = M1[j][r-1-i];
  253. }
  254.  
  255.  
  256. // Rotates the matrix around the horizontal axis.
  257. void rotate_matrix_around_horizontal_axis( int M[][MAX_SIZE], int r, int k )
  258. {
  259.     int M1[MAX_SIZE][MAX_SIZE]; // declare a new matrix M1
  260.     int i,j;
  261.  
  262.     copy_matrix(M1,M,r,k);      // Copy matrix M to matrix M1
  263.  
  264.     // Form the matrix M again by taking from the matrix M1
  265.     // certain elements and put them in the matrix M.
  266.     for(i=0;i<r;i++)
  267.         for(j=0;j<k;j++)
  268.             M[i][j] = M1[r-i-1][j];
  269. }
  270.  
  271.  
  272. // Rotates the matrix around the vertical axis.
  273. void rotate_matrix_around_vertical_axis( int M[][MAX_SIZE], int r, int k )
  274. {
  275.     int M1[MAX_SIZE][MAX_SIZE]; // declare a new matrix M1
  276.     int i,j;
  277.  
  278.     copy_matrix(M1,M,r,k);      // Copy matrix M to matrix M1
  279.  
  280.     // Form the matrix M again by taking from the matrix M1
  281.     // certain elements and put them in the matrix M.
  282.     for(i=0;i<r;i++)
  283.         for(j=0;j<k;j++)
  284.             M[i][j] = M1[i][k-1-j];
  285. }
  286.  
  287.  
  288.  
  289. int main(void)
  290. {
  291.     int M[MAX_SIZE][MAX_SIZE];
  292.     int i, r=4, c=4;
  293.     // r is the number of rows and c is the number of columns of the matrix M[][]
  294.  
  295.     create_matrix(M,r,c);
  296.     print_matrix(" Matrix M is: ",M,r,c);
  297.  
  298.     printf("\n Functions that do not change the matrix M, just displays rotated matrix : \n\n");
  299.  
  300.     printf(" \n Matrix rotated to the right by 90 degrees is: \n\n");
  301.     display_matrix_rotated_right_90_degrees(M,r,c);
  302.  
  303.     printf(" \n Matrix rotated to the right by 90 degrees v1 is: \n\n");
  304.     display_matrix_rotated_right_90_degrees_v1(M,r,c);
  305.  
  306.     printf(" \n Matrix rotated to the right by 90 degrees v2 is: \n\n");
  307.     display_matrix_rotated_right_90_degrees_v2(M,r,c);
  308.  
  309.     printf(" \n Matrix rotated to the left by 90 degrees is: \n\n");
  310.     display_matrix_rotated_left_90_degrees(M,r,c);
  311.  
  312.     printf(" \n Matrix rotated around the horizontal axis is: \n\n");
  313.     display_matrix_rotated_around_horizontal_axis(M,r,c);
  314.  
  315.     printf(" \n Matrix rotated around the horizontal axis v1 is: \n\n");
  316.     display_matrix_rotated_around_horizontal_axis_v1(M,r,c);
  317.  
  318.     printf(" \n Matrix rotated around the vertical axis is: \n\n");
  319.     display_matrix_rotated_around_vertical_axis(M,r,c);
  320.  
  321.     printf(" \n Matrix rotated around the vertical axis v1 is: \n\n");
  322.     display_matrix_rotated_around_vertical_axis_v1(M,r,c);
  323.  
  324.     print_matrix(" Matrix M is: ",M,r,c);
  325.  
  326.  
  327.     printf("\n Functions that change the matrix M when rotating : \n\n");
  328.  
  329.     rotate_matrix_right_90_degrees(M,r,c);
  330.     print_matrix(" Matrix rotated to the right 90 degrees is: ",M,r,c);
  331.  
  332.     create_matrix(M,r,c);    // reset the matrix M because it was modified
  333.     rotate_matrix_left_90_degrees(M,r,c);
  334.     print_matrix(" Matrix rotated to the left 90 degrees is: ",M,r,c);
  335.  
  336.     create_matrix(M,r,c);    // reset the matrix M because it was modified
  337.     rotate_matrix_around_horizontal_axis(M,r,c);
  338.     print_matrix(" Matrix rotated around the horizontal axis is: ",M,r,c);
  339.  
  340.     create_matrix(M,r,c);    // reset the matrix M because it was modified
  341.     rotate_matrix_around_vertical_axis(M,r,c);
  342.     print_matrix(" Matrix rotated around the vertical axis is: ",M,r,c);
  343.  
  344.     create_matrix(M,r,c);    // reset the matrix M because it was modified
  345.     print_matrix(" Matrix M is: ",M,r,c);
  346.  
  347.  
  348.     // Rotation without auxiliary matrix, ONLY FOR SQUARE MATRICES !
  349.     rotate_matrix_right_90_degrees_v1(M,r,c);
  350.     print_matrix(" Matrix rotated to the right 90 degrees v1 is: ",M,r,c);
  351.  
  352.  
  353.     return 0;
  354. }
  355.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement