Advertisement
Guest User

holes

a guest
Apr 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. //this idea was from Evan Kohilas on Friday's help session (21/4/17)
  2.  
  3.  
  4. #include <stdio.h>
  5. #include "captcha.h"
  6.  
  7. int no_neighbours(int k, int g, int N_HEIGHT, int N_WIDTH, int n[N_HEIGHT][N_WIDTH]);
  8. int find_lowest(int k, int g, int N_HEIGHT, int N_WIDTH, int n[N_HEIGHT][N_WIDTH]);
  9. int replace(int k, int g, int N_HEIGHT, int N_WIDTH, int n[N_HEIGHT][N_WIDTH], int to_be_replaced, int replacement);
  10. int merge(int k, int g, int N_HEIGHT, int N_WIDTH, int n[N_HEIGHT][N_WIDTH], int replacement);
  11.  
  12. // this function
  13. int no_neighbours(int k, int g, int N_HEIGHT, int N_WIDTH, int n[N_HEIGHT][N_WIDTH]) {
  14. return n[k][g+1]==0 && n[k-1][g]==0 && n[k][g-1]==0 && n[k+1][g]==0;
  15. }
  16.  
  17. int find_lowest(int k, int g, int N_HEIGHT, int N_WIDTH, int n[N_HEIGHT][N_WIDTH]){ // this function finds the lowest number in the (surrounding) array
  18. int adj[4]={n[k][g+1], n[k-1][g], n[k][g-1], n[k+1][g]};//must handle the case when n[-1][0]
  19. int i=1;
  20. int min=adj[0];
  21. while(i < 4) {
  22. if(adj[i]>0 && adj[i]<min && adj[i]!=0) {
  23. min=adj[i];
  24. }
  25. i++;
  26. }
  27. return min;
  28. }
  29.  
  30. //FOR TODAY-- NEED TO MAKE REPLACE FUNCTION
  31. // we need to scan the entire array again and replace every number in the array that is "replacement" with adj[i]
  32. int replace(int k, int g, int N_HEIGHT, int N_WIDTH, int n[N_HEIGHT][N_WIDTH], int to_be_replaced, int replacement){
  33.  
  34. k=0;
  35. while(g < N_HEIGHT-1) {
  36. g=0;
  37. while(g < N_WIDTH-1) {
  38. if(n[k][g]==to_be_replaced) {
  39. n[k][g]=replacement;
  40. }
  41. g++;
  42. }
  43. k++;
  44. }
  45. return replacement;
  46. }
  47.  
  48. //function that finds numbers that aren't 0 or replacement
  49. int merge(int k, int g, int N_HEIGHT, int N_WIDTH, int n[N_HEIGHT][N_WIDTH], int replacement){
  50. int adj[4]={n[k][g+1], n[k-1][g], n[k][g-1], n[k+1][g]};
  51. int i=1;
  52. int min=adj[0];
  53. int to_be_replaced;
  54. int m=0;
  55.  
  56. while(i < 4) {
  57. //n, 2, 1
  58. if(adj[i]>0 && adj[i]!=0 && adj[i]!=replacement) {
  59. to_be_replaced=adj[i];
  60. }
  61. i++;
  62. }
  63.  
  64. if(n[k][g]==to_be_replaced) {
  65. replace(k, g, N_HEIGHT, N_WIDTH, n, to_be_replaced, replacement)==n[k][g];
  66. m++;
  67. }
  68. return m;
  69. }
  70.  
  71.  
  72. int get_holes(int height, int width, int pixels[height][width]){
  73. int num=1; //replacement number
  74. int x=1, y=1;
  75. int replacement;
  76. int m=0; // number of total merges
  77. int number_of_holes;
  78. int to_be_replaced;
  79. int N_HEIGHT=height+2;
  80. int N_WIDTH=width+2;
  81. int k=x+1;
  82. int g=y+1;
  83.  
  84. printf("hello");
  85. int n[N_HEIGHT][N_WIDTH];//new array where all numeros to 0 and has a border of zeroes around array
  86. for(k=0; k < N_HEIGHT; k++){
  87. for(g=0; g < N_WIDTH; g++){
  88. n[k][g] = 0;
  89. }
  90. }
  91. printf("hello");
  92. k=1;
  93. while(k < N_HEIGHT-1) {
  94. g=1;
  95. while(g < N_HEIGHT-1) {
  96. //printf("%d\n %d\n", x, y);
  97. if (pixels[x][y]==0) {
  98.  
  99. if(no_neighbours(k, g, height, width, n)) { //if everything around the pixel is zero, then change the pixel value to num, and num increments
  100. n[k][g]=num;
  101. num++;
  102.  
  103. } else { //if surrounding pixels are not all zero, the pixel value becomes the lowest adjacent number
  104. replacement=find_lowest(k, g, height, width, n);
  105. merge(k, g, height, width, n, replacement);
  106. printf("lol");
  107. }
  108.  
  109.  
  110. } else {
  111. y++;
  112. }
  113.  
  114. }
  115. x++;
  116. }
  117. number_of_holes = num - m - 2;
  118.  
  119. printf("num is: %d\n", num);
  120. printf("m is: %d\n", m);
  121. printf("number of holes is: %d\n", number_of_holes);
  122. return number_of_holes;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement