-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscene.cpp
195 lines (174 loc) · 5.01 KB
/
scene.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
#include "scene.h"
const double PI = 3.1415926536;
PointList createPoints(Point begin, Point end, int num);
PointList shiftPoints(PointList points, Point begin, Point end);
double distance(Point A, Point B)
{
int ux = A.getX()-B.getX();
int uy = A.getY()-B.getY();
double un = ux*ux+uy*uy;
return sqrt(un);
}
Scene::Scene(QObject *parent):QGraphicsScene(parent)
{
numOfPoints=40;
mouseInteraction=true;
}
Scene::~Scene()
{
points.clear();
}
void Scene::draw(PointList points, QPen pen)
{
PointList::iterator i;
for(i=points.begin();i!=points.end();i++)
{
//addLine(i->getX(),i->getY(),i->getX(),i->getY(),pen);
addRect(i->getX(),i->getY(),1,1,pen);
}
}
void Scene::drawLine(PointList points)
{
QPen pen(QBrush(Qt::red),3);
PointList::iterator i;
for(i=points.begin();i!=points.end();i++)
{
addRect(i->getX()-2,i->getY()-2,4,4,pen);//addEllipse(i->getX()-2,i->getY()-2,4,4,pen);
if(i==prev(points.end()))
{
this->addLine(i->getX(),i->getY(),points.begin()->getX(),points.begin()->getY(),pen);
}
else
{
this->addLine(next(i)->getX(),next(i)->getY(),i->getX(),i->getY(),pen);
}
}
}
void Scene::drawLineWithSP(PointList points)
{
QPen pen(QBrush(Qt::red),3);
PointList::iterator i;
for(i=points.begin();i!=points.end();i++)
{
if(i==points.begin())
{
addRect(i->getX()-4,i->getY()-4,8,8,pen); //addEllipse(i->getX()-4,i->getY()-4,8,8,pen);
}
else
{
addRect(i->getX()-2,i->getY()-2,4,4,pen);//addEllipse(i->getX()-2,i->getY()-2,4,4,pen);
this->addLine(prev(i)->getX(),prev(i)->getY(),i->getX(),i->getY(),pen);
}
}
}
//void Scene::drawLine(PointList points, bool displayIndex)
//{
// QPen pen1(QBrush(Qt::red),3);
// QPen pen2(QBrush(Qt::blue),3);
// QPen pen;
// PointList::iterator i;
// for(i=points.begin();i!=points.end();i++)
// {
// if(i->isOriginal()) pen=pen1;
// else pen=pen2;
// if(i==points.begin())
// {
// addRect(i->getX()-4,i->getY()-4,8,8,pen); //addEllipse(i->getX()-4,i->getY()-4,8,8,pen);
// }
// else
// {
// addRect(i->getX()-2,i->getY()-2,4,4,pen);//addEllipse(i->getX()-2,i->getY()-2,4,4,pen);
// this->addLine(boost::prior(i)->getX(),boost::prior(i)->getY(),i->getX(),i->getY(),pen);
// }
// if(displayIndex)
// {
// if(i->getIndex() > 0)
// {
// QGraphicsTextItem *text=addText(QString::number(i->getIndex()));
// text->setPos(i->getX(),i->getY());
// }
// }
//
// }
//}
void Scene::drawLine(PointList points,QColor color)
{
QPen pen(QBrush(color),3);
PointList::iterator i;
for(i=points.begin();i!=points.end();i++)
{
if(i==points.begin()) addEllipse(i->getX()-4,i->getY()-4,8,8,pen);
else
{
addEllipse(i->getX()-2,i->getY()-2,4,4,pen);
this->addLine(prev(i)->getX(),prev(i)->getY(),i->getX(),i->getY(),pen);
}
}
}
void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
{
if(mouseInteraction)
{
last = Point(e->scenePos().x(),e->scenePos().y());
if(e->buttons() & Qt::LeftButton)
{
//this->addLine(this->first.getX(),this->first.getY(),e->scenePos().x(),e->scenePos().y(),pen);
points.clear();
points=createPoints(this->first,last,this->numOfPoints);
}
if(e->buttons() & Qt::RightButton)
{
PointList s=shiftPoints(points,first,last);
if((int)s.size()>0)
{
points.clear();
points=s;
}
first=last;
}
this->clear();
QPen pen(QBrush(Qt::red),3);
draw(points,pen);
}
}
void Scene::mousePressEvent(QGraphicsSceneMouseEvent* e)
{
first.setX(e->scenePos().x());
first.setY(e->scenePos().y());
//if(e->button()==Qt::LeftButton) std::cout<<first.getX()<<"\t"<<first.getX()<<std::endl;
//if(e->button()==Qt::LeftButton) addEllipse(e->scenePos().x()-5,e->scenePos().y()-5,10,10,pen);
}
void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent* e)
{
last.setX(e->pos().x());
last.setY(e->pos().y());
//std::cout<<last.x()<<"\t"<<last.y()<<std::endl;
}
PointList createPoints(Point begin, Point end, int num)
{
double angle=2*PI/num;
int radius=distance(begin,end);
Point p;
Point center((end.getX()+begin.getX())/2,(end.getY()+begin.getY())/2);
PointList s;
for(int i=0;i<num;i++)
{
p.setLocation(radius*cos(angle*i)+center.getX(),radius*sin(angle*i)+center.getY());
s.push_back(p);
}
return s;
}
PointList shiftPoints(PointList points, Point begin, Point end)
{
PointList s;
PointList::iterator i;
int dx=end.getX()-begin.getX();
int dy=end.getY()-begin.getY();
Point p;
for(i=points.begin();i!=points.end();i++)
{
p.setLocation(i->getX()+dx,i->getY()+dy);
s.push_back(p);
}
return s;
}