forked from anako18/Graphics4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
362 lines (302 loc) · 12.8 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media;
using System.Drawing.Drawing2D;
using MathNet.Numerics.LinearAlgebra.Double;
namespace graphics4
{
public partial class Form1 : Form
{
Graphics g;
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black);
addPoint addPointForm;
addEdge addEdgeForm;
addPolygon addPolygonForm;
//Координаты "центра"
int OX;
int OY;
public Form1()
{
InitializeComponent();
addPointForm = new addPoint(this, pictureBox1.Width, pictureBox1.Height);
addEdgeForm = new addEdge(this, addPointForm);
addPolygonForm = new addPolygon(this, addPointForm);
Points.ContextMenuStrip = contextMenuStrip1;
Edges.ContextMenuStrip = contextMenuStrip2;
Polygons.ContextMenuStrip = contextMenuStrip3;
}
public Dictionary<string, Point> points_list;
public Dictionary<string, Tuple<Point, Point>> edges_list;
public Dictionary<string, List<Point>> polygon_list;
private void Form1_Load(object sender, EventArgs e)
{
points_list = new Dictionary<string, Point>();
edges_list = new Dictionary<string, Tuple<Point, Point>>();
polygon_list = new Dictionary<string, List<Point>>();
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = bmp;
g = Graphics.FromImage(bmp);
OX = (pictureBox1.Width / 2);
OY = (pictureBox1.Height / 2);
Redraw();
}
public void AddNewPoint(string name, int x, int y)
{
points_list.Add(name, new Point(x, y));
Points.Items.Add(points_list.Last());
Draw_point();
//((addEdgeForm.Controls["panel1"] as Panel).Controls["listBox1"] as ListBox).Items.Add(points_list.Last());
// ((addEdgeForm.Controls["panel2"] as Panel).Controls["listBox2"] as ListBox).Items.Add(points_list.Last());
// (addPolygonForm.Controls["listBox1"] as ListBox).Items.Add(points_list.Last());
Points.Enabled = true;
}
public ListBox getPoints()
{
return Points;
}
public static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
public void AddNewEdge(string name, Point A, Point B)
{
if (!edges_list.ContainsKey(name) && !edges_list.ContainsKey(Reverse(name)))
{
edges_list.Add(name, Tuple.Create(A, B));
Edges.Items.Add(edges_list.Last());
g.DrawLine(pen, A.X + OX, A.Y + OY, B.X + OX, B.Y + OY);
pictureBox1.Refresh();
Edges.Enabled = true;
}
}
public void refresh_listboxes()
{
if (Points.Items.Count > 0)
{
((addEdgeForm.Controls["panel1"] as Panel).Controls["listBox1"] as ListBox).Items.Clear();
((addEdgeForm.Controls["panel1"] as Panel).Controls["listBox1"] as ListBox).Items.AddRange(Points.Items);
((addEdgeForm.Controls["panel2"] as Panel).Controls["listBox2"] as ListBox).Items.Clear();
((addEdgeForm.Controls["panel2"] as Panel).Controls["listBox2"] as ListBox).Items.AddRange(Points.Items);
(addPolygonForm.Controls["listBox1"] as ListBox).Items.Clear();
(addPolygonForm.Controls["listBox1"] as ListBox).Items.AddRange(Points.Items);
}
}
private void addPointToolStripMenuItem_Click(object sender, EventArgs e)
{
refresh_listboxes();
addPointForm.ShowDialog();
}
private void Draw_point()
{
Font drawFont = new Font("Arial", 10);
SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Black);
g.DrawString(points_list.Last().Key,drawFont,drawBrush, points_list.Last().Value.X + OX, -points_list.Last().Value.Y + OY);
g.DrawEllipse(pen, new Rectangle(points_list.Last().Value.X + OX, -points_list.Last().Value.Y + OY, 1, 1));
pictureBox1.Refresh();
}
private void addEdgeToolStripMenuItem_Click(object sender, EventArgs e)
{
refresh_listboxes();
addEdgeForm.ShowDialog();
}
public void Redraw()
{
g.Clear(System.Drawing.Color.White);
//Draw OX and OY
System.Drawing.Pen penLightGrey = new System.Drawing.Pen(System.Drawing.Color.LightGray);
int h = pictureBox1.Height;
int w = pictureBox1.Width;
//OX
g.DrawLine(penLightGrey, 0, h / 2, w, h / 2);
g.DrawLine(penLightGrey, w - 10, (h / 2) - 2, w, h / 2);
g.DrawLine(penLightGrey, w - 10, (h / 2) + 2, w, h / 2);
//OY
g.DrawLine(penLightGrey, w / 2, 0, w / 2, h);
g.DrawLine(penLightGrey, (w / 2) - 2, 10, w / 2, 0);
g.DrawLine(penLightGrey, (w / 2) + 2, 10, w / 2, 0);
Font drawFont = new Font("Arial", 10);
SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Black);
//Draw Points
foreach (var pnt in points_list)
{
g.DrawEllipse(pen, new Rectangle(pnt.Value.X + OX, -pnt.Value.Y + OY, 1, 1));
g.DrawString(pnt.Key, drawFont, drawBrush, pnt.Value.X + OX, -pnt.Value.Y + OY);
}
//Draw Edges
foreach (var edg in edges_list)
{
g.DrawLine(pen, edg.Value.Item1.X + OX, -edg.Value.Item1.Y + OY, edg.Value.Item2.X + OX, -edg.Value.Item2.Y + OY);
}
this.Refresh();
}
//Delete Point
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
//Delete all the edges with this point
for (int i = edges_list.Count-1; i>=0; i--)
{
if (edges_list.ElementAt(i).Key.Contains(Points.SelectedItem.ToString().Split(',')[0].Substring(1)))
{
Edges.Items.Remove(edges_list.ElementAt(i));
edges_list.Remove(edges_list.ElementAt(i).Key);
}
}
points_list.Remove(Points.SelectedItem.ToString().Split(',')[0].Substring(1));
Points.Items.Remove(Points.SelectedItem);
Redraw();
}
//Delete edge
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
edges_list.Remove(Edges.SelectedItem.ToString().Split(',')[0].Substring(1));
Edges.Items.Remove(Edges.SelectedItem);
Redraw();
}
//Delete polygon
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
List<Point> pol = polygon_list[Polygons.SelectedItem.ToString().Split(',')[0].Substring(1)];
int cnt = 0;
foreach (var pnt in pol)
{
//Delete all the edges with this points
for (int i = edges_list.Count - 1; i >= 0; i--)
{
if (edges_list.ElementAt(i).Key.Contains(Polygons.SelectedItem.ToString().Split(',')[0].Substring(1)[i]))
{
Edges.Items.Remove(edges_list.ElementAt(i));
edges_list.Remove(edges_list.ElementAt(i).Key);
}
}
//Delete points
points_list.Remove(Polygons.SelectedItem.ToString().Split(',')[0].Substring(1)[cnt].ToString());
for (var i = Points.Items.Count - 1; i >= 0; i--)
{
if (Points.Items[i].ToString().Contains(Polygons.SelectedItem.ToString().Split(',')[0].Substring(1)[cnt].ToString()))
Points.Items.Remove(Points.Items[i]);
}
cnt++;
}
//Delete polygon
polygon_list.Remove(Polygons.SelectedItem.ToString().Split(',')[0].Substring(1));
Polygons.Items.Remove(Polygons.SelectedItem);
Polygons.Items.Remove(Polygons.SelectedItem);
Redraw();
}
private void addPolygonToolStripMenuItem_Click(object sender, EventArgs e)
{
refresh_listboxes();
addPolygonForm.ShowDialog();
}
public void addPolygon()
{
Polygons.Items.Add(polygon_list.Last());
Polygons.Enabled = true;
Redraw();
}
private void Points_MouseDown(object sender, MouseEventArgs e)
{
Edges.ClearSelected();
Polygons.ClearSelected();
}
private void Edges_MouseDown(object sender, MouseEventArgs e)
{
Points.ClearSelected();
Polygons.ClearSelected();
}
private void Polygons_MouseDown(object sender, MouseEventArgs e)
{
Points.ClearSelected();
Edges.ClearSelected();
}
public double[,] MultiplyMatrix(double[,] A, double[,] B)
{
int rA = A.GetLength(0);
int cA = A.GetLength(1);
int rB = B.GetLength(0);
int cB = B.GetLength(1);
double temp = 0;
double[,] kHasil = new double[rA, cB];
if (cA != rB)
{
Console.WriteLine("matrix can't be multiplied !!");
return kHasil;
}
else
{
for (int i = 0; i < rA; i++)
{
for (int j = 0; j < cB; j++)
{
temp = 0;
for (int k = 0; k < cA; k++)
{
temp += A[i, k] * B[k, j];
}
kHasil[i, j] = temp;
}
}
return kHasil;
}
}
void connect_all_the_points(List<Point> lp)
{
g.Clear(System.Drawing.Color.White);
System.Drawing.Pen penLightGrey = new System.Drawing.Pen(System.Drawing.Color.LightGray);
int h = pictureBox1.Height;
int w = pictureBox1.Width;
//OX
g.DrawLine(penLightGrey, 0, h / 2, w, h / 2);
g.DrawLine(penLightGrey, w - 10, (h / 2) - 2, w, h / 2);
g.DrawLine(penLightGrey, w - 10, (h / 2) + 2, w, h / 2);
//OY
g.DrawLine(penLightGrey, w / 2, 0, w / 2, h);
g.DrawLine(penLightGrey, (w / 2) - 2, 10, w / 2, 0);
g.DrawLine(penLightGrey, (w / 2) + 2, 10, w / 2, 0);
for (int i = 0; i < lp.Count - 1; i++)
{
g.DrawLine(pen, lp[i].X + OX, lp[i].Y + OY, lp[i+1].X + OX, lp[i+1].Y + OY);
}
g.DrawLine(pen, lp[lp.Count - 1].X + OX, lp[lp.Count - 1].Y + OY, lp[0].X + OX, lp[0].Y + OY);
pictureBox1.Refresh();
}
//Rotation
private void button1_Click(object sender, EventArgs e)
{
if (polygon_list.Count == 0) return;
double a = System.Convert.ToDouble(numericUpDown2.Value);
double b = System.Convert.ToDouble(numericUpDown3.Value);
double angle = Math.PI * System.Convert.ToDouble(numericUpDown1.Value) / 180.0;
var ToAB = new double[,] { { 1.0, 0, 0 }, { 0, 1.0, 0 }, { -a, -b, 1.0 } };
var rotation_matrix = new double[,] {
{ Math.Cos(angle), Math.Sin(angle), 0},
{(-1)*Math.Sin(angle), Math.Cos(angle),0},
{0,0,1 }
};
var FromAB = new double [,] { { 1.0, 0, 0 }, { 0, 1.0, 0 }, { a, b, 1.0 } };
double[,] res = MultiplyMatrix(ToAB, rotation_matrix);
res = MultiplyMatrix(res, FromAB);
List<Point> newPoints = new List<Point>();
foreach (Point p in polygon_list.First().Value)
{
double px = p.X;
double py = p.Y;
double[,] ff = { { px, py, 1 } };
double[,] result_arr = MultiplyMatrix(ff, res);
newPoints.Add(new Point(System.Convert.ToInt32(result_arr.GetValue(0,0)), System.Convert.ToInt32(result_arr.GetValue(0,1))));
}
polygon_list.First().Value.Clear();
polygon_list.First().Value.AddRange(newPoints);
connect_all_the_points(newPoints);
}
}
}