SHARE
TWEET

Untitled

a guest Jun 15th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. typedef int matrixOptimizationValue;
  2. /*
  3. Striping a matrix by packing numbers who have the same distance from matrix border.
  4. Note that the packing is anti-clockwise.
  5.  E.g:
  6.  
  7.  Example 1:
  8.  ___      ___
  9.  . 1 1 1 1  .         Field:
  10.  . 2 2 2 2  .          [1,2,3,3,3,3,2,1,1,1]
  11.  . 3 3 3 3  .  -->     [2,2]
  12.  ___      ___
  13.  
  14.  Example 2:
  15.  ___      ___         Field:
  16.  . 1 1 1 4 1  .        [1,2,3,3,3,3,2,1,1,1]
  17.  . 2 2 2 3 2  .        [2,3,3,2,3,2]
  18.  . 3 3 3 2 3  .  -->
  19.  . 1 2 4 5 6  .
  20.  ___      ___
  21.  
  22.  
  23.  returns:
  24.  return_value      - Field of Arrays; unpacked Matrix
  25.  arr_lengths       - Since fields arrays have a variable size, "*array_lengths" contains the following information.
  26.                      {Amount of arrays in the field, sizes_of_array01, sizes_of_array03 ... }
  27.                      Therefore "*array_lengths" size must be *array_lengths[0]+1.
  28.  
  29.  in:
  30.  const int* matrix - Matrix to be unpacked
  31.  int rows          - rows of the matrix
  32.  int columns       - columns of the matrix
  33.  
  34.  Possible Errors:
  35.     1. Matrix has no measurments.
  36.  
  37.  */
  38. matrixOptimizationValue** strip(const matrixOptimizationValue* matrix, const int rows, const int columns, int** arr_lengths){
  39.     //Checking if Matrix has measurments.
  40.     int* lengths = *arr_lengths;
  41.     if(rows == 0 || columns == 0){
  42.         printf("nMatrix has no measurments. Error. n");
  43.         exit(0);
  44.     }
  45.  
  46.     //Calculating maximal distance of an element to borders.
  47.     const int max_dis = (columns > rows) ? round((double)(rows)/2) : round((double)(columns)/2);
  48.     //Creating future return_value
  49.     matrixOptimizationValue** unstriped_matrix = malloc(max_dis*sizeof(int*));
  50.     (lengths) = malloc((max_dis+1) * sizeof(matrixOptimizationValue));
  51.     (lengths)[0] = max_dis;
  52.     //Packing...
  53.     int x_minborder = 0; //Borders
  54.     int y_minborder = 0;
  55.     int x_maxborder = columns;
  56.     int y_maxborder = rows;
  57.  
  58.     for(int i = 0; i < max_dis; i++){
  59.         int elements_size=0; //Unfortunately, making "const" causes really bad syntax...
  60.         if(x_maxborder-x_minborder <= 1 || y_maxborder-y_minborder <=1){
  61.             if(x_maxborder-x_minborder <= 1 && y_maxborder-y_minborder <=1){
  62.                 elements_size = 1;
  63.             } else {
  64.                 elements_size = (x_maxborder-x_minborder == 1) ?  y_maxborder-y_minborder : x_maxborder-x_minborder;
  65.             }
  66.         } else {
  67.             elements_size = 2*(x_maxborder-x_minborder + y_maxborder-y_minborder)-4;
  68.         }
  69.         matrixOptimizationValue* array = malloc(elements_size * sizeof(int));
  70.         (lengths)[i+1]=elements_size;
  71.         //Initializing coordinates
  72.         int x = i; //It is certain, that this coordinates will always belong to fields array_i;
  73.         int y = i;
  74.  
  75.         int dir_x = 0; //Direction
  76.         int dir_y = 1;
  77.  
  78.         for(int j = 0; j < elements_size; j++){
  79.             array[j] = matrix[y*columns + x];
  80.             if(x+dir_x >= x_maxborder || x+dir_x < x_minborder || y+dir_y >= y_maxborder || y+dir_y < y_minborder){
  81.                 /*Rotate Direction-vector using Algebra
  82.                          x
  83.                          y
  84.                  0  1    y
  85.                 -1  0   -x
  86.                  */
  87.                 const int clone = dir_x;
  88.                 dir_x = dir_y;
  89.                 dir_y = -clone;
  90.             }
  91.             x += dir_x;
  92.             y += dir_y;
  93.         }
  94.         x_minborder++;
  95.         y_minborder++;
  96.         x_maxborder--;
  97.         y_maxborder--;
  98.         unstriped_matrix[i]=array;
  99.     }
  100.     return unstriped_matrix;
  101. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top