Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  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. int** strip(const matrixOptimizationValue* matrix, const int rows, const int columns, int** arr_lengths){
  39. //Checking if Matrix has measurments.
  40. if(rows == 0 || columns == 0){
  41. printf("nMatrix has no measurments. Error. n");
  42. exit(0);
  43. }
  44.  
  45. //Calculating maximal distance of an element to borders.
  46. const int max_dis = (columns > rows) ? round((double)(rows)/2) : round((double)(columns)/2);
  47. //Creating future return_value
  48. matrixOptimizationValue** unstriped_matrix = malloc(max_dis*sizeof(int*));
  49. (*arr_lengths) = calloc(max_dis+1, sizeof(matrixOptimizationValue));
  50. (*arr_lengths)[0] = max_dis;
  51. //Packing...
  52. int x_minborder = 0; //Borders
  53. int y_minborder = 0;
  54. int x_maxborder = columns;
  55. int y_maxborder = rows;
  56.  
  57. for(int i = 0; i < max_dis; i++){
  58. int elements_size=0; //Unfortunately, making "const" causes really bad syntax...
  59. if(x_maxborder-x_minborder <= 1 || y_maxborder-y_minborder <=1){
  60. if(x_maxborder-x_minborder <= 1 && y_maxborder-y_minborder <=1){
  61. elements_size = 1;
  62. } else {
  63. elements_size = (x_maxborder-x_minborder == 1) ? y_maxborder-y_minborder : x_maxborder-x_minborder;
  64. }
  65. } else {
  66. elements_size = 2*(x_maxborder-x_minborder + y_maxborder-y_minborder)-4;
  67. }
  68. matrixOptimizationValue* array = calloc(elements_size, sizeof(int));
  69. (*arr_lengths)[i+1]=elements_size;
  70. //Initializing coordinates
  71. int x = i; //It is certain, that this coordinates will always belong to fields array_i;
  72. int y = i;
  73.  
  74. int dir_x = 0; //Direction
  75. int dir_y = 1;
  76.  
  77. for(int j = 0; j < elements_size; j++){
  78. array[j] = matrix[y*columns + x];
  79. if(x+dir_x >= x_maxborder || x+dir_x < x_minborder || y+dir_y >= y_maxborder || y+dir_y < y_minborder){
  80. /*Rotate Direction-vector using Algebra
  81. x
  82. y
  83. 0 1 y
  84. -1 0 -x
  85. */
  86. const int clone = dir_x;
  87. dir_x = dir_y;
  88. dir_y = -clone;
  89. }
  90. x += dir_x;
  91. y += dir_y;
  92. }
  93. x_minborder++;
  94. y_minborder++;
  95. x_maxborder--;
  96. y_maxborder--;
  97. unstriped_matrix[i]=array;
  98. }
  99. return unstriped_matrix;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement