-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLA.cs
111 lines (89 loc) · 2.68 KB
/
DLA.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
using System;
namespace DLA
{
/// <summary>
/// Difusion Limited Aggregation
/// </summary>
class DLA
{
public byte[,] site;
//
public bool canPlot; // use this var for set bitamp
public int iCurrnet, jCurrent;
public int i0; // the index of array that we seed
public int nWalkers;
double R0 = 3;
int l;
bool isOnPerimeter;
double r;
Random rand = new Random();
public DLA(int l, double R0)
{
this.l = l;
this.R0 = R0;
Init();
}
public DLA(int l)
{
this.l = l;
Init();
}
void Init()
{
canPlot = true;
site = new byte[l, l];
i0 = l / 2;
site[i0, i0] = 1; // occuiped seed site
nWalkers = 1;
iCurrnet = jCurrent = i0;
}
public void MoveNextWalker()
{
double theta = 2 * Math.PI * rand.NextDouble();
int x = i0 + (int)(R0 * Math.Cos(theta));
int y = i0 + (int)(R0 * Math.Sin(theta));
Walk(x, y);
nWalkers++;
}
void Walk(int x, int y)
{
double temp;
int step, dx, dy;
isOnPerimeter = false;
do
{
dx = x - i0;
dy = y - i0;
r = Math.Sqrt(dx * dx + dy * dy);
if (r <= R0 )
Test(x, y); // test is walker on peimeter
if (!isOnPerimeter)
{
step = (int)(r - R0) - 1; // big step
if (step < 1) step = 1;
temp = rand.NextDouble();
if (temp < 0.25) x += step;
else if (temp < 0.5) x -= step;
else if (temp < 0.75) y += step;
else y -= step;
}
} while (!isOnPerimeter && r < 2 * R0);
//
canPlot = true; // just for plot in bitmap
if (r >= 2 * R0)
canPlot = false;
iCurrnet = x;
jCurrent = y;
//
}
void Test(int x, int y)
{
if (site[x + 1, y] + site[x - 1, y] + site[x, y + 1] + site[x, y - 1] > 0)
{
site[x, y] = 1;
isOnPerimeter = true;
if (r >= R0 - 1) R0 = (int)(r + 2);
}
}
}
}