-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCycleController.cpp
More file actions
346 lines (313 loc) · 11.1 KB
/
CycleController.cpp
File metadata and controls
346 lines (313 loc) · 11.1 KB
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
#include "CycleController.h"
CycleController::CycleController()
{
this->cycle = NULL;
this->painter = NULL;
this->setCycle = CYCLE_NULL;
}
void CycleController::clearState()
{
this->cycle = NULL;
this->painter = NULL;
this->setCycle = CYCLE_NULL;
}
void CycleController::getStartAndEnd(QPoint &start, QPoint &end)
{
start = cycle->startPoint.getQPoint(); //将圆信息存储下来
end = cycle->endPoint.getQPoint(); //将最终绘制的圆信息存储下来
}
void CycleController::setBigger(QPainter* painter, QMouseEvent *e, QPen pen)
{
qDebug()<<"Before Bigger,start.x is"<< this->cycle->startPoint.getX() <<endl;
this->cycle->setStartPoint(this->cycle->startPoint*ZOOM_IN);
this->cycle->setEndPoint(this->cycle->endPoint*ZOOM_IN);
qDebug()<<"Line bigger"<<endl;
qDebug()<<"After Bigger,start.x is"<< this->cycle->startPoint.getX() <<endl;
MyDrawCycleMidpoint(painter,cycle->startPoint.point,cycle->endPoint.point);
drawHandle(painter,pen);
}
void CycleController::setSmaller(QPainter* painter, QMouseEvent *e, QPen pen)
{
qDebug()<<"Before Bigger,start.x is"<< this->cycle->startPoint.getX() <<endl;
this->cycle->setStartPoint(this->cycle->startPoint*ZOOM_OUT);
this->cycle->setEndPoint(this->cycle->endPoint*ZOOM_OUT);
qDebug()<<"Line bigger"<<endl;
qDebug()<<"After Bigger,start.x is"<< this->cycle->startPoint.getX() <<endl;
MyDrawCycleMidpoint(painter,cycle->startPoint.point,cycle->endPoint.point);
drawHandle(painter,pen);
}
bool CycleController::isOperationing(QMouseEvent *e, QPoint &start, QPoint &end)
{
if(e->button()==Qt::LeftButton){
if(cycle->startPoint.distanceToPoint(e->pos())<=5)
{
qDebug()<<"CYCLE_CENTER"<<endl;
setCycle = CYCLE_CENTER;
return true;
}
else if(cycle->endPoint.distanceToPoint(e->pos())<=5)
{
qDebug()<<"CYCLE_OUT"<<endl;
setCycle = CYCLE_OUT;
return true;
}
else if(cycle->rotatePoint.distanceToPoint(e->pos())<=5)
{
qDebug()<<"CYCLE_OUT"<<endl;
setCycle = CYCLE_HANDLE;
return true;
}
}
setCycle=CYCLE_NULL;
*state = UNDO;
start = cycle->startPoint.getQPoint(); //将圆信息存储下来
end = cycle->endPoint.getQPoint(); //将最终绘制的圆信息存储下来
Cycle* p = cycle; //防止野指针
delete p;
cycle = NULL;
return false;
}
void CycleController::mousePressEvent(QPainter *painter, QMouseEvent *e, QPen pen)
{
qDebug()<<"CycleController::mousePressEvent"<<endl;
if(cycle!=NULL){
qDebug()<<"cycle!=NULL"<<endl;
}else{
qDebug()<<"cycle==NULL"<<endl;
}
if(e->button()==Qt::LeftButton || e->button()== Qt::RightButton)
{
if(e->button()==Qt::LeftButton && cycle!=NULL)
{
if(cycle->startPoint.distanceToPoint(e->pos())<=5)
{
qDebug()<<"CYCLE_CENTER"<<endl;
setCycle = CYCLE_CENTER;
return;
}
else if(cycle->endPoint.distanceToPoint(e->pos())<=5)
{
qDebug()<<"CYCLE_OUT"<<endl;
setCycle = CYCLE_OUT;
return;
}
else if(cycle->rotatePoint.distanceToPoint(e->pos())<=5)
{
qDebug()<<"CYCLE_HANDLE_ROATATE"<<endl;
setCycle = CYCLE_HANDLE;
return;
}
setCycle=CYCLE_NULL;
*state = UNDO;
Cycle* p = cycle; //防止野指针
delete p;
cycle = NULL;
return;
}
Point curPoint(e->pos().x(),e->pos().y());
cycle = new Cycle();
cycle->setStartPoint(curPoint);
cycle->setEndPoint(curPoint);
setCycle = CYCLE_OUT;
*state = DRAWING;
}
}
void CycleController::mouseMoveEvent(QPainter* painter, QMouseEvent *e, QPen pen)
{
qDebug()<<"CycleController::mouseMoveEvent"<<endl;
this->painter = painter;
Point curPoint(e->pos().x(),e->pos().y());
if (cycle == NULL)
return;
switch(setCycle){
case CYCLE_OUT: this->setEndPoint(curPoint); break;
case CYCLE_CENTER: this->moveToPoint(curPoint); break;
case CYCLE_HANDLE: this->rotateToPoint(curPoint); break;
default:
qDebug()<<"Error setLP"<<endl;
}
}
void CycleController::mouseReleaseEvent(QPainter* painter,QMouseEvent *e, QPen pen)
{
qDebug()<<"CycleController::mouseReleaseEvent"<<endl;
qDebug()<<"state is";
switch (*state) {
case UNDO:
qDebug()<<"UNDO"<<endl;
break;
case DRAWING :
qDebug()<<"DRAWING"<<endl;
break;
}
this->painter = painter;
MyDrawCycleMidpoint(painter,cycle->startPoint.getQPoint(),cycle->endPoint.getQPoint());
drawHandle(painter,pen);
}
void CycleController::setStartPoint(Point point)
{
qDebug() << "setStartPoint("<<endl;
cycle->setStartPoint(point);
qDebug() << "StartPoint(" <<cycle->startPoint.point.x()<<","<<cycle->startPoint.point.y()<<")"<<endl;
qDebug() << "EndPoint(" <<cycle->endPoint.point.x()<<","<<cycle->endPoint.point.y()<<")"<<endl;
//顺序不知道为啥会影响粗细。。
MyDrawCycleMidpoint(painter,cycle->startPoint.getQPoint(),cycle->endPoint.getQPoint());
drawHandle(painter,pen);
}
void CycleController::setEndPoint(Point point)
{
qDebug() << "setEndPoint("<<endl;
cycle->setEndPoint(point);
qDebug() << "StartPoint(" <<cycle->startPoint.point.x()<<","<<cycle->startPoint.point.y()<<")"<<endl;
qDebug() << "EndPoint(" <<cycle->endPoint.point.x()<<","<<cycle->endPoint.point.y()<<")"<<endl;
MyDrawCycleMidpoint(painter,cycle->startPoint.getQPoint(),cycle->endPoint.getQPoint());
drawHandle(painter,pen);
}
void CycleController::moveToPoint(Point point)
{
int offsetX = point.getX() - cycle->centerPoint.getX();
int offsetY = point.getY() - cycle->centerPoint.getY();
cycle->setStartPoint(Point(cycle->startPoint.getX()+offsetX,cycle->startPoint.getY()+offsetY));
cycle->setEndPoint(Point(cycle->endPoint.getX()+offsetX,cycle->endPoint.getY()+offsetY));
qDebug() << "StartPoint(" <<cycle->startPoint.point.x()<<","<<cycle->startPoint.point.y()<<")"<<endl;
qDebug() << "EndPoint(" <<cycle->endPoint.point.x()<<","<<cycle->endPoint.point.y()<<")"<<endl;
MyDrawCycleMidpoint(painter,cycle->startPoint.getQPoint(),cycle->endPoint.getQPoint());
drawHandle(painter,pen);
}
void CycleController::rotateToPoint(Point point)
{
qDebug()<<"圆旋转起始:半径为"<<cycle->getRadius()<<endl;
double RotaryAngle = getRotaryAngle(cycle->centerPoint,cycle->rotatePoint,point);
bool wiseFlag = clockWise(cycle->centerPoint,cycle->rotatePoint,point);
if(wiseFlag)
qDebug()<<"顺时针旋转"<<endl;
else
qDebug()<<"逆时针"<<endl;
if(wiseFlag){//顺时针转
RotaryAngle *= -1;
}
int x = cycle->endPoint.getX(); //旋转点
int y = cycle->endPoint.getY(); //旋转点
int rx0 = cycle->centerPoint.getX(); //基准点
int ry0 = cycle->centerPoint.getY(); //基准点
if(cycle->centerPoint.getQPoint()!=cycle->startPoint.getQPoint()){
qDebug()<<"圆心不一"<<endl;
}
qDebug()<<"圆旋转中:半径为"<<cycle->getRadius()<<endl;
int rotateStartX = (x - rx0)*cos(RotaryAngle) + (y - ry0)*sin(RotaryAngle) + rx0 + 0.5;
int rotateStartY = -(x - rx0)*sin(RotaryAngle) + (y - ry0)*cos(RotaryAngle) + ry0 + 0.5;
qreal r = this->cycle->getRadius();
cycle->setEndPoint(getTheAccurayRotatePoint(r,rotateStartX,rotateStartY));
qDebug()<<"圆旋转后:半径为"<<cycle->getRadius()<<endl;
//有精度损失。。导致圆可能会变大变小
qDebug() << "StartPoint(" <<cycle->startPoint.point.x()<<","<<cycle->startPoint.point.y()<<")"<<endl;
qDebug() << "EndPoint(" <<cycle->endPoint.point.x()<<","<<cycle->endPoint.point.y()<<")"<<endl;
MyDrawCycleMidpoint(painter,cycle->startPoint.getQPoint(),cycle->endPoint.getQPoint());
drawHandle(painter,pen);
}
void CycleController::setState(DRAW_STATE *state)
{
this->state = state;
}
void CycleController::drawHandle(QPainter *painter, QPen pen)
{
if(!painter->isActive()) {return;}//保证在Painter有效的时候才进行
cycle->startPoint.DrawWarnPoint(painter,pen);
cycle->endPoint.DrawCyclePoint(painter,pen);
//cycle->centerPoint.DrawCyclePoint(painter,pen);
cycle->rotatePoint.DrawCyclePoint(painter,pen);
}
void CycleController::MyDrawCycleBresenham(QPainter *painter, QPoint &start, QPoint &end)//
{
if(!painter->isActive()) {return;}//保证在Painter有效的时候才进行
//首先先在这里实现我的画圆算法
qDebug()<<"MyDrawCycleBresenham "<<endl;
int x0 = start.x();
int y0 = start.y();
double R = this->getLength(start,end);
int x,y,p;
x=0;
y=R;
p=3-2*R;
for(;x<=y;x++)
{
this->drawEighthCycle(painter,x0,y0,x,y);
if(p>=0){
p+=4*(x-y)+10;
y--;
}else{
p+=4*x+6;
}
}
}
void CycleController::MyDrawCycleMidpoint(QPainter *painter, QPoint &start, QPoint &end)
{
if(!painter->isActive()) {return;}//保证在Painter有效的时候才进行
//首先先在这里实现我的画圆算法
qDebug()<<"MyDrawCycleMidpoint "<<endl;
int x0 = start.x();
int y0 = start.y();
double R = this->getLength(start,end);
int x,y;
double d;
x = 0;
y = R;
d = 1-R;
int deltaX = 3;
int deltaY = 5-2*R;
this->drawEighthCycle(painter,x0,y0,x,y);
while(x<y){
if(d<0){
d+=deltaX;
deltaX+=2;
deltaY+=2;
x++;
}
else
{
d+=deltaY;
deltaX+=2;
deltaY+=4;
x++;
y--;
}
this->drawEighthCycle(painter,x0,y0,x,y);
}
}
void CycleController::drawEighthCycle(QPainter *painter, int x0, int y0, int x, int y)
{
if(!painter->isActive()) {return;}//保证在Painter有效的时候才进行
QPoint temPt1(x0+x,y0+y);
QPoint temPt2(x0+y,y0+x);
QPoint temPt3(x0+x,y0-y);
QPoint temPt4(x0+y,y0-x);
QPoint temPt5(x0-x,y0-y);
QPoint temPt6(x0-y,y0-x);
QPoint temPt7(x0-x,y0+y);
QPoint temPt8(x0-y,y0+x);
painter->drawPoint(temPt1);
painter->drawPoint(temPt2);
painter->drawPoint(temPt3);
painter->drawPoint(temPt4);
painter->drawPoint(temPt5);
painter->drawPoint(temPt6);
painter->drawPoint(temPt7);
painter->drawPoint(temPt8);
}
Point CycleController::getTheAccurayRotatePoint(qreal ridus, int x, int y)
{
int resX = x-ROTATE_ACCURACY;
int resY = y-ROTATE_ACCURACY;
double minDiff =100;
for(int i=x-ROTATE_ACCURACY;i<=x+ROTATE_ACCURACY;i++){
for(int j=y-ROTATE_ACCURACY;j<=y+ROTATE_ACCURACY;j++){
Point tmp(i,j);
double diffTmp = fabs(this->cycle->centerPoint.distanceToPoint(tmp.getQPoint())-ridus);
if(diffTmp<minDiff){
resX=i;
resY=j;
minDiff = diffTmp;
}
}
}
return Point(resX,resY);
}