-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
236 lines (209 loc) · 7.3 KB
/
Form1.cs
File metadata and controls
236 lines (209 loc) · 7.3 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Fractal
{
public partial class MainForm : Form
{
Mondelbrot mondelbrot;
private Point initialPoint;
private bool EventHandlerEnabled = true;
private const decimal x0 = -2, y0 = -2, size = 4;
public void DrawFrac()
{
Cursor.Current = Cursors.WaitCursor;
UseWaitCursor = true;
mondelbrot = new Mondelbrot(PB.ClientSize, (int)udIter.Value, (double)udX.Value, (double)udY.Value, (double)udSize.Value);
PB.Invalidate();
UseWaitCursor = false;
Cursor.Current = Cursors.Default;
}
private void ResetValues()
{
udX.Value = x0;
udY.Value = y0;
udSize.Value = size;
}
public MainForm()
{
InitializeComponent();
cbZoom.SelectedIndex = 0;
DrawFrac();
}
private void btDraw_Click(object sender, EventArgs e)
{
DrawFrac();
}
private void PB_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(mondelbrot.bmp, 0, 0);
}
private void PB_MouseDoubleClick(object sender, MouseEventArgs e)
{
int zoom = Int32.Parse(cbZoom.SelectedItem.ToString());
Zoom(e.Location, zoom);
}
private void SetUd(NumericUpDown ud, decimal value)
{
if (ud.Minimum > value)
ud.Value = ud.Minimum;
else
if (ud.Maximum < value)
ud.Value = ud.Maximum;
else
ud.Value = value;
}
private void Zoom(Point p, decimal scale)
{
EventHandlerEnabled = false;
decimal x_centr = udSize.Value / PB.ClientSize.Width * p.X + udX.Value;
decimal y_centr = udSize.Value / PB.ClientSize.Height * p.Y + udY.Value;
if (rbPlus.Checked)
SetUd(udSize, udSize.Value / scale);
else
SetUd(udSize, udSize.Value * scale);
SetUd(udX, x_centr - udSize.Value / 2);
SetUd(udY, y_centr - udSize.Value / 2);
EventHandlerEnabled = true;
DrawFrac();
}
private void udIter_ValueChanged(object sender, EventArgs e)
{
if(EventHandlerEnabled)
DrawFrac();
}
private void btReset_Click(object sender, EventArgs e)
{
ResetValues();
}
private void PB_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button ==MouseButtons.Left)
initialPoint = e.Location;
}
private void PB_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
EventHandlerEnabled = false;
int deltaX = initialPoint.X - e.X;
int deltaY = initialPoint.Y - e.Y;
SetUd(udX, udSize.Value / PB.ClientSize.Width * deltaX + udX.Value);
SetUd(udY, udSize.Value / PB.ClientSize.Height * deltaY + udY.Value);
EventHandlerEnabled = true;
DrawFrac();
}
}
private void PB_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Graphics g = PB.CreateGraphics();
g.Clear(PB.BackColor);
g.DrawImage(mondelbrot.bmp, e.X - initialPoint.X, e.Y - initialPoint.Y);
}
}
private void MainForm_Resize(object sender, EventArgs e)
{
int W = this.Width - 220;
int H = this.Height - 40;
int size = W > H ? H : W;
PB.Width = size;
PB.Height = size;
}
private void MainForm_ResizeEnd(object sender, EventArgs e)
{
DrawFrac();
}
private void timer_Tick(object sender, EventArgs e)
{
udIter.Value++;
}
private void btSaveImg_Click(object sender, EventArgs e)
{
if (sfdPic.ShowDialog() == DialogResult.OK)
{
switch (sfdPic.FilterIndex)
{
case 1:
mondelbrot.bmp.Save(sfdPic.FileName, ImageFormat.Png);
break;
case 2:
mondelbrot.bmp.Save(sfdPic.FileName, ImageFormat.Bmp);
break;
default:
mondelbrot.bmp.Save(sfdPic.FileName, ImageFormat.Jpeg);
break;
}
}
}
private void btLoadBin_Click(object sender, EventArgs e)
{
if (ofdCoord.ShowDialog() == DialogResult.OK)
{
BinaryReader br = new BinaryReader(File.Open(ofdCoord.FileName, FileMode.Open));
SetUd(udX, br.ReadDecimal());
SetUd(udY, br.ReadDecimal());
SetUd(udSize, br.ReadDecimal());
SetUd(udIter, br.ReadDecimal());
br.Dispose();
DrawFrac();
}
}
private void btSaveTxt_Click(object sender, EventArgs e)
{
if (sfdCoordTxt.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(sfdCoordTxt.FileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(udX.Value.ToString());
sw.WriteLine(udY.Value.ToString());
sw.WriteLine(udSize.Value.ToString());
sw.WriteLine(udIter.Value.ToString());
sw.Dispose();
}
}
private void btLoadTxt_Click(object sender, EventArgs e)
{
if (ofdCoordTxt.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(ofdCoordTxt.FileName, FileMode.Open);
StreamReader sr = new StreamReader(fs);
SetUd(udX, Convert.ToDecimal(sr.ReadLine()));
SetUd(udY, Convert.ToDecimal(sr.ReadLine()));
SetUd(udSize, Convert.ToDecimal(sr.ReadLine()));
SetUd(udIter, Convert.ToDecimal(sr.ReadLine()));
sr.Dispose();
DrawFrac();
}
}
private void btSaveBin_Click(object sender, EventArgs e)
{
if (sfdCoord.ShowDialog() == DialogResult.OK)
{
BinaryWriter bw = new BinaryWriter(File.Open(sfdCoord.FileName, FileMode.Create));
bw.Write(udX.Value);
bw.Write(udY.Value);
bw.Write(udSize.Value);
bw.Write(udIter.Value);
bw.Dispose();
}
}
private void btTimer_Click(object sender, EventArgs e)
{
timer.Enabled = !timer.Enabled;
if (btTimer.Text == "Старт")
btTimer.Text = "Стоп";
else
btTimer.Text = "Старт";
}
}
}