-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
373 lines (336 loc) · 9.82 KB
/
mainwindow.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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolTip>
#include <QMouseEvent>
#include <QPainter>
#include <QtDebug>
#include <vector>
#include <QtCore/QThread>
typedef std::vector<double> vec;
typedef std::vector<vec> mat;
mat mul(mat &A, mat &B) //计算A*B
{
mat C(A.size(), vec(B[0].size())); //A矩阵的行数、B矩阵的列数
for (int i = 0; i < (int)A.size(); ++i)
for (int k = 0; k < (int)B.size(); ++k)
if (A[i][k]) //对稀疏矩阵的优化
for (int j = 0; j < (int)B[0].size(); ++j)
C[i][j] += A[i][k] * B[k][j];
return C;
}
void MainWindow::drawCurve(QPainter &painter)
{
curve.bezierNodes.clear();
for(double t=0; t<1; t+=0.001)
{
curve.recursive_bezier(painter,t);
}
//qDebug()<<"done"<<endl;
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(tr("qk"));
connect(ui->actionPolygon, &QAction::triggered, this, &MainWindow::setPolygonModel);
connect(ui->actionLine, &QAction::triggered, this, &MainWindow::setLineModel);
connect(ui->actionClear, &QAction::triggered, this, &MainWindow::Clear);
connect(ui->actionFiller,&QAction::triggered, this, &MainWindow::setFillerModel);
connect(ui->actionCutter,&QAction::triggered, this, &MainWindow::setCutModel);
connect(ui->actionCurve,&QAction::triggered, this, &MainWindow::setCurveModel);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(Translate())); //display() connect
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(Scale()));
connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(Rotate()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setPolygonModel()
{
Model = "Polygon";
Ply.init();
repaint();
}
void MainWindow::setLineModel()
{
Model = "Line";
L.init();
repaint();
}
void MainWindow::setCutModel()
{
Model = "Cut";
C.cutdone =C.cutting= false;
repaint();
}
void MainWindow::setFillerModel()
{
Model = "Fill";
F.init();
repaint();
}
void MainWindow::setCurveModel()
{
if(Model == "Curve")
{
curve.done = true;
}
else
{
Model = "Curve";
curve.init();
}
repaint();
}
void MainWindow::Clear()
{
Model = "";
repaint();
}
void MainWindow::Translate()
{
translate(Ply.V);
repaint();
}
void MainWindow::Rotate()
{
rotate(Ply.V);
repaint();
}
void MainWindow::Scale()
{
scale(Ply.V);
repaint();
}
void MainWindow::translate(QVector<QPoint> &vertices)//参照点与平移向量
{
QPoint ref;
ref.rx() = ui->textEdit->toPlainText().toInt();
ref.ry() = ui->textEdit_2->toPlainText().toInt();
int x = ui->textEdit_3->toPlainText().toInt();
int y = ui->textEdit_4->toPlainText().toInt();
mat m(3,vec(3));
m[0][0]=1;
m[0][2]=x;
m[1][1]=1;
m[1][2]=y;
m[2][2]=1;
vec point(3);
for (auto&it:vertices) //遍历每个顶点进行修改
{
point[0] = it.x();
point[1] = it.y();
point[2] = 1;
it.rx() = m[0][0] * point[0] + m[0][1] * point[1] + m[0][2] * point[2];
it.ry() = m[1][0] * point[0] + m[1][1] * point[1] + m[1][2] * point[2];
}
}
void MainWindow::rotate(QVector<QPoint> &vertices)
{
QPoint ref;
ref.rx() = ui->textEdit->toPlainText().toInt();
ref.ry() = ui->textEdit_2->toPlainText().toInt();
double angle = ui->textEdit_7->toPlainText().toDouble();
double rad = (angle / 180) * pi;
mat m(3, vec(3));
double cosa = cos(rad);
double sina = sin(rad);
m[0][0] = cosa;
m[0][1] = -sina;
m[0][2] = ref.x() * (1 - cosa) + ref.y() * sina;
m[1][0] = sina;
m[1][1] = cosa;
m[1][2] = ref.y() * (1 - cosa) - ref.x() * sina;
m[2][2] = 1;
vec point(3);
for (auto&it:vertices) //遍历每个顶点进行修改
{
point[0] = it.x();
point[1] = it.y();
point[2] = 1;
it.rx() = m[0][0] * point[0] + m[0][1] * point[1] + m[0][2] * point[2];
it.ry() = m[1][0] * point[0] + m[1][1] * point[1] + m[1][2] * point[2];
}
}
void MainWindow::scale(QVector<QPoint> &vertices)//参照点与缩放倍数
{
QPoint ref;
ref.rx() = ui->textEdit->toPlainText().toInt();
ref.ry() = ui->textEdit_2->toPlainText().toInt();
double sizex = ui->textEdit_5->toPlainText().toDouble();
double sizey = ui->textEdit_6->toPlainText().toDouble();
//qDebug()<<sizex<<' '<<sizey<<endl;
mat m(3,vec(3));
m[0][0]=sizex;
m[0][2]=ref.x()*(1-sizex);
m[1][1]=sizey;
m[1][2]=ref.y()*(1-sizey);
m[2][2]=1;
vec point(3);
for (auto&it:vertices) //遍历每个顶点进行修改
{
point[0] = it.x();
point[1] = it.y();
point[2] = 1;
it.rx() = m[0][0] * point[0] + m[0][1] * point[1] + m[0][2] * point[2];
it.ry() = m[1][0] * point[0] + m[1][1] * point[1] + m[1][2] * point[2];
}
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) //左键
{
if(Model == "Polygon")
Ply.mousePressEvent(event);
else if(Model == "Line")
L.mousePressEvent(event);
else if(Model == "Cut")
cutStart = event->pos(),C.cutting=true;
else if(Model == "Fill")
F.seed=true,F.start=event->pos();
else if(Model == "Curve")
curve.mousePressEvent(event);
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{ //显示鼠标坐标 跟随鼠标用方框的形式
int x0 = event->pos().x(), y0 = event->pos().y();
QString coordinates = QString("%1 %2").arg(x0).arg(y0);
QToolTip::showText(QPoint(event->globalPos()), coordinates, this);
if(Model == "Polygon")
Ply.mouseMoveEvent(event),isVertex=Ply.isVertex;
else if(Model == "Line")
L.mouseMoveEvent(event),isVertex=L.isVertex;
else if(Model == "Cut")
cutEnd = event->pos();
else if(Model == "Curve")
curve.mouseMoveEvent(event);
if (isVertex)
setCursor(Qt::CrossCursor); //改变鼠标的样式
else
setCursor(Qt::ArrowCursor);
repaint(); //刷新
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if(Model == "Polygon")
Ply.mouseReleaseEvent(event);
else if(Model == "Line")
L.mouseReleaseEvent(event);
else if(Model == "Cut")
C.cutdone = true,C.cutting=false;
else if(Model == "Curve")
curve.mouseReleaseEvent(event);
setCursor(Qt::ArrowCursor);
repaint();
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
Q_UNUSED(event);
}
void MainWindow::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
//qDebug()<<Model<<endl;
QPainter painter(this);
QPen pen; //设置点的样式
pen.setColor(Qt::red); //改变颜色
pen.setWidth(5); //改变点的宽度
painter.setPen(pen);
if(Model == "Polygon" || Model == "Fill")
Ply.drawPoints(painter);
else if(Model == "Line")
L.drawPoints(painter);
QPen pen1; //设置线的样式
pen1.setWidth(2);
pen1.setColor(Qt::blue);
painter.setPen(pen1);
if(Model == "Polygon")
Ply.drawPolygon(painter);
else if(Model == "Line")
L.drawLine(painter);
else if(Model == "Fill")
{
F.V = Ply.V;
F.V1 = Ply.V1;
for(auto &it:F.V)
qDebug()<<it.x()<<' '<<it.y()<<endl;
F.drawLine(painter);
if(test)
{
F.polygonScan();
F.fill(painter);
}
else
{
if(F.seed)
{
qDebug()<<F.start.x()<<' '<<F.start.y()<<endl;
F.seedfill4(F.start.x(),F.start.y());
F.fill(painter);
}
}
//Model = "Polygon";
}
else if(Model == "Cut")
{
qDebug()<<C.cutdone<<endl;
if(!C.cutdone)
{
//L.drawLine(painter);
L.drawLine(painter);
if(C.cutting)
{
QPen pen2; //设置线的样式
pen2.setWidth(1);
pen2.setColor(Qt::blue);
pen2.setStyle(Qt::DashLine);
painter.setPen(pen2);
painter.drawRect(cutStart.x(), cutStart.y(), cutEnd.x() - cutStart.x(), cutEnd.y() - cutStart.y());
}
}
if(C.cutdone)
{
C.XL=cutStart.x(),C.XR=cutEnd.x();
C.YB=cutStart.y(),C.YT=cutEnd.y();
bool flag=false;
if(test)
flag = C.CohenSutherland_LineClip(L.V[0].x(),L.V[0].y(),L.V[1].x(),L.V[1].y());
else
flag = C.MidClip(L.V[0].x(),L.V[0].y(),L.V[1].x(),L.V[1].y());
if(flag)
{
L.V[0]=C.P0,L.V[1]=C.P1;
//qDebug()<<C.P0.x() << C.P0.y() <<C.P1.x() << C.P1.y()<<endl;
painter.setPen(pen);
L.drawPoints(painter);
Model = "Line";
painter.setPen(pen1);
L.drawLine(painter);
}
}
}
else if(Model == "Curve")
{
for(auto&it:curve.ctrlPoints)
curve.drawPoint(painter, it, Qt::blue); //顶点,蓝色
QPen debugPen;
debugPen.setWidth(1);
debugPen.setColor(Qt::black); //颜色
painter.setPen(debugPen);
for(int i=0; i< (int)curve.ctrlPoints.size()-1; ++i)
painter.drawLine(curve.ctrlPoints[i], curve.ctrlPoints[i+1]);
if(curve.done)
{
drawCurve(painter);
for(int i=0; i<(int)curve.bezierNodes.size()-1;++i)
//painter.drawPoint(curve.bezierNodes[i]);
painter.drawLine(curve.bezierNodes[i], curve.bezierNodes[i+1]);
}
}
}