-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
193 lines (165 loc) · 6.03 KB
/
MainWindow.xaml.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
using Microsoft.Win32;
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace Fractals {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
bitmap = new WriteableBitmap(2000, 2000, 500d, 500d, PixelFormats.Bgra32, null);
pixels = new byte[bitmap.PixelWidth][][];
for (int i = 0; i < bitmap.PixelWidth; i++) {
pixels[i] = new byte[bitmap.PixelHeight][];
for (int j = 0; j < bitmap.PixelHeight; j++)
pixels[i][j] = new byte[4];
}
height = bitmap.PixelHeight;
width = bitmap.PixelWidth; //wymiary bitmapy
pgBar.Maximum = height;
}
WriteableBitmap bitmap;
int width, height, iterations = 255;
byte[][][] pixels;
double offX = 0, offY = 0, zoom = 1, actualZoom = 1, actualOffX = 0, actualOffY = 0;
Image image;
bool isCalcOn = false;
private void clrBtn_Click(object sender, RoutedEventArgs e) {
canvas.Children.Clear();
pgBar.Value = 0;
}
#region zoom
private void zoomTxtBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e) {
Regex regex = new Regex("[^0-9.-]+");
e.Handled = regex.IsMatch(e.Text);
}
private void zoomTxtBox_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) {
zoom *= Math.Pow(10, e.Delta / 120);
zoomTxtBox.Text = zoom.ToString();
}
private void zoomBtnPLus_Click(object sender, RoutedEventArgs e) {
zoom = double.Parse(zoomTxtBox.Text) * 10;
zoomTxtBox.Text = zoom.ToString();
}
private void zoomBtnMinus_Click(object sender, RoutedEventArgs e) {
zoom = double.Parse(zoomTxtBox.Text) / 10;
zoomTxtBox.Text = zoom.ToString();
}
#endregion
#region offX & offY txtboxes
private void upBtn_Click(object sender, RoutedEventArgs e) {
offY = double.Parse(offYTxtBox.Text) + 1 / zoom;
offYTxtBox.Text = offY.ToString();
}
private void downBtn_Click(object sender, RoutedEventArgs e) {
offY = double.Parse(offYTxtBox.Text) - 1 / zoom;
offYTxtBox.Text = offY.ToString();
}
private void leftBtn_Click(object sender, RoutedEventArgs e) {
offX = double.Parse(offXTxtBox.Text) - 1 / zoom;
offXTxtBox.Text = offX.ToString();
}
private void rightBtn_Click(object sender, RoutedEventArgs e) {
offX = double.Parse(offXTxtBox.Text) + 1 / zoom;
offXTxtBox.Text = offX.ToString();
}
private void offXTxtBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e) {
Regex regex = new Regex("[^0-9.-]+");
e.Handled = regex.IsMatch(e.Text);
}
private void offYTxtBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e) {
Regex regex = new Regex("[^0-9.-]+");
e.Handled = regex.IsMatch(e.Text);
}
#endregion
private void saveImageAs_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
SaveFileDialog dialog = new SaveFileDialog();
if (dialog.ShowDialog() == true) {
using (FileStream stream = new FileStream(dialog.FileName, FileMode.Create)) {
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.Save(stream);
}
}
}
private void iterationsTxtBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e) {
Regex regex = new Regex("[^0-9.-]+");
e.Handled = regex.IsMatch(e.Text);
}
private void canvas_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
offX = (e.GetPosition(canvas).X - canvas.Width / 2) / (zoom * canvas.Width / 4) + actualOffX;
offY = -(e.GetPosition(canvas).Y - canvas.Height / 2) / (zoom * canvas.Height / 4) + actualOffY;
offXTxtBox.Text = offX.ToString();
offYTxtBox.Text = offY.ToString();
}
byte checkConvergence(double[] c, int acc) {
double[] z = { 0, 0 }; //z0
double[] oldZ = new double[2];
for (int i = 0; i < acc; i++) {
if (z[0] * z[0] + z[1] * z[1] > 4) {
return (byte)(255 * i / acc);
}
oldZ[0] = z[0];
oldZ[1] = z[1];
z[0] = oldZ[0] * oldZ[0] - oldZ[1] * oldZ[1] + c[0];
z[1] = 2 * oldZ[0] * oldZ[1] + c[1]; //zN+1 = zN^2 + c
}
return 255;
}
private void CalcButton_Click(object sender, RoutedEventArgs e) {
if (!isCalcOn) {
isCalcOn = true;
int count = 0;
actualZoom = double.Parse(zoomTxtBox.Text);
actualOffX = double.Parse(offXTxtBox.Text);
actualOffY = double.Parse(offYTxtBox.Text);
pgBar.Value = 0;
//var s = Stopwatch.StartNew();
double[] z = new double[2];
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
z[0] = (double)(col - width / 2) / (actualZoom * 500d) + offX;
z[1] = -(double)(row - height / 2) / (actualZoom * 500d) + offY;
pixels[row][col][3] = (byte)checkConvergence(z, iterations); //nadaje kolor (alpha) w zaleznosci od ilosci iteracji
}
count++;
if (count % 10 == 0) pgBar.Dispatcher.Invoke(() => pgBar.Value = count, DispatcherPriority.Background);
}
//s.Stop();
//System.Console.WriteLine(s.ElapsedMilliseconds);
/*for (int i = -7; i <= 7; i++) {//iksik
pixels[width / 2 + i, height / 2 + i, 2] = 0xFF;
pixels[width / 2 + i, height / 2 - i, 2] = 0xFF;
}*/
byte[] pixels1d = new byte[width * height * 4];
int index = 0;
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
for (int i = 0; i < 4; i++)
pixels1d[index++] = pixels[row][col][i];
}
}
Int32Rect rect = new Int32Rect(0, 0, width, height);
int stride = 4 * width;
bitmap.WritePixels(rect, pixels1d, stride, 0);
image = new Image() {
Source = bitmap,
Stretch = Stretch.Fill,
Margin = new Thickness(0),
Width = 700,
Height = 700
};
canvas.Children.Clear();
canvas.Children.Add(image);
isCalcOn = false;
}
}
}
}