Advertisement
dmilicev

matrix_rotation.c

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