/// <summary> /// Creates a new grid from the 2d array of cells. /// </summary> /// <param name="cells">A uniform, two dimensional array of cells</param> /// <exception cref="FormatException">Thrown when the given 2d array does not have uniform dimensions</exception> public Grid(Cell[,] cells) { if (cells.GetLength(0) != cells.GetLength(1)) throw new FormatException("Two dimensional cells array must have uniform dimensions"); Cells = cells; }
/// <summary> /// Creates a uniformly sized grid of cells with no chemicals or their associated concentrations set /// </summary> /// <param name="size">The number of cells wide and high this grid is</param> public Grid(int size) { // Initialize cells with equal dimensions Cells = new Cell[size, size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { // Each cell in grid is empty of chemicals and concentrations Cells[i, j] = new Cell(); } } }
public void CellConcentrationClampingTest() { Cell cell = new Cell(); // Assert concentration of A is clamped to maximum and minimum values of 1 and 0 cell["A"] = 1.5; Assert.AreEqual(cell["A"], 1); cell["A"] = -3; Assert.AreEqual(cell["A"], 0); // Assert concentration of B is clamped to maximum and minimum values of 1 and 0 cell["B"] = 1.1; Assert.AreEqual(cell["B"], 1); cell["B"] = -0.00001; Assert.AreEqual(cell["B"], 0); }
/// <summary> /// Creates a uniformly sized grid of cells with the same chemicals and concentrations as in the given cell /// </summary> /// <param name="size">The number of cells wide and high this grid is</param> /// <param name="cell">The cell prefab whose chemicals and concentrations will be copied to each cell in this grid</param> public Grid(int size, Cell prefab) { // Create a uniformly sized two dimensional array of cells Cells = new Cell[size, size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { // Cell concentrations are set to constructor arguements Cells[i, j] = new Cell(); foreach (KeyValuePair<string, double> chemical in prefab) { // Set chemicals and concentrations for cell to same as passed cell Cells[i, j].Add(chemical.Key, chemical.Value); } } } }
public void CellConcentrationTest() { Cell cell = new Cell { { "A", 0 }, { "B", 0 } }; // Assert cell concentrations default to 0 Assert.AreEqual(cell["A"], 0); Assert.AreEqual(cell["B"], 0); // Assert cell concentrations retain passed amounts cell.Add("C", 0.25); cell.Add("D", 0.99999); Assert.AreEqual(cell["C"], 0.25); Assert.AreEqual(cell["D"], 0.99999); }
public Form1() { InitializeComponent(); Cell prefabCell = new Cell { { GrayScottDiffusionReaction.CHEMICAL_A, 0 }, { GrayScottDiffusionReaction.CHEMICAL_B, 0 } }; Grid grid = new Grid(GRID_SIZE, prefabCell); grid.Seed(grid.Size / 2, grid.Size / 2, 2, 2, GrayScottDiffusionReaction.CHEMICAL_B, 1); // Seed central area with B = 1 reaction = new GrayScottDiffusionReaction(grid, FEED_A, KILL_B, new PerpendicularNeighboursLaplacianFunction()); renderer = new NETGridRenderer(grid, new GreyscaleShader()) { CellWidth = 4, CellHeight = 4 }; }
public void Update() { // Create new array of cells so as to not overwrite old values while still performing calculations Grid grid = new Grid(Grid.Size); // Iterate through entire grid of cells for (int column = 0; column < Grid.Size; column++) { for (int row = 0; row < Grid.Size; row++) { // Calculate next iteration of A and B double A = NextA(column, row); double B = NextB(column, row); // Create new cell containing new A and B concentrations grid[column, row] = new Cell { { CHEMICAL_A, A }, { CHEMICAL_B, B } }; } } // Assign new array of cells to grid object, overwritting old array Grid.Overwrite(grid); // Keep track of the number of iterations this reaction has been running for iterations++; }