-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGrid.cs
228 lines (195 loc) · 5.52 KB
/
Grid.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace grain_growth
{
public class Grid
{
protected Cell[,] cells;
public int Width { set; get; }
public int Height { set; get; }
public bool periodic;
public int curPosX;
public int curPosY;
public Grid(int width, int height, bool periodic)
{
this.Width = width;
this.Height = height;
this.periodic = periodic;
// Initialize cells
this.cells = new Cell[height, width];
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
this.cells[i, j] = new Cell();
}
}
// Setup neighbors
this.ResetCurrentCellPosition();
do
{
this.CurrentCell.Neighbors = this.NeighborhoodOfCurrentCell;
} while (this.Next());
}
public Cell CurrentCell
{
get
{
return this.GetCell(this.curPosX, this.curPosY);
}
}
/// <summary>
/// Return cell using boundary condition
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public Cell GetCell(int x, int y)
{
if (x < 0 || x >= this.Width || y < 0 || y >= this.Height)
{
// Periodic boundary condition
if ( this.periodic )
{
return this.cells[MathFunctions.MathMod(y, this.Height), MathFunctions.MathMod(x, this.Width)];
}
// Absorbent boundary condition
else
{
return new Cell();
}
}
else
{
return this.cells[y,x];
}
}
public void ResetCurrentCellPosition()
{
this.curPosX = 0;
this.curPosY = 0;
}
public void SetCurrentCellPosition(int x, int y)
{
this.curPosX = x;
this.curPosY = y;
}
/// <summary>
/// Move CurrentCell pointer to next cell (line by line)
/// </summary>
/// <returns>false when CurrentCell is the last cell otherwise true</returns>
public bool Next()
{
// Last cell
if (this.curPosX == this.Width - 1 && this.curPosY == this.Height - 1)
{
return false;
}
// Move pointer to next cell
if (this.curPosX == this.Width - 1)
{
this.curPosX = 0;
this.curPosY += 1;
}
else
{
this.curPosX += 1;
}
return true;
}
/// <summary>
/// Move NewID value to ID (using after one step of growth)
/// </summary>
public void CopyNewIDtoID()
{
for (int i = 0; i < this.Height; ++i)
{
for (int j = 0; j < this.Width; ++j)
{
this.cells[i, j].ID = this.cells[i, j].NewID;
}
}
}
#region Neighbors
// Neighbors of current cell by directions of the world
public Cell NeighborN
{
get
{
return this.GetNeighbor(0, 1);
}
}
public Cell NeighborNW
{
get
{
return this.GetNeighbor(1, 1);
}
}
public Cell NeighborW
{
get
{
return this.GetNeighbor(1, 0);
}
}
public Cell NeighborSW
{
get
{
return this.GetNeighbor(1, -1);
}
}
public Cell NeighborS
{
get
{
return this.GetNeighbor(0, -1);
}
}
public Cell NeighborSE
{
get
{
return this.GetNeighbor(-1, -1);
}
}
public Cell NeighborE
{
get
{
return this.GetNeighbor(-1, 0);
}
}
public Cell NeighborNE
{
get
{
return this.GetNeighbor(-1, 1);
}
}
protected Cell GetNeighbor(int diffX, int diffY)
{
return this.GetCell(this.curPosX + diffX, this.curPosY + diffY);
}
public Cell[] NeighborhoodOfCurrentCell
{
get
{
List<Cell> cells = new List<Cell>();
cells.Add(this.NeighborN);
cells.Add(this.NeighborNE);
cells.Add(this.NeighborE);
cells.Add(this.NeighborSE);
cells.Add(this.NeighborS);
cells.Add(this.NeighborSW);
cells.Add(this.NeighborW);
cells.Add(this.NeighborNW);
return cells.ToArray();
}
}
#endregion
}
}