Advertisement
Guest User

holes

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