forked from msis/mSimpleDrawer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsimpledrawer.cpp
211 lines (183 loc) · 5.6 KB
/
msimpledrawer.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
#include "msimpledrawer.h"
#include <QDebug>
mSimpleDrawer::mSimpleDrawer(double xmin, double xmax,
double ymin, double ymax,
QWidget *parent) :
QWidget(parent),
xMax(xmax), xMin(xmin),
yMax(ymax), yMin(ymin)
{
label = new QLabel(this);
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
label->resize(size());
pixMap = new QPixmap(this->size());
Clean();
}
mSimpleDrawer::mSimpleDrawer(QWidget *parent):
QWidget(parent),
xMax(10), xMin(-10),
yMax(10), yMin(-10)
{
label = new QLabel(this);
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
label->resize(size());
pixMap = new QPixmap(this->size());
Clean();
}
mSimpleDrawer::~mSimpleDrawer()
{
delete pixMap;
delete label;
}
void mSimpleDrawer::paintEvent(QPaintEvent *)
{
label->setPixmap(*pixMap);
}
void mSimpleDrawer::resizeEvent(QResizeEvent *)
{
label->resize(size());
QSize s = label->size();
QPixmap tmp((pixMap->scaled(s,Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
pixMap->swap(tmp);
}
double mSimpleDrawer::xToPix(double x)
{
double echx = pixMap->width()/(xMax-xMin);
return (x-xMin)*echx;
}
double mSimpleDrawer::yToPix(double y)
{
double echy = pixMap->height()/(yMax - yMin);
return pixMap->height() - (y-yMin)*echy;
}
void mSimpleDrawer::Clean()
{
QPainter p(pixMap) ;
QRect dim = p.window() ;
p.setPen(QPen(Qt::red, 4)) ;
p.setBrush(QBrush(Qt::white)) ;
p.drawRect(dim) ;
}
void mSimpleDrawer::Center(double x, double y)
{
double Rx=(xMax-xMin)/2.0;
double Ry=(yMax-yMin)/2.0;
xMin=x-Rx; xMax=x+Rx;
yMin=y-Ry; yMax=y+Ry;
}
void mSimpleDrawer::DrawArrow(double x, double y,
double dx, double dy,
double r, QPen pen)
{
double x2=x+dx;
double y2=y+dy;
double a=3.14-1.0;
double px=x2+r*(cos(a)*dx-sin(a)*dy); // cot de pointe
double py=y2+r*(sin(a)*dx+cos(a)*dy);
double qx=x2+r*(cos(-a)*dx-sin(-a)*dy); // autre cot de pointe
double qy=y2+r*(sin(-a)*dx+cos(-a)*dy);
QPainter p(pixMap);
p.setPen(pen);
p.drawLine(xToPix(x),yToPix(y),xToPix(x2),yToPix(y2));
p.drawLine(xToPix(x2),yToPix(y2),xToPix(px),yToPix(py));
p.drawLine(xToPix(x2),yToPix(y2),xToPix(qx),yToPix(qy));
}
void mSimpleDrawer::DrawBox(double xmin, double xmax,
double ymin, double ymax,
QPen pen, QBrush brush)
{
QPolygonF box;
box << QPointF(xToPix(xmin),yToPix(ymin))
<< QPointF(xToPix(xmax),yToPix(ymin))
<< QPointF(xToPix(xmax),yToPix(ymax))
<< QPointF(xToPix(xmin),yToPix(ymax));
QPainter p(pixMap);
p.setPen(pen);
p.setBrush(brush);
p.drawPolygon(box);
}
void mSimpleDrawer::DrawEllipse(double cx, double cy, double r,
QPen pen, QBrush brush)
{
QPainter p(pixMap);
p.setPen(pen);
p.setBrush(brush);
p.drawEllipse(xToPix(cx-r),yToPix(cy+r),
xToPix(cx+r)-xToPix(cx-r),yToPix(cy-r)-yToPix(cy+r));
}
void mSimpleDrawer::DrawLine(double x1, double y1,
double x2, double y2,
QPen pen)
{
QPainter p(pixMap);
p.setPen(pen);
p.drawLine(xToPix(x1),yToPix(y1),xToPix(x2),yToPix(y2));
}
void mSimpleDrawer::DrawPolygon(double x, double y,
double theta,
vector<double> X, vector<double> Y,
QPen pen, QBrush brush)
{
QPolygonF poly;
for (int k=0;k<X.size();k++)
{
double x1=x+cos(theta)*X[k]-sin(theta)*Y[k];
double y1=y+sin(theta)*X[k]+cos(theta)*Y[k];
poly << QPointF(xToPix(x1),yToPix(y1));
}
QPainter p(pixMap);
p.setPen(pen);
p.setBrush(brush);
p.drawPolygon(poly);
}
void mSimpleDrawer::DrawRobot(double x, double y, double theta)
{
vector<double> X,Y;
X.push_back(0); Y.push_back(-1);
X.push_back(3); Y.push_back(0);
X.push_back(0); Y.push_back(1);
DrawPolygon(x,y,theta,X,Y, QPen(Qt::blue), QBrush(Qt::black));
}
void mSimpleDrawer::DrawSailboat(double x, double y,
double theta, QColor colr,
double deltag, QColor colg,
double deltav, QColor colv)
{
double a=0.8;
vector<double> Xc,Yc; //coque
Xc.push_back(-a); Yc.push_back(-a);
Xc.push_back(4*a); Yc.push_back(0);
Xc.push_back(-a); Yc.push_back(a);
Xc.push_back(-a); Yc.push_back(-a);
DrawPolygon(x,y,theta,Xc,Yc,colr, colr);
vector<double> Xr,Yr; //gouvernail
Xr.push_back(-2*a); Yr.push_back(0);
Xr.push_back(0); Yr.push_back(0);
DrawPolygon(x-a*cos(theta),y-a*sin(theta),
theta+deltag,Xr,Yr,QPen(colg),QBrush(colg));
vector<double> Xv,Yv; //voile
Xv.push_back(-5*a); Yv.push_back(0);
Xv.push_back(0); Yv.push_back(0);
DrawPolygon(x+3*a*cos(theta),y+3*a*sin(theta),
theta+deltav,Xv,Yv,colv,colv);
}
void mSimpleDrawer::DrawText(double x, double y,
QString s, QColor col)
{
QPainter p(pixMap);
p.setPen(QPen(col));
p.setFont(QFont("Arial"));
p.drawText((int)xToPix(x),(int)yToPix(y),s);
}
void mSimpleDrawer::Save(QString s)
{
pixMap->save(s+".png","PNG",100);
}
void mSimpleDrawer::setDrawingArea(double xmin, double xmax,
double ymin, double ymax)
{
xMax=xmax;
xMin=xmin;
yMax=ymax;
yMin=ymin;
}