Guest User

Untitled

a guest
Jul 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. private void FloodFill(Point node, Color targetColor, Color replaceColor)
  2. {
  3. //perform bounds checking X
  4. if ((node.X >= CANVAS_SIZE) || (node.X < 0))
  5. return; //outside of bounds
  6.  
  7. //perform bounds checking Y
  8. if ((node.Y >= CANVAS_SIZE) || (node.Y < 0))
  9. return; //ouside of bounds
  10.  
  11. //check to see if the node is the target color
  12. if (pixels[node.X, node.Y].CellColor != targetColor)
  13. return; //return and do nothing
  14. else
  15. {
  16. pixels[node.X, node.Y].CellColor = replaceColor;
  17.  
  18. //recurse
  19. //try to fill one step to the right
  20. FloodFill(new Point(node.X + 1, node.Y), targetColor, replaceColor);
  21. //try to fill one step to the left
  22. FloodFill(new Point(node.X - 1, node.Y), targetColor, replaceColor);
  23. //try to fill one step to the north
  24. FloodFill(new Point(node.X, node.Y - 1), targetColor, replaceColor);
  25. //try to fill one step to the south
  26. FloodFill(new Point(node.X, node.Y + 1), targetColor, replaceColor);
  27.  
  28. //exit method
  29. return;
  30. }
  31. }
  32.  
  33. private void QueueFloodFill(Point node, Color targetColor, Color replaceColor)
  34. {
  35. Queue<Point> points = new Queue<Point>();
  36. if (pixels[node.X, node.Y].CellColor != targetColor)
  37. return;
  38.  
  39. points.Enqueue(node);
  40.  
  41. while (points.Count > 0)
  42. {
  43. Point n = points.Dequeue();
  44. if (pixels[n.X, n.Y].CellColor == targetColor)
  45. pixels[n.X, n.Y].CellColor = replaceColor;
  46.  
  47. if (n.X != 0)
  48. {
  49. if (pixels[n.X - 1, n.Y].CellColor == targetColor)
  50. points.Enqueue(new Point(n.X - 1, n.Y));
  51. }
  52.  
  53. if (n.X != CANVAS_SIZE - 1)
  54. {
  55. if (pixels[n.X + 1, n.Y].CellColor == targetColor)
  56. points.Enqueue(new Point(n.X + 1, n.Y));
  57. }
  58.  
  59. if (n.Y != 0)
  60. {
  61. if (pixels[n.X, n.Y - 1].CellColor == targetColor)
  62. points.Enqueue(new Point(n.X, n.Y - 1));
  63. }
  64.  
  65. if (n.Y != CANVAS_SIZE - 1)
  66. {
  67. if (pixels[n.X, n.Y + 1].CellColor == targetColor)
  68. points.Enqueue(new Point(n.X, n.Y + 1));
  69. }
  70. }
  71. DrawCanvas();
  72. return;
  73. }
  74.  
  75. private void RevisedQueueFloodFill(Point node, Color targetColor, Color replaceColor)
  76. {
  77. Queue<Point> q = new Queue<Point>();
  78. if (pixels[node.X, node.Y].CellColor != targetColor)
  79. return;
  80.  
  81. q.Enqueue(node);
  82. while (q.Count > 0)
  83. {
  84. Point n = q.Dequeue();
  85. if (pixels[n.X, n.Y].CellColor == targetColor)
  86. {
  87. Point e = n;
  88. Point w = n;
  89. while ((w.X != 0) && (pixels[w.X, w.Y].CellColor == targetColor))
  90. {
  91. pixels[w.X, w.Y].CellColor = replaceColor;
  92. w = new Point(w.X - 1, w.Y);
  93. }
  94.  
  95. while ((e.X != CANVAS_SIZE - 1) && (pixels[e.X, e.Y].CellColor == targetColor))
  96. {
  97. pixels[e.X, e.Y].CellColor = replaceColor;
  98. e = new Point(e.X + 1, e.Y);
  99. }
  100.  
  101. for (int i = w.X; i <= e.X; i++)
  102. {
  103. Point x = new Point(i, e.Y);
  104. if (e.Y + 1 != CANVAS_SIZE - 1)
  105. {
  106. if (pixels[x.X, x.Y + 1].CellColor == targetColor)
  107. q.Enqueue(new Point(x.X, x.Y + 1));
  108. }
  109. if (e.Y - 1 != -1)
  110. {
  111. if (pixels[x.X, x.Y - 1].CellColor == targetColor)
  112. q.Enqueue(new Point(x.X, x.Y - 1));
  113. }
  114. }
  115. }
  116. }
  117. }
  118.  
  119. private void RevisedQueueFloodFill(Point node, Color targetColor, Color replaceColor)
  120. {
  121. Queue<Point> q = new Queue<Point>();
  122.  
  123. if (pixels[node.X, node.Y].CellColor != targetColor)
  124. return;
  125.  
  126. q.Enqueue(node);
  127.  
  128. Point n, e, w, x;
  129. while (q.Count > 0)
  130. {
  131. n = q.Dequeue();
  132. if (pixels[n.X, n.Y].CellColor == targetColor)
  133. {
  134. e = n;
  135. w = n;
  136. while ((w.X != 0) && (pixels[w.X, w.Y].CellColor == targetColor))
  137. {
  138. pixels[w.X, w.Y].CellColor = replaceColor;
  139. w = new Point(w.X - 1, w.Y);
  140. }
  141.  
  142. while ((e.X != CANVAS_SIZE - 1) && (pixels[e.X, e.Y].CellColor == targetColor))
  143. {
  144. pixels[e.X, e.Y].CellColor = replaceColor;
  145. e = new Point(e.X + 1, e.Y);
  146. }
  147.  
  148. for (int i = w.X; i <= e.X; i++)
  149. {
  150. x = new Point(i, e.Y);
  151. if (e.Y + 1 != CANVAS_SIZE - 1)
  152. {
  153. if (pixels[x.X, x.Y + 1].CellColor == targetColor)
  154. q.Enqueue(new Point(x.X, x.Y + 1));
  155. }
  156. if (e.Y - 1 != -1)
  157. {
  158. if (pixels[x.X, x.Y - 1].CellColor == targetColor)
  159. q.Enqueue(new Point(x.X, x.Y - 1));
  160. }
  161. }
  162. }
  163. }
  164. }
  165.  
  166. for (int i = w.X; i <= e.X; i++)
  167. {
  168. Point x = new Point(i, e.Y);
  169. }
  170.  
  171. Point x;
  172.  
  173. for(int i = w.X; i<= e.X; i++)
  174. {
  175. x = new Point(i, e.Y);
  176. }
  177.  
  178. private void RevisedQueueFloodFill(Point node, Color targetColor, Color replaceColor)
  179. {
  180. if (pixels[node.X, node.Y].CellColor != targetColor) return;
  181.  
  182. Queue<Point> Q = new Queue<Point>();
  183. Q.Enqueue(node);
  184.  
  185. while (Q.Count != 0)
  186. {
  187. Point n = Q.Dequeue();
  188. if (pixels[n.X, n.Y].CellColor == targetColor)
  189. {
  190. int y = n.Y;
  191. int w = n.X;
  192. int e = n.X;
  193. while (w > 0 && pixels[w - 1, y].CellColor == targetColor) w--;
  194. while (e < CANVAS_SIZE - 1 && pixels[e + 1, y].CellColor == targetColor) e++;
  195.  
  196. for (int x = w; x <= e; x++)
  197. {
  198. pixels[x, y].CellColor = replaceColor;
  199. if (y > 0 && pixels[x, y - 1].CellColor == targetColor)
  200. {
  201. Q.Enqueue(new Point(x, y - 1));
  202. }
  203. if (y < CANVAS_SIZE - 1 && pixels[x, y + 1].CellColor == targetColor)
  204. {
  205. Q.Enqueue(new Point(x, y + 1));
  206. }
  207. }
  208. }
  209. }
  210. }
  211.  
  212. private void RevisedQueueFloodFill(Point node, Color replaceColor)
  213. {
  214. Color targetColor = pixels[node.X, node.Y].CellColor;
  215. if (targetColor == replaceColor) return;
  216.  
  217. Queue<Point> q = new Queue<Point>();
  218. q.Enqueue(node);
  219.  
  220. Point n, t, u;
  221.  
  222. while (q.Count > 0)
  223. {
  224. n = q.Dequeue();
  225. if (pixels[n.X, n.Y].CellColor == targetColor)
  226. {
  227.  
  228. t = n;
  229. while ((t.X > 0) && (pixels[t.X, t.Y].CellColor == targetColor))
  230. {
  231. pixels[t.X, t.Y].CellColor = replaceColor;
  232. t.X--;
  233. }
  234. int XMin = t.X + 1;
  235.  
  236.  
  237. t = n;
  238. t.X++;
  239. while ((t.X < CANVAS_SIZE - 1) &&
  240. (pixels[t.X, t.Y].CellColor == targetColor))
  241. {
  242. pixels[t.X, t.Y].CellColor = replaceColor;
  243. t.X++;
  244. }
  245. int XMax = t.X - 1;
  246.  
  247. t = n;
  248. t.Y++;
  249.  
  250. u = n;
  251. u.Y--;
  252.  
  253. for (int i = XMin; i <= XMax; i++)
  254. {
  255. t.X = i;
  256. u.X = i;
  257.  
  258. if ((t.Y < CANVAS_SIZE - 1) &&
  259. (pixels[t.X, t.Y].CellColor == targetColor)) q.Enqueue(t);
  260.  
  261. if ((u.Y >= 0) &&
  262. (pixels[u.X, u.Y].CellColor == targetColor)) q.Enqueue(u);
  263. }
  264. }
  265. }
  266. }
Add Comment
Please, Sign In to add comment