-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
133 lines (122 loc) · 3.63 KB
/
Form1.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Approximation_dots
{
public partial class MainForm : Form
{
private int width;
private int height;
private float a;
private float b;
private int sumP, sumX, sumY, sumX2;
private int max = int.MinValue;
private int min = int.MaxValue;
private int x_max;
private int x_min;
private Graphics g;
private List<int> x;
private List<int> y;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
width = ClientSize.Width;
height = ClientSize.Height;
x = new List<int>();
y = new List<int>();
sumP = 0;
sumX = 0;
sumY = 0;
sumX2 = 0;
a = 0;
b = 0;
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
g = e.Graphics;
for (int i = 0; i < x.Count; i++)
{
g.FillEllipse(new SolidBrush(Color.Black), x[i] - 4, Math.Abs(-height + y[i]) - 4, 8, 8);
}
if (min != int.MaxValue && max != int.MinValue)
{
g.DrawLine(new Pen(Color.Red, 2), x_min, min, x_max, max);
}
}
private void clear_b_Click(object sender, EventArgs e)
{
max = int.MinValue;
min = int.MaxValue;
sumP = 0;
sumX = 0;
sumY = 0;
sumX2 = 0;
a = 0;
b = 0;
x.Clear();
y.Clear();
Invalidate();
}
private void MainForm_MouseClick(object sender, MouseEventArgs e)
{
x.Add(e.X);
y.Add(height - e.Y);
max = int.MinValue;
min = int.MaxValue;
if (x.Count > 1)
{
Approximating();
for (int i = 0; i < width; i++)
{
if (i * a + b < height)
{
if (i * a + b > max)
{
max = (int)(i * a + b);
x_max = i;
}
if (i * a + b < min)
{
min = (int)(i * a + b);
x_min = i;
}
}
else
{
break;
}
}
}
label1.Text = "a: " + a;
label2.Text = "b: " + b;
Invalidate();
}
private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
x_label.Text = "X: " + e.X;
y_label.Text = "Y: " + e.Y;
}
private void Approximating()
{
sumP = 0;
sumX = 0;
sumY = 0;
sumX2 = 0;
a = 0;
b = 0;
for (int i = 0; i < x.Count; i++)
{
sumP += x[i] * y[i];
sumX += x[i];
sumY += y[i];
sumX2 += x[i] * x[i];
}
a = (float)(x.Count * sumP - sumX * sumY) / (float)(x.Count * sumX2 - sumX * sumX);
b = (float)(sumY - a * sumX) / (float)x.Count;
}
}
}