-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
201 lines (178 loc) · 6.93 KB
/
MainWindow.xaml.cs
File metadata and controls
201 lines (178 loc) · 6.93 KB
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
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Color = System.Drawing.Color;
using Brush = System.Drawing.Brush;
namespace SandpileFractalWPF
{
public partial class MainWindow : Window
{
private SandpileFractal sandpile;
private DispatcherTimer timer;
private DispatcherTimer elapsedTimer;
private int size = 400;
private int scaleFactor = 2;
private int sandAmount = 1000;
private DateTime startTime;
private TimeSpan totalElapsedTime;
public MainWindow()
{
InitializeComponent();
sandpile = new SandpileFractal(size);
// Настройка таймера для обновления изображения
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(50); // 20 FPS
timer.Tick += Timer_Tick;
// Настройка таймера для отслеживания времени работы
elapsedTimer = new DispatcherTimer();
elapsedTimer.Interval = TimeSpan.FromSeconds(1);
elapsedTimer.Tick += ElapsedTimer_Tick;
// Настройка начальных значений слайдеров
SizeSlider.Value = size;
ScaleFactorSlider.Value = scaleFactor;
SandAmountSlider.Value = sandAmount;
// Подписка на события изменения значений слайдеров
SizeSlider.ValueChanged += (s, e) => UpdateParameters();
ScaleFactorSlider.ValueChanged += (s, e) => UpdateParameters();
SandAmountSlider.ValueChanged += (s, e) => UpdateParameters();
}
private void Timer_Tick(object sender, EventArgs e)
{
sandpile.AddSand(size / 2, size / 2, sandAmount);
UpdateImage();
}
private void UpdateImage()
{
using (MemoryStream memory = new MemoryStream())
{
sandpile.SaveImage(memory, scaleFactor);
memory.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
SandpileImage.Source = bitmapImage;
}
}
private void UpdateParameters()
{
size = (int)SizeSlider.Value;
scaleFactor = (int)ScaleFactorSlider.Value;
sandAmount = (int)SandAmountSlider.Value;
// Пересоздаем сетку фрактала при изменении размера
sandpile = new SandpileFractal(size);
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
timer.Start();
elapsedTimer.Start();
startTime = DateTime.Now;
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
timer.Stop();
elapsedTimer.Stop();
totalElapsedTime += DateTime.Now - startTime;
}
private void ContinueButton_Click(object sender, RoutedEventArgs e)
{
timer.Start();
elapsedTimer.Start();
startTime = DateTime.Now;
}
private void ElapsedTimer_Tick(object sender, EventArgs e)
{
TimeSpan currentElapsedTime = DateTime.Now - startTime;
TimerTextBlock.Text = $"Время работы: {totalElapsedTime + currentElapsedTime:hh\\:mm\\:ss}";
}
}
public class SandpileFractal
{
private int[,] grid;
private int size;
public SandpileFractal(int size)
{
this.size = size;
grid = new int[size, size];
}
public void AddSand(int x, int y, int amount)
{
if (x < 0 || x >= size || y < 0 || y >= size)
{
throw new ArgumentOutOfRangeException("x or y", "Индекс находился вне границ массива.");
}
grid[x, y] += amount;
Topple();
}
private void Topple()
{
bool toppled = false;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (grid[i, j] >= 4)
{
int excess = grid[i, j] / 4;
grid[i, j] %= 4;
if (i > 0) grid[i - 1, j] += excess;
if (i < size - 1) grid[i + 1, j] += excess;
if (j > 0) grid[i, j - 1] += excess;
if (j < size - 1) grid[i, j + 1] += excess;
toppled = true;
}
}
}
if (toppled)
{
Topple(); // Рекурсивный вызов
}
}
public void SaveImage(Stream stream, int scaleFactor = 1)
{
int scaledSize = size * scaleFactor;
using (Bitmap bitmap = new Bitmap(scaledSize, scaledSize))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
int value = grid[i, j];
Color color = GetColor(value);
using (Brush brush = new SolidBrush(color))
{
g.FillRectangle(brush, i * scaleFactor, j * scaleFactor, scaleFactor, scaleFactor);
}
}
}
}
bitmap.Save(stream, ImageFormat.Png);
}
}
private Color GetColor(int value)
{
switch (value)
{
case 0:
return Color.FromArgb(240, 230, 140); // Светло-желтый
case 1:
return Color.FromArgb(218, 165, 32); // Золотой
case 2:
return Color.FromArgb(184, 134, 11); // Темно-золотой
case 3:
return Color.FromArgb(139, 69, 19); // Коричневый
default:
return Color.FromArgb(255, 0, 0); // Красный (для значений, превышающих 3)
}
}
}
}