Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qbezier_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QBEZIER_P_H
5#define QBEZIER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of other Qt classes. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtGui/private/qtguiglobal_p.h>
19#include "QtCore/qline.h"
20#include "QtCore/qlist.h"
21#include "QtCore/qpoint.h"
22#include "QtCore/qrect.h"
23#include "QtGui/qtransform.h"
24#include <private/qdatabuffer_p.h>
25
27
28class QPolygonF;
29
30class Q_GUI_EXPORT QBezier
31{
32public:
33 static QBezier fromPoints(const QPointF &p1, const QPointF &p2,
34 const QPointF &p3, const QPointF &p4)
35 { return {p1.x(), p1.y(), p2.x(), p2.y(), p3.x(), p3.y(), p4.x(), p4.y()}; }
36
37 static void coefficients(qreal t, qreal &a, qreal &b, qreal &c, qreal &d);
38
39 inline QPointF pointAt(qreal t) const;
40 inline QPointF normalVector(qreal t) const;
41
42 inline QPointF derivedAt(qreal t) const;
43 inline QPointF secondDerivedAt(qreal t) const;
44
45 QPolygonF toPolygon(qreal bezier_flattening_threshold = 0.5) const;
46 void addToPolygon(QPolygonF *p, qreal bezier_flattening_threshold = 0.5) const;
47 void addToPolygon(QDataBuffer<QPointF> &polygon, qreal bezier_flattening_threshold) const;
48
49 QRectF bounds() const;
50 qreal length(qreal error = 0.01) const;
51 void addIfClose(qreal *length, qreal error) const;
52
53 qreal tAtLength(qreal len) const;
54
55 int stationaryYPoints(qreal &t0, qreal &t1) const;
56 qreal tForY(qreal t0, qreal t1, qreal y) const;
57
58 QPointF pt1() const { return QPointF(x1, y1); }
59 QPointF pt2() const { return QPointF(x2, y2); }
60 QPointF pt3() const { return QPointF(x3, y3); }
61 QPointF pt4() const { return QPointF(x4, y4); }
62
63 QBezier mapBy(const QTransform &transform) const;
64
65 inline QPointF midPoint() const;
66 inline QLineF midTangent() const;
67
68 inline QLineF startTangent() const;
69 inline QLineF endTangent() const;
70
71 inline void parameterSplitLeft(qreal t, QBezier *left);
72 inline std::pair<QBezier, QBezier> split() const;
73
74 int shifted(QBezier *curveSegments, int maxSegmets,
75 qreal offset, float threshold) const;
76
77 QBezier bezierOnInterval(qreal t0, qreal t1) const;
78 QBezier getSubRange(qreal t0, qreal t1) const;
79
80 qreal x1, y1, x2, y2, x3, y3, x4, y4;
81};
82
84{
85 return QPointF((x1 + x4 + 3*(x2 + x3))/8., (y1 + y4 + 3*(y2 + y3))/8.);
86}
87
89{
90 QPointF mid = midPoint();
91 QLineF dir(QLineF(x1, y1, x2, y2).pointAt(0.5), QLineF(x3, y3, x4, y4).pointAt(0.5));
92 return QLineF(mid.x() - dir.dx(), mid.y() - dir.dy(),
93 mid.x() + dir.dx(), mid.y() + dir.dy());
94}
95
97{
98 QLineF tangent(pt1(), pt2());
99 if (tangent.isNull())
100 tangent = QLineF(pt1(), pt3());
101 if (tangent.isNull())
102 tangent = QLineF(pt1(), pt4());
103 return tangent;
104}
105
107{
108 QLineF tangent(pt4(), pt3());
109 if (tangent.isNull())
110 tangent = QLineF(pt4(), pt2());
111 if (tangent.isNull())
112 tangent = QLineF(pt4(), pt1());
113 return tangent;
114}
115
117{
118 qreal m_t = 1. - t;
119 b = m_t * m_t;
120 c = t * t;
121 d = c * t;
122 a = b * m_t;
123 b *= 3. * t;
124 c *= 3. * m_t;
125}
126
128{
129 // numerically more stable:
130 qreal x, y;
131
132 qreal m_t = 1. - t;
133 {
134 qreal a = x1*m_t + x2*t;
135 qreal b = x2*m_t + x3*t;
136 qreal c = x3*m_t + x4*t;
137 a = a*m_t + b*t;
138 b = b*m_t + c*t;
139 x = a*m_t + b*t;
140 }
141 {
142 qreal a = y1*m_t + y2*t;
143 qreal b = y2*m_t + y3*t;
144 qreal c = y3*m_t + y4*t;
145 a = a*m_t + b*t;
146 b = b*m_t + c*t;
147 y = a*m_t + b*t;
148 }
149 return QPointF(x, y);
150}
151
153{
154 qreal m_t = 1. - t;
155 qreal a = m_t * m_t;
156 qreal b = t * m_t;
157 qreal c = t * t;
158
159 return QPointF((y2-y1) * a + (y3-y2) * b + (y4-y3) * c, -(x2-x1) * a - (x3-x2) * b - (x4-x3) * c);
160}
161
163{
164 // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 * t^2) * p2 + t^2 * p3)
165
166 qreal m_t = 1. - t;
167
168 qreal d = t * t;
169 qreal a = -m_t * m_t;
170 qreal b = 1 - 4 * t + 3 * d;
171 qreal c = 2 * t - 3 * d;
172
173 return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4,
174 a * y1 + b * y2 + c * y3 + d * y4);
175}
176
178{
179 qreal a = 2. - 2. * t;
180 qreal b = -4 + 6 * t;
181 qreal c = 2 - 6 * t;
182 qreal d = 2 * t;
183
184 return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4,
185 a * y1 + b * y2 + c * y3 + d * y4);
186}
187
188std::pair<QBezier, QBezier> QBezier::split() const
189{
190 const auto mid = [](QPointF lhs, QPointF rhs) { return (lhs + rhs) * .5; };
191
192 const QPointF mid_12 = mid(pt1(), pt2());
193 const QPointF mid_23 = mid(pt2(), pt3());
194 const QPointF mid_34 = mid(pt3(), pt4());
195 const QPointF mid_12_23 = mid(mid_12, mid_23);
196 const QPointF mid_23_34 = mid(mid_23, mid_34);
197 const QPointF mid_12_23__23_34 = mid(mid_12_23, mid_23_34);
198
199 return {
200 fromPoints(pt1(), mid_12, mid_12_23, mid_12_23__23_34),
201 fromPoints(mid_12_23__23_34, mid_23_34, mid_34, pt4()),
202 };
203}
204
206{
207 left->x1 = x1;
208 left->y1 = y1;
209
210 left->x2 = x1 + t * ( x2 - x1 );
211 left->y2 = y1 + t * ( y2 - y1 );
212
213 left->x3 = x2 + t * ( x3 - x2 ); // temporary holding spot
214 left->y3 = y2 + t * ( y3 - y2 ); // temporary holding spot
215
216 x3 = x3 + t * ( x4 - x3 );
217 y3 = y3 + t * ( y4 - y3 );
218
219 x2 = left->x3 + t * ( x3 - left->x3);
220 y2 = left->y3 + t * ( y3 - left->y3);
221
222 left->x3 = left->x2 + t * ( left->x3 - left->x2 );
223 left->y3 = left->y2 + t * ( left->y3 - left->y2 );
224
225 left->x4 = x1 = left->x3 + t * (x2 - left->x3);
226 left->y4 = y1 = left->y3 + t * (y2 - left->y3);
227}
228
230
231#endif // QBEZIER_P_H
QPointF pt1() const
Definition qbezier_p.h:58
QPointF pt3() const
Definition qbezier_p.h:60
qreal x4
Definition qbezier_p.h:80
static QBezier fromPoints(const QPointF &p1, const QPointF &p2, const QPointF &p3, const QPointF &p4)
Definition qbezier_p.h:33
QLineF midTangent() const
Definition qbezier_p.h:88
std::pair< QBezier, QBezier > split() const
Definition qbezier_p.h:188
QPointF pointAt(qreal t) const
Definition qbezier_p.h:127
void parameterSplitLeft(qreal t, QBezier *left)
Definition qbezier_p.h:205
qreal y1
Definition qbezier_p.h:80
qreal y4
Definition qbezier_p.h:80
QLineF startTangent() const
Definition qbezier_p.h:96
QPointF midPoint() const
Definition qbezier_p.h:83
static void coefficients(qreal t, qreal &a, qreal &b, qreal &c, qreal &d)
Definition qbezier_p.h:116
qreal x1
Definition qbezier_p.h:80
QPointF secondDerivedAt(qreal t) const
Definition qbezier_p.h:177
QPointF derivedAt(qreal t) const
Definition qbezier_p.h:162
QPointF pt4() const
Definition qbezier_p.h:61
qreal x3
Definition qbezier_p.h:80
qreal y3
Definition qbezier_p.h:80
QLineF endTangent() const
Definition qbezier_p.h:106
QPointF normalVector(qreal t) const
Definition qbezier_p.h:152
qreal x2
Definition qbezier_p.h:80
qreal y2
Definition qbezier_p.h:80
QPointF pt2() const
Definition qbezier_p.h:59
\inmodule QtCore\compares equality \compareswith equality QLine \endcompareswith
Definition qline.h:192
constexpr bool isNull() const
Returns true if the line does not have distinct start and end points; otherwise returns false.
Definition qline.h:312
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
The QPolygonF class provides a list of points using floating point precision.
Definition qpolygon.h:96
\inmodule QtCore\reentrant
Definition qrect.h:484
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
QPixmap p2
QPixmap p1
[0]
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint GLfloat GLfloat GLfloat GLfloat y1
GLuint GLfloat GLfloat GLfloat x1
GLenum GLuint GLenum GLsizei length
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLint left
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t0
GLenum GLuint GLintptr offset
GLint y
GLuint GLenum GLenum transform
const GLubyte * c
GLfixed GLfixed GLfixed y2
GLfixed GLfixed x2
GLdouble GLdouble t
Definition qopenglext.h:243
GLfloat GLfloat p
[1]
GLenum GLsizei len
static void split(QT_FT_Vector *b)
double qreal
Definition qtypes.h:187
QString dir
[11]