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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace lab_5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
b = new Bitmap(this.work_panel.Width, this.work_panel.Height);
g = Graphics.FromImage(b);
this.position_x.MouseWheel += new MouseEventHandler(position_x_MouseWheel);
this.position_y.MouseWheel += new MouseEventHandler(position_y_MouseWheel);
this.rotation.MouseWheel += new MouseEventHandler(rotation_MouseWheel);
this.zoom.MouseWheel += new MouseEventHandler(zoom_MouseWheel);
}
// Глобальные переменные
Graphics g;
Bitmap b;
Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
Brush brush = new SolidBrush(Color.FromArgb(255, 0, 0, 0));
bool DoDraw = false, CanDraw = true, Shift = false, Ctrl = false;
Point[] start = new Point[0];
Point[] cord = new Point[0];
Point[] temp = new Point[0];
Point dp, sdp, maxp, minp;
int q = 0, i = 0, dx, dy;
Color cp = Color.FromArgb(255, 0, 0, 0);
float dz = 1, dr = 1;
// EXIT
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
g.Dispose();
b.Dispose();
b = new Bitmap(this.work_panel.Width, this.work_panel.Height);
g = Graphics.FromImage(b);
this.work_panel.Image = b;
//g = this.work_panel.CreateGraphics();
//po = new PaintedObject(pen, new GraphicsPath());
q = 0;
start = new Point[0];
cord = new Point[0];
if (CanDraw == false) CanDraw = true;
this.position_x.Text = String.Empty;
this.position_y.Text = String.Empty;
this.rotation.Text = String.Empty;
this.zoom.Text = String.Empty;
this.position_x.ReadOnly = true;
this.position_y.ReadOnly = true;
this.rotation.ReadOnly = true;
this.zoom.ReadOnly = true;
dz = 1;
dr = 1;
temp = new Point[0];
}
private void button3_Click(object sender, EventArgs e)
{
if (CanDraw == true) CanDraw = false;
if (q > 2)
{
g.DrawLine(pen, start[q - 1], start[0]);
//g.DrawRectangle(pen, minp.X, minp.Y, maxp.X - minp.X, maxp.Y - minp.Y);
this.work_panel.Image = b;
}
dp.X = maxp.X - ((maxp.X - minp.X) / 2);
dp.Y = maxp.Y - ((maxp.Y - minp.Y) / 2);
sdp = dp;
this.position_x.ReadOnly = false;
this.position_y.ReadOnly = false;
this.rotation.ReadOnly = false;
this.zoom.ReadOnly = false;
this.rotation.Text = "0";
this.zoom.Text = "100";
this.position_x.Text = dp.X.ToString();
this.position_y.Text = dp.Y.ToString();
}
private void work_panel_MouseEnter(object sender, EventArgs e)
{
if (DoDraw == false)
{
DoDraw = true;
}
}
private void work_panel_MouseLeave(object sender, EventArgs e)
{
if (DoDraw == true)
{
DoDraw = false;
}
}
private void work_panel_MouseClick(object sender, MouseEventArgs e)
{
if (DoDraw == true && CanDraw == true)
{
q++;
if (q == 1)
{
start = new Point[q];
start[0] = e.Location;
minp = start[0];
maxp = start[0];
}
if (q > 1)
{
temp = new Point[q];
for (i = 0; i < q - 1; i++)
{
temp[i] = start[i];
}
start = new Point[q];
temp[q - 1] = e.Location;
start = temp;
temp = new Point[0];
g.DrawLines(pen, start);
this.work_panel.Image = b;
minp.X = Math.Min(minp.X, start[q - 1].X);
minp.Y = Math.Min(minp.Y, start[q - 1].Y);
maxp.X = Math.Max(maxp.X, start[q - 1].X);
maxp.Y = Math.Max(maxp.Y, start[q - 1].Y);
}
b.SetPixel(e.X, e.Y, cp);
this.work_panel.Image = b;
}
}
private void zoom_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) e.Handled = true;
}
private void rotation_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != ',' && e.KeyChar != '-') e.Handled = true;
}
00:44:46
private void position_x_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '-') e.Handled = true;
}
private void position_y_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '-') e.Handled = true;
}
private void position_x_TextChanged(object sender, EventArgs e)
{
if (q > 0)
{
//int dx;
if (this.position_x.Text == String.Empty) this.position_x.Text = "0";
dx = int.Parse(this.position_x.Text);
dx -= dp.X;
for (i = 0; i < q; i++)
{
cord[i].X += dx;
}
dp.X += dx;
b = new Bitmap(this.work_panel.Width, this.work_panel.Height);
g = Graphics.FromImage(b);
for (i = 0; i < q - 1; i++)
{
g.DrawLine(pen, cord[i].X + temp[i].X, cord[i].Y + temp[i].Y, cord[i + 1].X + temp[i + 1].X, cord[i + 1].Y + temp[i + 1].Y);
}
g.DrawLine(pen, cord[q - 1].X + temp[q - 1].X, cord[q - 1].Y + temp[q - 1].Y, cord[0].X + temp[0].X, cord[0].Y + temp[0].Y);
this.work_panel.Image = b;
}
}
void position_x_MouseWheel(object sender, MouseEventArgs e)
{
//throw new NotImplementedException();
if (this.position_x.Text.Length != 0)
{
dx = int.Parse(this.position_x.Text);
if (e.Delta > 0)
{
if (Shift)
{
dx += 10;
}
else
{
dx++;
}
}
if (e.Delta < 0)
{
if (Shift)
{
dx -= 10;
}
else
{
dx--;
}
}
this.position_x.Text = (dx).ToString();
}
}
private void position_y_TextChanged(object sender, EventArgs e)
{
if (q > 0)
{
//int dy;
if (this.position_y.Text == String.Empty) this.position_y.Text = "0";
dy = int.Parse(this.position_y.Text);
dy -= dp.Y;
for (i = 0; i < q; i++)
{
cord[i].Y += dy;
}
dp.Y += dy;
b = new Bitmap(this.work_panel.Width, this.work_panel.Height);
g = Graphics.FromImage(b);
for (i = 0; i < q - 1; i++)
{
g.DrawLine(pen, cord[i].X + temp[i].X, cord[i].Y + temp[i].Y, cord[i + 1].X + temp[i + 1].X, cord[i + 1].Y + temp[i + 1].Y);
}
g.DrawLine(pen, cord[q - 1].X + temp[q - 1].X, cord[q - 1].Y + temp[q - 1].Y, cord[0].X + temp[0].X, cord[0].Y + temp[0].Y);
this.work_panel.Image = b;
}
}
void position_y_MouseWheel(object sender, MouseEventArgs e)
{
//throw new NotImplementedException();
if (this.position_y.Text.Length != 0)
{
dy = int.Parse(this.position_y.Text);
if (e.Delta > 0)
{
if (Shift)
{
dy += 10;
}
else
{
dy++;
}
}
if (e.Delta < 0)
{
if (Shift)
{
dy -= 10;
}
else
{
dy--;
}
}
this.position_y.Text = (dy).ToString();
}
}
private void rotation_TextChanged(object sender, EventArgs e)
{
if (q > 0)
{
// Поворот
cord = new Point[q];
int rx = 0, ry = 0;
float delx, dely, R;
if (this.rotation.Text == String.Empty) this.rotation.Text = "0";
if (float.Parse(this.rotation.Text) > 180)
{
this.rotation.Text = ((float)(-360 + float.Parse(this.rotation.Text))).ToString();
}
if (float.Parse(this.rotation.Text) < -180)
{
this.rotation.Text = ((float)(360 + float.Parse(this.rotation.Text))).ToString();
}
if (dr != (float)(((float)(float.Parse(this.rotation.Text))) * Math.PI / 180))
{
dr = (float)(((float)(float.Parse(this.rotation.Text))) * Math.PI / 180);
for (i = 0; i < q; i++)
{
R = (float)(Math.Sqrt(Math.Pow(Math.Abs(sdp.X - start[i].X), 2) + Math.Pow(Math.Abs(sdp.Y - start[i].Y), 2)));
delx = (float)(Math.Asin((float)((start[i].X - sdp.X)) / R));
dely = (float)(Math.Acos((float)((start[i].Y - sdp.Y)) / R));
/*if (delx > -Math.PI / 2 && delx < 0 && dely > Math.PI / 2 && dely < Math.PI)
{
rx = -sdp.X + (int)((sdp.X + R * Math.Sin(delx + dr)));
ry = -sdp.Y + (int)(sdp.Y + R * Math.Cos(dely + dr));
}
if (delx > 0 && delx < Math.PI / 2 && dely > 0 && dely < Math.PI / 2)
{
rx = -sdp.X + (int)((sdp.X + R * Math.Sin(delx - dr)));
ry = -sdp.Y + (int)(sdp.Y + R * Math.Cos(dely - dr));
}
if (delx > -Math.PI /
2 && delx < 0 && dely < Math.PI / 2 && dely > 0)
{
rx = -sdp.X + (int)((sdp.X + R * Math.Sin(delx - dr)));
ry = -sdp.Y + (int)(sdp.Y + R * Math.Cos(dely + dr));
}
if (delx > 0 && delx < Math.PI / 2 && dely < Math.PI && dely > Math.PI / 2)
{
rx = -sdp.X + (int)((sdp.X + R * Math.Sin(delx + dr)));
ry = -sdp.Y + (int)(sdp.Y + R * Math.Cos(dely - dr));
}*/
if (delx >= -Math.PI / 2 && delx <= 0 && dely >= Math.PI / 2 && dely <= Math.PI)
{
rx = -sdp.X + (int)((sdp.X + R * Math.Sin(delx + dr)));
ry = -sdp.Y + (int)(sdp.Y + R * Math.Cos(dely + dr));
}
if (delx >= 0 && delx <= Math.PI / 2 && dely >= 0 && dely <= Math.PI / 2)
{
rx = -sdp.X + (int)((sdp.X + R * Math.Sin(delx - dr)));
ry = -sdp.Y + (int)(sdp.Y + R * Math.Cos(dely - dr));
}
if (delx >= -Math.PI / 2 && delx <= 0 && dely <= Math.PI / 2 && dely >= 0)
{
rx = -sdp.X + (int)((sdp.X + R * Math.Sin(delx - dr)));
ry = -sdp.Y + (int)(sdp.Y + R * Math.Cos(dely + dr));
}
if (delx >= 0 && delx <= Math.PI / 2 && dely <= Math.PI && dely >= Math.PI / 2)
{
rx = -sdp.X + (int)((sdp.X + R * Math.Sin(delx + dr)));
ry = -sdp.Y + (int)(sdp.Y + R * Math.Cos(dely - dr));
}
cord[i].X = dp.X + rx;
cord[i].Y = dp.Y + ry;
}
// Масштабирование
if (this.zoom.Text == String.Empty) this.zoom.Text = "0";
temp = new Point[q];
g = Graphics.FromImage(b);
for (i = 0; i < q; i++)
{
temp[i].X = (int)((1.0 - dz) * (float)(dp.X - cord[i].X));
temp[i].Y = (int)((1.0 - dz) * (float)(dp.Y - cord[i].Y));
}
b = new Bitmap(this.work_panel.Width, this.work_panel.Height);
g = Graphics.FromImage(b);
for (i = 0; i < q - 1; i++)
{
g.DrawLine(pen, cord[i].X + temp[i].X, cord[i].Y + temp[i].Y, cord[i + 1].X + temp[i + 1].X, cord[i + 1].Y + temp[i + 1].Y);
}
g.DrawLine(pen, cord[q - 1].X + temp[q - 1].X, cord[q - 1].Y + temp[q - 1].Y, cord[0].X + temp[0].X, cord[0].Y + temp[0].Y);
this.work_panel.Image = b;
}
}
}
void rotation_MouseWheel(object sender, MouseEventArgs e)
{
//throw new NotImplementedException();
if (this.rotation.Text.Length != 0)
{
double der = double.Parse(this.rotation.Text);
if (e.Delta > 0)
{
if (Shift && !Ctrl)
{
der++;
}
else if (Ctrl && !Shift)
{
der += 0.01;
}
else if (Shift && Ctrl)
{
der += 10;
}
else
{
der += 0.1;
}
}
if (e.Delta < 0)
{
if (Shift && !Ctrl)
{
der--;
}
else if (Ctrl && !Shift)
{
der -= 0.01;
}
else if (Shift && Ctrl)
{
der -= 10;
}
else
{
der -= 0.1;
}
}
this.rotation.Text = (der).ToString();
}
}
private void zoom_TextChanged(object sender, EventArgs e)
{
if (q > 0)
{
if (this.zoom.Text == String.Empty) this.zoom.Text = "0";
temp = new Point[q];
if (dz != (float)(float.Parse(this.zoom.Text)) / 100)
{
dz = (float)(float.Parse(this.zoom.Text)) / 100;
g = Graphics.FromImage(b);
for (i = 0; i < q; i++)
{
temp[i].X = (int)((1.0 - dz) * (float)(dp.X - cord[i].X));
temp[i].Y = (int)((1.0 - dz) * (float)(dp.Y - cord[i].Y));
}
b = new Bitmap(this.work_panel.Width, this.work_panel.Height);
g = Graphics.FromImage(b);
for (i = 0; i < q - 1; i++)
{
g.DrawLine(pen, cord[i].X + temp[i].X, cord[i].Y + temp[i].Y, cord[i + 1].X + temp[i + 1].X, cord[i + 1].Y + temp[i + 1].Y);
}
g.DrawLine(pen, cord[q - 1].X + temp[q - 1].X, cord[q - 1].Y + temp[q - 1].Y, cord[0].X + temp[0].X, cord[0].Y + temp[0].Y);
this.work_panel.Image = b;
}
}
}
void zoom_MouseWheel(object sender, MouseEventArgs e)
{
//throw new NotImplementedException();
int dez;
if (this.zoom.Text.Length != 0)
{
dez = int.Parse(this.zoom.Text);
if (e.Delta > 0)
{
if (Shift)
{
dez += 10;
}
else
{
dez++;
}
}
if (e.Delta < 0)
{
if (Shift)
{
dez -= 10;
}
else
{
dez--;
}
}
this.zoom.Text = (dez).ToString();
}
}
private void zoom_KeyDown(object sender, KeyEventArgs e)
{
if (e.Shift && Shift == false)
{
Shift = true;
}
}
private void zoom_KeyUp(object sender, KeyEventArgs e)
{
if (Shift == true)
{
Shift = false;
}
}
private void position_x_KeyDown(object sender, KeyEventArgs e)
{
if (e.Shift && Shift == false)
{
Shift = true;
}
}
private void position_x_KeyUp(object sender, KeyEventArgs e)
{
if (Shift == true)
{
Shift = false;
}
}
private void position_y_KeyDown(object sender, KeyEventArgs e)
{
if (e.Shift && Shift == false)
{
Shift = true;
}
}
private void position_y_KeyUp(object sender, KeyEventArgs e)
{
if (Shift == true)
{
Shift = false;
}
}
private void rotation_KeyDown(object sender, KeyEventArgs e)
{
if (e.Shift && Shift == false)
{
Shift = true;
}
if (e.Control && Ctrl == false)
{
Ctrl = true;
}
}
private void rotation_KeyUp(object sender, KeyEventArgs e)
{
if (Shift == true)
{
Shift = false;
}
if (Ctrl == true)
{
Ctrl = false;
}
}
private void work_panel_Click(object sender, EventArgs e)
{
}
}
} |