-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
201 lines (168 loc) · 6.83 KB
/
Form1.cs
File metadata and controls
201 lines (168 loc) · 6.83 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.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace Computer_Modeling_Task
{
public partial class Form1 : Form
{
private ChartManager _manager;
private int minimumChartWidth;
private int minimumChartHeight;
private Point _prevMousePos;
private bool isMouseDown = false;
public Form1()
{
InitializeComponent();
AddEvents();
InitFields();
}
private void AddEvents()
{
Chart.MouseWheel += new MouseEventHandler(Chart_MouseWheel);
}
private void InitFields()
{
_manager = new ChartManager(Chart);
minimumChartWidth = Chart.Size.Width;
minimumChartHeight = Chart.Size.Height;
}
private void Chart_MouseWheel(object sender, MouseEventArgs e)
{
var oldChartSize = Chart.Size;
if (e.Delta > 0)
Chart.Size = new Size(Chart.Size.Width + minimumChartWidth / 10, Chart.Size.Height + minimumChartHeight / 10);
else if (e.Delta < 0)
{
int newWidth = Chart.Size.Width - minimumChartWidth / 10;
int newHeight = Chart.Size.Height - minimumChartHeight / 10;
if (newWidth >= minimumChartWidth && newHeight >= minimumChartHeight)
Chart.Size = new Size(newWidth, newHeight);
}
}
private void ExampleChart_Button_Click(object sender, EventArgs e)
{
ExampleChartBuilder.BuildExampleChart(Chart);
}
private void ClearChartArea_Button_Click(object sender, EventArgs e)
{
_manager.ClearAll();
}
private void Draw_Button_Click(object sender, EventArgs e)
{
if (AreFieldsEmpty())
return;
string choosedChart = ChooseChart_ComboBox.SelectedItem.ToString();
string choosedMethod = ChooseMethod_ComboBox.SelectedItem.ToString();
var startX = 1.0;
var startY = 0.0;
var stepValue = 0.0;
var iterations = 0;
if (StartX_TextBox.Text.Length != 0 && StartY_TextBox.Text.Length != 0)
{
if (!double.TryParse(StartX_TextBox.Text, out startX) || !double.TryParse(StartY_TextBox.Text, out startY))
{
MessageBox.Show("Недопустимые координаты.");
return;
}
}
if (StepValue_TextBox.Text.Length != 0 && !double.TryParse(StepValue_TextBox.Text, out stepValue))
{
MessageBox.Show("Недопустимое значение шага.");
return;
}
if (Iterations_TextBox.Text.Length != 0 && !int.TryParse(Iterations_TextBox.Text, out iterations)
|| iterations < 0)
{
MessageBox.Show("Недопустимое количество итераций.");
return;
}
BaseChart chartType = GetChartType();
double[] parameters = GetParameters();
string methodName = ChooseMethod_ComboBox.SelectedItem.ToString();
_manager.Draw(chartType, methodName, startX, startY, stepValue, iterations, parameters);
}
private double[] GetParameters()
{
var parameters = new List<double>();
var splitResult = Parameters_TextBox.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
if (splitResult.Length == 0)
return null;
var parameter = 0.0;
for (int i = 0; i < splitResult.Length; i++)
{
if (!double.TryParse(splitResult[i], out parameter))
{
MessageBox.Show("Один или несколько параметров введены неверно.");
return null;
}
else
parameters.Add(parameter);
}
return parameters.ToArray();
}
//TODO: как то избавиться от явного задания строк. Переписать return'ы
private BaseChart GetChartType()
{
var choice = ChooseChart_ComboBox.SelectedItem.ToString();
if (choice.Equals("Тестовый пример"))
return new TestChart();
if (choice.Equals("Электрический генератор с жестким возбуждением"))
return new MyTaskChart();
return null;
}
private bool AreFieldsEmpty()
{
if (ChooseChart_ComboBox.SelectedItem == null || ChooseMethod_ComboBox.SelectedItem == null)
{
MessageBox.Show("Выберите график и метод");
return true;
}
else if (StartX_TextBox.Text.Length == 0 && StartY_TextBox.Text.Length != 0)
{
MessageBox.Show("Введите X координату");
return true;
}
else if (StartX_TextBox.Text.Length != 0 && StartY_TextBox.Text.Length == 0)
{
MessageBox.Show("Введите Y координату");
return true;
}
return false;
}
private void Chart_MouseDown(object sender, MouseEventArgs e)
{
_prevMousePos = e.Location;
isMouseDown = true;
System.Windows.Forms.Cursor.Current = Cursors.Hand;
}
//TODO: Попробовать решить проблему с мерцанием
private void Chart_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
int mousePosX = e.X;
int mousePosY = e.Y;
int diffX = mousePosX - _prevMousePos.X;
int diffY = mousePosY - _prevMousePos.Y;
Chart.Location = new Point(Chart.Location.X + diffX, Chart.Location.Y + diffY);
_prevMousePos.X = mousePosX;
_prevMousePos.Y = mousePosY;
}
}
private void Chart_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
System.Windows.Forms.Cursor.Current = Cursors.Default;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
ChooseChart_ComboBox.SelectedIndex = 1;
ChooseMethod_ComboBox.SelectedIndex = 1;
StartX_TextBox.Text = "1";
StartY_TextBox.Text = "0";
}
}
}