Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9.  
  10. namespace Ziarno
  11. {
  12. class Ziarno
  13. {
  14. private PictureBox pb;
  15. private int seeds;
  16. private List<Color> colors;
  17. private Bitmap bmap;
  18. private int currentX;
  19. private int currentY;
  20. private Color currentColor;
  21. private List<Color> currentNeighbours;
  22. Random rand;
  23. private int width = 200;
  24. private int height = 200;
  25. private string neighType;
  26. private string randomType;
  27. private bool periodicConds;
  28. private bool isRunning = false;
  29. private System.Threading.Thread th;
  30. private Color[,] nextCells;
  31. BackgroundWorker bg = new BackgroundWorker();
  32. public Ziarno(int seeds, PictureBox pb, int radius, string neighType, string randomType, bool periodicConds)
  33. {
  34. this.seeds = seeds;
  35. this.pb = pb;
  36. this.rand = new Random(DateTime.Now.Millisecond);
  37. this.neighType = neighType;
  38. this.randomType = randomType;
  39. this.periodicConds = periodicConds;
  40. this.nextCells = new Color[width, height];
  41. this.currentNeighbours = new List<Color>();
  42. }
  43.  
  44. public void stop()
  45. {
  46. this.isRunning = false;
  47. //this.th.Abort();
  48. }
  49.  
  50. public void start()
  51. {
  52. if (this.seeds == 0)
  53. {
  54. return;
  55. }
  56.  
  57. this.bmap = new Bitmap(this.width, this.height);
  58. for (int i = 0; i < this.width; i++)
  59. {
  60. for (int j = 0; j < this.height; j++)
  61. {
  62. this.bmap.SetPixel(i, j, Color.Black);
  63. this.nextCells[i, j] = Color.Black;
  64. }
  65. }
  66.  
  67. colors = new List<Color>(this.seeds);
  68. for (int i = 0; i < this.seeds; i++)
  69. {
  70. colors.Add(Color.FromArgb(this.rand.Next(1, 256), this.rand.Next(1, 256), this.rand.Next(1, 256)));
  71. }
  72.  
  73.  
  74. addSeedsToBmp();
  75.  
  76. this.pb.Invoke(new Action(() =>
  77. {
  78. this.pb.Image = this.bmap;
  79. }));
  80. this.bmap.SetResolution(this.pb.Width, this.pb.Height);
  81. //th = new System.Threading.Thread(this.loop);
  82. this.bg.DoWork += loop;
  83. this.bg.RunWorkerAsync();
  84. this.isRunning = true;
  85. //th.Start();
  86. //this.loop();
  87. }
  88.  
  89. private void loop(object sender, DoWorkEventArgs e)
  90. {
  91. while (this.isRunning)
  92. {
  93. try
  94. {
  95. for (int x = 0; x < this.width - 1; x++)
  96. {
  97. for (int y = 0; y < this.height - 1; y++)
  98. {
  99. this.currentX = x;
  100. this.currentY = y;
  101. this.currentColor = this.nextCells[this.currentX, this.currentY];
  102. this.doShit();
  103.  
  104. }
  105. }
  106. this.refresh();
  107. }catch(InvalidOperationException ex)
  108. {
  109. MessageBox.Show(ex.Message);
  110. }
  111. }
  112. // this.th.Abort();
  113. }
  114.  
  115. private void doShit()
  116. {
  117. if(this.neighType == "Moore")
  118. {
  119. getMooreNeigh(this.currentX, this.currentY);
  120. changeState(this.currentX, this.currentY);
  121. }
  122. }
  123.  
  124. private void refresh()
  125. {
  126. try
  127. {
  128. for (int x = 0; x < this.width - 1; x++)
  129. {
  130. for (int y = 0; y < this.height - 1; y++)
  131. {
  132.  
  133. this.bmap.SetPixel(x, y, nextCells[x, y]);
  134. }
  135. }
  136. this.pb.Invoke(new Action(() =>
  137. {
  138. this.pb.Image = this.bmap;
  139. }));
  140. }catch(InvalidOperationException ex)
  141. {
  142. MessageBox.Show(ex.Message);
  143. }
  144. }
  145.  
  146. private void addSeedsToBmp()
  147. {
  148. if(this.randomType == "Random")
  149. {
  150. List<Point> tmpPoints = new List<Point>();
  151. Point tmpPoint = new Point(this.rand.Next(0, this.width), this.rand.Next(0, this.height));
  152. for (int i = 0; i < this.seeds; i++)
  153. {
  154. while (tmpPoints.Count((point) => point.X == tmpPoint.X && point.Y == tmpPoint.Y) != 0)
  155. {
  156. tmpPoint = new Point(this.rand.Next(0, this.width), this.rand.Next(0, this.height));
  157. }
  158. tmpPoints.Add(tmpPoint);
  159. this.nextCells[tmpPoint.X, tmpPoint.Y] = this.colors[i];
  160. this.bmap.SetPixel(tmpPoint.X, tmpPoint.Y, this.colors[i]);
  161. }
  162. }
  163. }
  164.  
  165. private void changeState(int x, int y)
  166. {
  167. int colorCount = 0;
  168. int count = 0;
  169. Color tmpColor = this.currentColor;
  170. if (tmpColor.ToArgb() == Color.Black.ToArgb())
  171. {
  172. for (int k = 0; k < currentNeighbours.Count; k++)
  173. {
  174. if (count < this.currentNeighbours.Count((c) => c.ToArgb() == this.currentNeighbours[k].ToArgb()))
  175. {
  176. this.nextCells[x, y] = currentNeighbours[k];
  177. }
  178. }
  179. /*foreach (Color c in this.currentNeighbours)
  180. {
  181. if ((tmpCount = this.currentNeighbours.Count((color) => c.ToArgb() == color.ToArgb() )) > colorCount)
  182. {
  183. colorCount = tmpCount;
  184. tmpColor = c;
  185. this.nextCells[x, y] = tmpColor;
  186. }
  187. }*/
  188. }
  189. }
  190.  
  191. private void getMooreNeigh(int x, int y)
  192. {
  193. try
  194. {
  195.  
  196. this.currentNeighbours = new List<Color>();
  197. if (x == 0 && y != 0 && y != this.height - 1)
  198. {
  199. for (int k = y - 1; k <= y + 1; k++)
  200. this.currentNeighbours.Add(this.nextCells[x + 1, k]);
  201. this.currentNeighbours.Add(this.nextCells[x, y - 1]);
  202. this.currentNeighbours.Add(this.nextCells[x, y + 1]);
  203. }
  204. else if (x == this.width - 1 && y != 0 && y != this.height - 1)
  205. {
  206. for (int k = y - 1; k <= y + 1; k++)
  207. this.currentNeighbours.Add(this.nextCells[this.width - 1, k]);
  208. this.currentNeighbours.Add(this.nextCells[x, y - 1]);
  209. this.currentNeighbours.Add(this.nextCells[x, y + 1]);
  210. }
  211. else if (y == 0 && x != 0 && x != this.width - 1)
  212. {
  213. for (int k = x - 1; k <= x + 1; k++)
  214. this.currentNeighbours.Add(this.nextCells[k, y + 1]);
  215. this.currentNeighbours.Add(this.nextCells[x - 1, y]);
  216. this.currentNeighbours.Add(this.nextCells[x + 1, y]);
  217. }
  218. else if (y == this.height - 1 && x != 0 && x != this.width - 1)
  219. {
  220. for (int k = x - 1; k <= x + 1; k++)
  221. this.currentNeighbours.Add(this.nextCells[k, y - 1]);
  222. this.currentNeighbours.Add(this.nextCells[x - 1, y]);
  223. this.currentNeighbours.Add(this.nextCells[x + 1, y]);
  224. }
  225. else if (x == 0 && y == 0)
  226. {
  227. this.currentNeighbours.Add(this.nextCells[x, y + 1]);
  228. this.currentNeighbours.Add(this.nextCells[x + 1, y + 1]);
  229. this.currentNeighbours.Add(this.nextCells[x + 1, y]);
  230. }
  231. else if (x == this.width - 1 && y == 0)
  232. {
  233. this.currentNeighbours.Add(this.nextCells[x - 1, y + 1]);
  234. this.currentNeighbours.Add(this.nextCells[x - 1, y]);
  235. this.currentNeighbours.Add(this.nextCells[x, y + 1]);
  236. }
  237. else if (x == this.width - 1 && y == this.height - 1)
  238. {
  239. this.currentNeighbours.Add(this.nextCells[x, y - 1]);
  240. this.currentNeighbours.Add(this.nextCells[x - 1, y - 1]);
  241. this.currentNeighbours.Add(this.nextCells[x - 1, y]);
  242. }
  243. else if (x == 0 && y == this.height-1)
  244. {
  245. this.currentNeighbours.Add(this.nextCells[x, y - 1]);
  246. this.currentNeighbours.Add(this.nextCells[x + 1, y - 1]);
  247. this.currentNeighbours.Add(this.nextCells[x + 1, y]);
  248. }
  249. else
  250. {
  251. this.currentNeighbours.Add(this.nextCells[x - 1, y - 1]);
  252. this.currentNeighbours.Add(this.nextCells[x - 1, y]);
  253. this.currentNeighbours.Add(this.nextCells[x - 1, y + 1]);
  254. this.currentNeighbours.Add(this.nextCells[x, y - 1]);
  255. this.currentNeighbours.Add(this.nextCells[x, y + 1]);
  256. this.currentNeighbours.Add(this.nextCells[x + 1, y - 1]);
  257. this.currentNeighbours.Add(this.nextCells[x + 1, y]);
  258. this.currentNeighbours.Add(this.nextCells[x + 1, y + 1]);
  259. }
  260. }
  261. catch (InvalidOperationException ex)
  262. {
  263. //th.Abort();
  264. }
  265. }
  266.  
  267. }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement