-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormSand.cs
197 lines (156 loc) · 5.51 KB
/
FormSand.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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SandPile
{
public partial class FormSand : Form
{
#region Fields
Sand sand;
Bitmap mapSandPile;
int sandSize;
Color[] colorArray = { Color.Yellow, Color.Orange, Color.Chocolate, Color.Brown };
int snapshots = 0;
#endregion
#region Methods
Color FindColor(int value, int scale)
{
if (value > scale)
return Color.Black;
else if (value == 0)
return Color.White;
else if (value < 0)
return Color.Blue;
return colorArray[(value-1) * colorArray.Length / scale];
}
void SetBitMap()
{
if (sandSize == 1)
{
for (int i = 0; i < sand.Lx; i++)
for (int j = 0; j < sand.Ly; j++)
mapSandPile.SetPixel(i, j, FindColor(sand.h[i, j], sand.scale));
}
else
{
Graphics g = Graphics.FromImage(mapSandPile);
for (int i = 0; i < sand.Lx; i++)
for (int j = 0; j < sand.Ly; j++)
g.FillRectangle(new SolidBrush(FindColor(sand.h[i, j], sand.scale)), i * sandSize, j * sandSize, sandSize, sandSize);
g.Dispose();
}
}
void Reset()
{
if (timer1 != null && timer1.Enabled) timer1.Stop();
buttonAvalanche.Text = "Avalanche";
int Lx, Ly, e, m;
try
{
Lx = int.Parse(textBox_Lx.Text);
Ly = int.Parse(textBox_Ly.Text);
e = int.Parse(textBox_e.Text);
m = int.Parse(textBox_m.Text);
}
catch (FormatException fe)
{
return;
}
if (Lx <= 64) sandSize = 6;
else if (Lx <= 100) sandSize = 4;
else if (Lx <= 200) sandSize = 2;
else if (Lx <= 300) sandSize = 1;
else sandSize = 1;
EpsionDis epsilonDis;
if (radioButtonDirectal.Checked) epsilonDis = EpsionDis.Directed;
else if (radioButtonEliptic.Checked) epsilonDis = EpsionDis.Elliptical;
else epsilonDis = EpsionDis.Random;
mapSandPile = new Bitmap(Lx * sandSize, Ly * sandSize);
pictureBox1.Size = mapSandPile.Size;
groupBoxStructure.Left = pictureBox1.Right + 10;
sand = new Sand(Lx, Ly, e, m, epsilonDis);
SetBitMap();
labelEpsilonMethod.Text = sand.epsilonDis.ToString();
ShowLables();
pictureBox1.Invalidate();
}
int CalculateTimerInterval()
{
int maxInterval = 1000, minInterval = 10,
min = trackBar_Latency.Minimum, max = trackBar_Latency.Maximum, value = trackBar_Latency.Value;
return ((minInterval - maxInterval) * (value - min)) / (max - min) + maxInterval;
}
void ShowLables()
{
labelNumTopple.Text = sand.numFalledGrains.ToString();
}
void Init()
{
textBox_Lx.Text = "64";
textBox_Ly.Text = "64";
textBox_m.Text = "0";
textBox_e.Text = "0";
}
#endregion
public FormSand()
{
InitializeComponent();
Init();
Reset();
}
private void timer1_Tick(object sender, EventArgs e)
{
int step = (int)numericUpDownIt.Value;
for (int n = 0; n < step; n++ )
if (sand.isCompleteAvalanche)
sand.AddOneGrain();
else
sand.PropagateOneWave();
SetBitMap();
ShowLables();
pictureBox1.Invalidate();
}
private void buttonReset_Click(object sender, EventArgs e)
{
Reset();
}
private void trackBar_Latency_Scroll(object sender, EventArgs e)
{
timer1.Interval = CalculateTimerInterval();
}
private void FormSand_FormClosing(object sender, FormClosingEventArgs e)
{
if (timer1 != null && timer1.Enabled)
timer1.Stop();
}
private void buttonAvalanche_Click(object sender, EventArgs e)
{
if (buttonAvalanche.Text == "Avalanche")
{
timer1.Start();
buttonAvalanche.Text = "Pause";
}
else
{
timer1.Stop();
buttonAvalanche.Text = "Avalanche";
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(mapSandPile, 0, 0);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
string text = "Designed by Hor Dashti ([email protected])";
MessageBox.Show(text);
}
private void buttonSnapshot_Click(object sender, EventArgs e)
{
string namefile = snapshots.ToString() + ".jpeg";
mapSandPile.Save(namefile);
snapshots++;
}
}
}