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
| using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace CGcontr
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
Graphics g; //Объект поверхности рисования
int a = 40, b = 60; //Размеры ромба
int dx = 33, by = 6; //Смещения для переноса
int tdx, tdy;
Point[] romb = new Point[6]; //вершины фигуры
MouseEventArgs _e; // координты последнего вывода
public Form1()
{
//инициализаця компонентов экранной формы
InitializeComponent();
//получение ссылки на объект поверхности рисования
g = pictureBox1.CreateGraphics();
//формируем точки полигонов - вершины ромба
romb[0] = new Point (-a/2,0);//левая вершина
romb[1] = new Point (a / 2, 0);//правая
//нижняя
romb[2] = new Point (0, Convert.ToInt32(0.5 * a * Math.Sqrt(3)));
romb[3] = new Point (-a / 2, 0);//левая
//верхняя
romb[4] = new Point (0, -Convert.ToInt32(0.5 * a * Math.Sqrt(3)));
romb[5] = new Point(a / 2, 0);//правая
}
private void DrawRomb(int x, int y, int dx, int dy)
{
GraphicsPath gp = new GraphicsPath();
Matrix tr1 = new Matrix(); //матрица для переноса в поицию мыши
Matrix tr2 = new Matrix(); //матрица для переноса согласно смещению
//добавляем ромб в путь
gp.AddPolygon(romb);
//создаем матрицу для перенос ромба в позицию курсора
tr1.Translate(x, y);
//создаем матрицн для переноса ромба согласно преобразованию
tr2.Translate(dx, dy);
//перенос фигуры в позицию курсора мыши
gp.Transform(tr1);
//перенос фигуры в при преобразовании
gp.Transform(tr2);
//прорисовка фигуры
g.DrawPath(Pens.Red, gp);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
//pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.SystemColors.Window;
this.pictureBox1.Location = new System.Drawing.Point(0, 32);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(360, 288);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
//
//button1
//
this.button1.Location = new System.Drawing.Point (0,0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104,32);
this.button1.TabIndex = 1;
this.button1.Text = "Преобразование";
this.button1.Click += new
System.EventHandler (this.button1_Click);
//
//Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5,13);
this.ClientSize = new System.Drawing.Size(360,317);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Test = "Контрольная по КГ";
this.ResumeLayout (false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
tdx = 0; tdy = 0; //текущие параметры преобразования
DrawRomb(e.X, e.Y, tdx, tdy); //отрисовака ромба
_e = e; ;//сохранение координат курсора мыши
}
private void button1_Click(object sender, System.EventArgs e)
{
// наращиваем текущие параметры преобразования на dx,dy
tdx += dx;
tdy += dy;
DrawRomb(_e.X, _e.Y, tdx, tdy); //отрисовка ромба
}
}
} |