Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Ziarno
- {
- class Ziarno
- {
- private PictureBox pb;
- private int seeds;
- private List<Color> colors;
- private Bitmap bmap;
- private int currentX;
- private int currentY;
- private Color currentColor;
- private List<Color> currentNeighbours;
- Random rand;
- private int width = 200;
- private int height = 200;
- private string neighType;
- private string randomType;
- private bool periodicConds;
- private bool isRunning = false;
- private System.Threading.Thread th;
- private Color[,] nextCells;
- BackgroundWorker bg = new BackgroundWorker();
- public Ziarno(int seeds, PictureBox pb, int radius, string neighType, string randomType, bool periodicConds)
- {
- this.seeds = seeds;
- this.pb = pb;
- this.rand = new Random(DateTime.Now.Millisecond);
- this.neighType = neighType;
- this.randomType = randomType;
- this.periodicConds = periodicConds;
- this.nextCells = new Color[width, height];
- this.currentNeighbours = new List<Color>();
- }
- public void stop()
- {
- this.isRunning = false;
- //this.th.Abort();
- }
- public void start()
- {
- if (this.seeds == 0)
- {
- return;
- }
- this.bmap = new Bitmap(this.width, this.height);
- for (int i = 0; i < this.width; i++)
- {
- for (int j = 0; j < this.height; j++)
- {
- this.bmap.SetPixel(i, j, Color.Black);
- this.nextCells[i, j] = Color.Black;
- }
- }
- colors = new List<Color>(this.seeds);
- for (int i = 0; i < this.seeds; i++)
- {
- colors.Add(Color.FromArgb(this.rand.Next(1, 256), this.rand.Next(1, 256), this.rand.Next(1, 256)));
- }
- addSeedsToBmp();
- this.pb.Invoke(new Action(() =>
- {
- this.pb.Image = this.bmap;
- }));
- this.bmap.SetResolution(this.pb.Width, this.pb.Height);
- //th = new System.Threading.Thread(this.loop);
- this.bg.DoWork += loop;
- this.bg.RunWorkerAsync();
- this.isRunning = true;
- //th.Start();
- //this.loop();
- }
- private void loop(object sender, DoWorkEventArgs e)
- {
- while (this.isRunning)
- {
- try
- {
- for (int x = 0; x < this.width - 1; x++)
- {
- for (int y = 0; y < this.height - 1; y++)
- {
- this.currentX = x;
- this.currentY = y;
- this.currentColor = this.nextCells[this.currentX, this.currentY];
- this.doShit();
- }
- }
- this.refresh();
- }catch(InvalidOperationException ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- // this.th.Abort();
- }
- private void doShit()
- {
- if(this.neighType == "Moore")
- {
- getMooreNeigh(this.currentX, this.currentY);
- changeState(this.currentX, this.currentY);
- }
- }
- private void refresh()
- {
- try
- {
- for (int x = 0; x < this.width - 1; x++)
- {
- for (int y = 0; y < this.height - 1; y++)
- {
- this.bmap.SetPixel(x, y, nextCells[x, y]);
- }
- }
- this.pb.Invoke(new Action(() =>
- {
- this.pb.Image = this.bmap;
- }));
- }catch(InvalidOperationException ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void addSeedsToBmp()
- {
- if(this.randomType == "Random")
- {
- List<Point> tmpPoints = new List<Point>();
- Point tmpPoint = new Point(this.rand.Next(0, this.width), this.rand.Next(0, this.height));
- for (int i = 0; i < this.seeds; i++)
- {
- while (tmpPoints.Count((point) => point.X == tmpPoint.X && point.Y == tmpPoint.Y) != 0)
- {
- tmpPoint = new Point(this.rand.Next(0, this.width), this.rand.Next(0, this.height));
- }
- tmpPoints.Add(tmpPoint);
- this.nextCells[tmpPoint.X, tmpPoint.Y] = this.colors[i];
- this.bmap.SetPixel(tmpPoint.X, tmpPoint.Y, this.colors[i]);
- }
- }
- }
- private void changeState(int x, int y)
- {
- int colorCount = 0;
- int count = 0;
- Color tmpColor = this.currentColor;
- if (tmpColor.ToArgb() == Color.Black.ToArgb())
- {
- for (int k = 0; k < currentNeighbours.Count; k++)
- {
- if (count < this.currentNeighbours.Count((c) => c.ToArgb() == this.currentNeighbours[k].ToArgb()))
- {
- this.nextCells[x, y] = currentNeighbours[k];
- }
- }
- /*foreach (Color c in this.currentNeighbours)
- {
- if ((tmpCount = this.currentNeighbours.Count((color) => c.ToArgb() == color.ToArgb() )) > colorCount)
- {
- colorCount = tmpCount;
- tmpColor = c;
- this.nextCells[x, y] = tmpColor;
- }
- }*/
- }
- }
- private void getMooreNeigh(int x, int y)
- {
- try
- {
- this.currentNeighbours = new List<Color>();
- if (x == 0 && y != 0 && y != this.height - 1)
- {
- for (int k = y - 1; k <= y + 1; k++)
- this.currentNeighbours.Add(this.nextCells[x + 1, k]);
- this.currentNeighbours.Add(this.nextCells[x, y - 1]);
- this.currentNeighbours.Add(this.nextCells[x, y + 1]);
- }
- else if (x == this.width - 1 && y != 0 && y != this.height - 1)
- {
- for (int k = y - 1; k <= y + 1; k++)
- this.currentNeighbours.Add(this.nextCells[this.width - 1, k]);
- this.currentNeighbours.Add(this.nextCells[x, y - 1]);
- this.currentNeighbours.Add(this.nextCells[x, y + 1]);
- }
- else if (y == 0 && x != 0 && x != this.width - 1)
- {
- for (int k = x - 1; k <= x + 1; k++)
- this.currentNeighbours.Add(this.nextCells[k, y + 1]);
- this.currentNeighbours.Add(this.nextCells[x - 1, y]);
- this.currentNeighbours.Add(this.nextCells[x + 1, y]);
- }
- else if (y == this.height - 1 && x != 0 && x != this.width - 1)
- {
- for (int k = x - 1; k <= x + 1; k++)
- this.currentNeighbours.Add(this.nextCells[k, y - 1]);
- this.currentNeighbours.Add(this.nextCells[x - 1, y]);
- this.currentNeighbours.Add(this.nextCells[x + 1, y]);
- }
- else if (x == 0 && y == 0)
- {
- this.currentNeighbours.Add(this.nextCells[x, y + 1]);
- this.currentNeighbours.Add(this.nextCells[x + 1, y + 1]);
- this.currentNeighbours.Add(this.nextCells[x + 1, y]);
- }
- else if (x == this.width - 1 && y == 0)
- {
- this.currentNeighbours.Add(this.nextCells[x - 1, y + 1]);
- this.currentNeighbours.Add(this.nextCells[x - 1, y]);
- this.currentNeighbours.Add(this.nextCells[x, y + 1]);
- }
- else if (x == this.width - 1 && y == this.height - 1)
- {
- this.currentNeighbours.Add(this.nextCells[x, y - 1]);
- this.currentNeighbours.Add(this.nextCells[x - 1, y - 1]);
- this.currentNeighbours.Add(this.nextCells[x - 1, y]);
- }
- else if (x == 0 && y == this.height-1)
- {
- this.currentNeighbours.Add(this.nextCells[x, y - 1]);
- this.currentNeighbours.Add(this.nextCells[x + 1, y - 1]);
- this.currentNeighbours.Add(this.nextCells[x + 1, y]);
- }
- else
- {
- this.currentNeighbours.Add(this.nextCells[x - 1, y - 1]);
- this.currentNeighbours.Add(this.nextCells[x - 1, y]);
- this.currentNeighbours.Add(this.nextCells[x - 1, y + 1]);
- this.currentNeighbours.Add(this.nextCells[x, y - 1]);
- this.currentNeighbours.Add(this.nextCells[x, y + 1]);
- this.currentNeighbours.Add(this.nextCells[x + 1, y - 1]);
- this.currentNeighbours.Add(this.nextCells[x + 1, y]);
- this.currentNeighbours.Add(this.nextCells[x + 1, y + 1]);
- }
- }
- catch (InvalidOperationException ex)
- {
- //th.Abort();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement