-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormDLA.cs
149 lines (125 loc) · 4.25 KB
/
FormDLA.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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DLA
{
public partial class FormDLA : Form
{
//
#region Fields
//int sizeBitMap;
Bitmap bitMap;
int xC_Bitmap, yC_BitMap;
int sizeWalker = 1;
DLA dla;
int snapshots = 0;
Brush brushWalker = Brushes.Red;
Color[] colorArr = { Color.SkyBlue, Color.Violet, Color.Brown, Color.Red, Color.Orange,
Color.Green, Color.Blue, Color.DarkBlue, Color.DarkKhaki, Color.DarkMagenta, Color.DarkOrange, Color.DarkRed,Color.Tomato, Color.Black};
#endregion
//
//
#region Private Methods
void Reset()
{
int sizeBitMapMax = Math.Max(pictureBox1.Width, pictureBox1.Height);
bitMap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
xC_Bitmap = bitMap.Width/2;
yC_BitMap = bitMap.Height/2;
int l = sizeBitMapMax / sizeWalker;
dla = new DLA(4*l);
label_nWalkers.Text = dla.nWalkers.ToString();
BitMapSetPixel();
pictureBox1.Invalidate();
GC.Collect();
}
void DynamicSimulation()
{
int iteration = (int)numericUpDown1.Value;
for (int i = 0; i < iteration; i++)
{
dla.MoveNextWalker();
BitMapSetPixel();
}
label_nWalkers.Text = dla.nWalkers.ToString();
pictureBox1.Invalidate();
}
void BitMapSetPixel()
{
if (!dla.canPlot)
return;
int di = dla.iCurrnet - dla.i0;
int dj = dla.jCurrent - dla.i0;
int x0 = xC_Bitmap + di * sizeWalker - sizeWalker / 2;
int y0 = yC_BitMap + dj * sizeWalker - sizeWalker / 2;
if (x0 >= bitMap.Width || x0 < 0 || y0 >= bitMap.Height || y0 < 0)
return;
int dr = (int)Math.Sqrt(di * di + dj * dj);
Color colorWalker = colorArr[(dr / 20) % colorArr.Length];
if (sizeWalker > 1)
{
Graphics g = Graphics.FromImage(bitMap);
g.FillRectangle(brushWalker, new Rectangle(x0, y0, sizeWalker, sizeWalker));
g.Dispose();
}
else // sizeWalker = 1;
{
bitMap.SetPixel(x0, y0, colorWalker);
}
}
#endregion
//
public FormDLA()
{
InitializeComponent();
pictureBox1.Size = new Size(1200, 640);
Reset();
}
private void buttonNext_Click(object sender, EventArgs e)
{
DynamicSimulation();
}
private void buttonReset_Click(object sender, EventArgs e)
{
Reset();
}
private void buttonDynamic_Click(object sender, EventArgs e)
{
if (buttonDynamic.Text == "Dynamic")
{
buttonDynamic.Text = "Stop";
timer1.Start();
}
else
{
buttonDynamic.Text = "Dynamic";
timer1.Stop();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(bitMap, 0, 0);
}
private void timer1_Tick(object sender, EventArgs e)
{
DynamicSimulation();
}
private void FormDLA_FormClosing(object sender, FormClosingEventArgs e)
{
if (timer1 != null && timer1.Enabled)
timer1.Stop();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
string text = "Designed by Hor Dashti (h.dashti2@gmail.com)";
MessageBox.Show(text, "About");
}
private void buttonSnapshot_Click(object sender, EventArgs e)
{
string namefile = snapshots.ToString() + ".png";
bitMap.Save(namefile);
snapshots++;
}
}
}