Repository navigation
Expand file tree
/
Copy pathPlot2D.cpp
More file actions
273 lines (248 loc) · 7.12 KB
/
Copy pathPlot2D.cpp
File metadata and controls
273 lines (248 loc) · 7.12 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
#include "wplot/Plot2D.h"
#include <QPainter>
namespace WPlot {
//////////////////////////////////////////////////////////////////////////////
// PUBLIC SECTION //
//////////////////////////////////////////////////////////////////////////////
Plot2D::Plot2D(QWidget *parent) :
Plot(parent),
m_upperLeftCorner(-1.0, 1.0),
m_lowerRightCorner(1.0, -1.0),
m_scaleX(1.0),
m_scaleY(1.0)
{}
/**
* Add a axes to the plot.
*
* @param[in] axes Axes that must be added.
*/
void Plot2D::addAxes(const Axes2D::Ptr &axes) {
if (axes == nullptr) {
return;
}
m_axesList.append(axes);
}
/**
* Add axes to the plot at specified index.
*
* @param[in] index Index.
* @param[in] axes Axes that must be added.
*/
void Plot2D::addAxes(const int &index, const Axes2D::Ptr &axes) {
if (axes == nullptr) {
return;
}
if (index > m_axesList.size()) {
m_axesList.append(axes);
} else if (index < 0) {
m_axesList.prepend(axes);
} else {
m_axesList.insert(index, axes);
}
}
/**
* Add an item to the plot. The item will be drawn above graphs.
*
* @param[in] item Item.
*/
void Plot2D::addItem(const Item2D::Ptr &item) {
if (item == nullptr) {
return;
}
m_itemList.append(item);
}
/**
* Add an item to the plot at specified index. The item will be
* drawn above graphs.
*
* @param[in] index Index.
* @param[in] item Item.
*/
void Plot2D::addItem(const int &index, const Item2D::Ptr &item) {
if (item == nullptr) {
return;
}
if (index > m_itemList.size()) {
m_itemList.append(item);
} else if (index < 0) {
m_itemList.prepend(item);
} else {
m_itemList.insert(index, item);
}
}
/**
* Retrieve the plot origin in widget coordinates.
*
* @return Origin.
*/
QPointF Plot2D::getScreenOrigin() const {
return QPointF(m_screenOriginX, m_screenOriginY);
}
/**
* Retrieve the scale factor for X coordiate.
*
* @return Scale factor.
*/
double Plot2D::getScaleX() const {
return m_scaleX;
}
/**
* Retrieve the scale factor for Y coordiate.
*
* @return Scale factor.
*/
double Plot2D::getScaleY() const {
return m_scaleY;
}
/**
* Retrieve the scaled point from plot coordinates to widget coordinates.
*
* @param[in] point Point in plot coordinates.
*
* @return Point in widget coordinates.
*/
QPointF Plot2D::scalePoint(const QPointF &point) const {
return scalePoint(point.x(), point.y());
}
/**
* Retrieve the scaled point from plot coordinates to widget coordinates.
*
* @param[in] x \f$X\f$ coordinate.
* @param[in] y \f$Y\f$ coordinate.
*
* @return Point in widget coordinates.
*/
QPointF Plot2D::scalePoint(const double &x, const double &y) const {
const double sx = scalePointX(x);
const double sy = scalePointY(y);
return QPointF(sx, sy);
}
/**
* Scale the \f$X\f$ coordinate from plot coordinates to widget coordinates.
*
* @param[in] x \f$X\f$ coordinate.
*
* @return Scaled coordinate.
*/
double Plot2D::scalePointX(const double &x) const {
const double paddingLeft = m_padding.getLeftPadding(Padding::PIXELS);
const double paddingRight = m_padding.getRightPadding(Padding::PIXELS);
const double multX = paddingLeft + paddingRight - width();
const double fracX = (x - m_upperLeftCorner.x()) / (m_upperLeftCorner.x() - m_lowerRightCorner.x());
return paddingLeft + multX * fracX;
}
/**
* Scale the \f$Y\f$ coordinate from plot coordinates to widget coordinates.
*
* @param[in] y \f$Y\f$ coordinate.
*
* @return Scaled coordinate.
*/
double Plot2D::scalePointY(const double &y) const {
const double paddingBottom = m_padding.getBottomPadding(Padding::PIXELS);
const double paddingTop = m_padding.getTopPadding(Padding::PIXELS);
const double multY = paddingBottom + paddingTop - height();
const double fracY = (y - m_lowerRightCorner.y()) / (m_lowerRightCorner.y() - m_upperLeftCorner.y());
return height() - paddingBottom - multY * fracY;
}
/**
* Scale the given point from widget coordinates to plot coordinates.
*
* @param[in] point Point.
*
* @return Scaled point in plot coordinates.
*/
QPointF Plot2D::inverseScalePoint(const QPointF &point) const {
return inverseScalePoint(point.x(), point.y());
}
/**
* Scale the given point from widget coordinates to plot coordinates.
*
* @param[in] x X coordinate.
* @param[in] y Y coordinate.
*
* @return Scaled point in plot coordinates.
*/
QPointF Plot2D::inverseScalePoint(const double &x, const double &y) const {
return QPointF(inverseScalePointX(x), inverseScalePointY(y));
}
/**
* Scale the coordiante in X axis from widget coordinates to plot coordinates.
*
* @param[in] x X coordinate.
*
* @return Scaled coordinate in plot coordinates.
*/
double Plot2D::inverseScalePointX(const double &x) const {
const double paddingLeft = m_padding.getLeftPadding(Padding::PIXELS);
const double paddingRight = m_padding.getRightPadding(Padding::PIXELS);
const double multX = paddingLeft + paddingRight - width();
const double a = m_upperLeftCorner.x();
const double b = m_lowerRightCorner.x();
const double num1 = a * (multX - paddingLeft + x);
const double num2 = b * (paddingLeft - x);
return (num1 + num2) / multX;
}
/**
* Scale the coordiante in Y axis from widget coordinates to plot coordinates.
*
* @param[in] y Y coordinate.
*
* @return Scaled coordinate in plot coordinates.
*/
double Plot2D::inverseScalePointY(const double &y) const {
const double paddingBottom = m_padding.getBottomPadding(Padding::PIXELS);
const double paddingTop = m_padding.getTopPadding(Padding::PIXELS);
const double multY = paddingBottom + paddingTop - height();
const double a = m_lowerRightCorner.y();
const double b = m_upperLeftCorner.y();
const double num1 = a * (height() + multY - paddingBottom - y);
const double num2 = b * (paddingBottom - height() + y);
return (num1 + num2) / multY;
}
/**
* Retrieve the upper left corner.
*
* @return Corner coordinates.
*/
QPointF Plot2D::getUpperLeftCorner() const {
return m_upperLeftCorner;
}
/**
* Retrieve the lower right corner.
*
* @return Corner coordinates.
*/
QPointF Plot2D::getLowerRightCorner() const {
return m_lowerRightCorner;
}
///////////////////////////////////////////////////////////////////////////////
// PROTECTED SECTION //
///////////////////////////////////////////////////////////////////////////////
/**
* Initialize coordinate conversion. To be used before paint anything.
*/
void Plot2D::initPainterCoordinates() {
const double dx = m_upperLeftCorner.x() - m_lowerRightCorner.x();
const double dy = m_lowerRightCorner.y() - m_upperLeftCorner.y();
m_padding.setSize(static_cast<double>(width()), static_cast<double>(height()));
const Padding::Values paddingValues = m_padding.getPaddingValues(Padding::PIXELS);
m_scaleX = (static_cast<double>(width()) * (1.0 - paddingValues.left - paddingValues.right)) / -dx;
m_scaleY = (static_cast<double>(height()) * (1.0 - paddingValues.bottom - paddingValues.top)) / -dy;
m_screenOriginX = scalePointX(0.0);
m_screenOriginY = scalePointY(0.0);
}
void Plot2D::drawAxes() {
for (auto& it : m_axesList) {
it->draw(this);
}
}
/**
* Draw all items that are added.
*/
void Plot2D::drawItems() {
for (auto& it : m_itemList) {
it->draw(this);
}
}
} // namespace WPlot