-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyWiget.cpp
316 lines (281 loc) · 8.6 KB
/
MyWiget.cpp
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
#include "MyWiget.h"
#include<qpushbutton.h>
#include<QFileDialog>
#include<QPainter>
#include<QMessageBox>
#include "../Painter/obj.cpp"
////从dll中调用
//void LoadFromFile(ifstream& input, vector<Line>& lines, vector<Ray>& rays,
// vector<Segment>& segments, vector<Circle>& circles) {};
//
////从dll中调用
//void Calculate_Points(set<Point>& points, vector<Line>& lines, vector<Ray>& rays,
// vector<Segment>& segments, vector<Circle>& circles) {};
//从容器中删除几何对象
void MyWiget::delete_obj(QString& str) {
string s = str.toStdString();
if (s[0] == 'L') {
vector<Line>::iterator iter = lines.begin();
for (; iter != lines.end(); iter++)
if (s == iter->display()) {
lines.erase(iter); break;
}
}
else if (s[0] == 'R') {
vector<Ray>::iterator iter = rays.begin();
for (; iter != rays.end(); iter++)
if (s == iter->display()) {
rays.erase(iter); break;
}
}
else if (s[0] == 'S') {
vector<Segment>::iterator iter = segments.begin();
for (; iter != segments.end(); iter++)
if (s == iter->display()) {
segments.erase(iter); break;
}
}
else if (s[0] == 'C') {
vector<Circle>::iterator iter = circles.begin();
for (; iter != circles.end(); iter++)
if (s == iter->display()) {
circles.erase(iter); break;
}
}
}
//UI内的各种点击事件
MyWiget::MyWiget(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
setWindowTitle("Painter");
//
//从文件导入几何对象
connect(ui.btn_loadfile, &QPushButton::clicked, [=]() {
QString cur_path = QCoreApplication::applicationDirPath();//当前文件夹路径
QString path = QFileDialog::getOpenFileName(this, "open file", cur_path);
string file_name = path.toStdString();
//取消从文件导入
if (file_name == "") return;
//调用计算模块读取文件的函数LoadFromFile
try {
//清空交点集和各几何对象,重新计算
points.clear();
lines.clear();
rays.clear();
segments.clear();
circles.clear();
//
ifstream fin;
fin.open(file_name);
LoadFromFile(fin, lines, rays, segments, circles);
Calculate_Points(points, lines, rays, segments, circles);
}
catch (exception& e) {
QMessageBox::critical(this,"critical",e.what());
}
//更新UI中下拉栏已有的几何对象,建立映射
for (auto& obj : lines) {
QString str = QString::fromStdString(obj.display());
ui.comboBox_delete->addItem(str);
line_map.insert(str, obj);
}
for (auto& obj : rays) {
QString str = QString::fromStdString(obj.display());
ui.comboBox_delete->addItem(str);
ray_map.insert(str, obj);
}
for (auto& obj : segments) {
QString str = QString::fromStdString(obj.display());
ui.comboBox_delete->addItem(str);
segment_map.insert(str, obj);
}
for (auto& obj : circles) {
QString str = QString::fromStdString(obj.display());
ui.comboBox_delete->addItem(str);
circle_map.insert(str, obj);
}
});
//添加几何对象,假设UI输入的几何对象与已有几何对象不重合(不查重)
connect(ui.btn_add, &QPushButton::clicked, [=]() {
//提取几何对象种类
QString str = ui.comboBox_type->currentText();
//提取四个数
int fir_num = ui.lineEdit_1->displayText().toInt();
int sec_num = ui.lineEdit_2->displayText().toInt();
int thi_num = ui.lineEdit_3->displayText().toInt();
int four_num = ui.lineEdit_4->displayText().toInt();
//检查合法性
try {
exception_if_outBorder(fir_num);
exception_if_outBorder(sec_num);
exception_if_outBorder(thi_num);
exception_if_outBorder(four_num);
if (str == QString("circle")) { exception_if_illegalRadius(thi_num); }
else { exception_if_samePoints(fir_num, sec_num, thi_num, four_num); }
}
catch (exception& e) {
QMessageBox::critical(this,"critical",e.what());
return;
}
//创建对象,并更新UI中下拉栏已有的几何对象,建立映射
if (str == QString("line")) {
Line line(fir_num, sec_num, thi_num, four_num);
lines.push_back(line);
QString display_str = QString::fromStdString(line.display());
ui.comboBox_delete->addItem(display_str);
line_map.insert(display_str, line);
try {
Calculate_Points(points, lines, rays, segments, circles);
}
catch (exception& e) {
QMessageBox::critical(this, "critical", e.what());
//删除已插入的对象和其在下拉栏中的显示
lines.pop_back();
ui.comboBox_delete->removeItem(ui.comboBox_delete->findText(display_str));
}
}
else if (str == QString("ray")) {
Ray ray(fir_num, sec_num, thi_num, four_num);
rays.push_back(ray);
QString display_str = QString::fromStdString(ray.display());
ui.comboBox_delete->addItem(display_str);
ray_map.insert(display_str, ray);
try {
Calculate_Points(points, lines, rays, segments, circles);
}
catch (exception& e) {
QMessageBox::critical(this, "critical", e.what());
rays.pop_back();
ui.comboBox_delete->removeItem(ui.comboBox_delete->findText(display_str));
}
}
else if (str == QString("segment")) {
Segment seg(fir_num, sec_num, thi_num, four_num);
segments.push_back(seg);
QString display_str = QString::fromStdString(seg.display());
ui.comboBox_delete->addItem(display_str);
segment_map.insert(display_str, seg);
try {
Calculate_Points(points, lines, rays, segments, circles);
}
catch (exception& e) {
QMessageBox::critical(this, "critical", e.what());
segments.pop_back();
ui.comboBox_delete->removeItem(ui.comboBox_delete->findText(display_str));
}
}
else if (str == QString("circle")) {
Circle circle(fir_num, sec_num, thi_num);
circles.push_back(circle);
QString display_str = QString::fromStdString(circle.display());
ui.comboBox_delete->addItem(display_str);
circle_map.insert(display_str, circle);
try {
Calculate_Points(points, lines, rays, segments, circles);
}
catch (exception& e) {
QMessageBox::critical(this, "critical", e.what());
circles.pop_back();
ui.comboBox_delete->removeItem(ui.comboBox_delete->findText(display_str));
}
}
});
//删除几何对象
connect(ui.btn_delete, &QPushButton::clicked, [=]() {
//更新本地几何对象集合
QString cur_str = ui.comboBox_delete->currentText();
this->delete_obj(cur_str);//删除容器中的几何对象
//删除映射
int cur_index = ui.comboBox_delete->currentIndex();
ui.comboBox_delete->removeItem(cur_index);
//重置points
points.clear();
//调用计算模块,更新points
try {
Calculate_Points(points, lines, rays, segments, circles);
}
catch (exception& e) {
QMessageBox::critical(this, "critical", e.what());
}
});
connect(ui.btn_paint, &QPushButton::clicked, [=]() {
update();
//更新UI显示的交点数
QString str;
str.setNum(points.size());
ui.lineEdit_ans->setText(str);
});
//交点数初始化为0
ui.lineEdit_ans->setText("0");
}
////画图函数
void MyWiget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
//painter.drawLine(QLineF(QPoint(-400, 280), QPoint(100, 280)));
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(500, 500);//更改坐标原点
//定义画布区域 y>=280,O坐标(500,500)
//绘制坐标轴
painter.drawLine(QLineF(QPoint(-500, 0), QPoint(500, 0)));
painter.drawLine(QLineF(QPoint(0, 500), QPoint(0, -220)));
//比例尺20:1
//绘制几何对象
QPen pen((QColor)Qt::darkCyan);
pen.setWidth(3);
painter.setPen(pen);
for (auto& line : lines) {
double A = line.A, B = line.B, C = line.C;
double x1, y1 = 11, x2, y2 = -15;
if (A == 0) {
y1 = y2 = -C / B;
}
else {
x1 = (-C - B * y1) / A;
x2 = (-C - B * y2) / A;
}
painter.drawLine(QPoint(x1 * 20, -y1 * 20), QPoint(x2 * 20, -y2 * 20));
//y=11;x1
//y=-15,x2;
}
QPen pen1((QColor)Qt::darkBlue);
pen1.setWidth(3);
painter.setPen(pen1);
for (auto& ray : rays) {
double A = ray.A, B = ray.B, C = ray.C;
double x1, y1 = 11, x2, y2 = -15;
double sta_x = ray.start_x, sta_y = ray.start_y;
if (A == 0) {
y1 = y2 = -C / B;
}
else {
x1 = (-C - B * y1) / A;
x2 = (-C - B * y2) / A;
}
if (ray.islawful(Point(x1, y1))) painter.drawLine(QPoint(sta_x * 20, -sta_y * 20), QPoint(x1 * 20, -y1 * 20));
else painter.drawLine(QPoint(sta_x * 20, -sta_y * 20), QPoint(x2 * 20, -y2 * 20));
}
QPen pen2((QColor)Qt::darkYellow);
pen2.setWidth(3);
painter.setPen(pen2);
for (auto& seg : segments) {
double A = seg.A, B = seg.B, C = seg.C;
painter.drawLine(QPoint(seg.x1 * 20, -seg.y1 * 20), QPoint(seg.x2 * 20, -seg.y2 * 20));
}
QPen pen3((QColor)Qt::black);
pen3.setWidth(3);
painter.setPen(pen3);
for (auto& cir : circles) {
painter.drawEllipse(QPoint(cir.cx * 20, -cir.cy * 20), int(cir.cr * 20), int(cir.cr * 20));
}
//绘制交点
/*painter.drawLine(QPoint(800, 800), QPoint(900, 900));
painter.drawEllipse(QPoint(800, 800), 50, 50);*/
QPen pen4((QColor)Qt::red);
pen4.setWidth(5);
painter.setPen(pen4);
for (auto& point : points) {
QPoint p(point.x * 20, -point.y * 20);
painter.drawPoint(p);
}
}