Qt 6.x
The Qt SDK
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/qpair.h"
22#include "QtCore/qpoint.h"
23#include "QtCore/qrect.h"
24#include "QtGui/qtransform.h"
25#include <private/qdatabuffer_p.h>
26
28
29class QPolygonF;
30
31class Q_GUI_EXPORT QBezier
32{
33public:
34 static QBezier fromPoints(const QPointF &p1, const QPointF &p2,
35 const QPointF &p3, const QPointF &p4)
36 { return {p1.x(), p1.y(), p2.x(), p2.y(), p3.x(), p3.y(), p4.x(), p4.y()}; }
37
38 static void coefficients(qreal t, qreal &a, qreal &b, qreal &c, qreal &d);
39
40 inline QPointF pointAt(qreal t) const;
41 inline QPointF normalVector(qreal t) const;
42
43 inline QPointF derivedAt(qreal t) const;
44 inline QPointF secondDerivedAt(qreal t) const;
45
46 QPolygonF toPolygon(qreal bezier_flattening_threshold = 0.5) const;
47 void addToPolygon(QPolygonF *p, qreal bezier_flattening_threshold = 0.5) const;
48 void addToPolygon(QDataBuffer<QPointF> &polygon, qreal bezier_flattening_threshold) const;
49
50 QPolygonF toQuadratics(qreal errorLimit = 0.2) const;
51 void addToQuadratics(QPolygonF *p, qreal tspan = 1.0, qreal errorLimit = 0.2) const;
52 int inflectionPoints(qreal *tpoints) const;
53
54 QRectF bounds() const;
55 qreal length(qreal error = 0.01) const;
56 void addIfClose(qreal *length, qreal error) const;
57
58 qreal tAtLength(qreal len) const;
59
60 int stationaryYPoints(qreal &t0, qreal &t1) const;
61 qreal tForY(qreal t0, qreal t1, qreal y) const;
62
63 QPointF pt1() const { return QPointF(x1, y1); }
64 QPointF pt2() const { return QPointF(x2, y2); }
65 QPointF pt3() const { return QPointF(x3, y3); }
66 QPointF pt4() const { return QPointF(x4, y4); }
67
68 QBezier mapBy(const QTransform &transform) const;
69
70 inline QPointF midPoint() const;
71 inline QLineF midTangent() const;
72
73 inline QLineF startTangent() const;
74 inline QLineF endTangent() const;
75
76 inline void parameterSplitLeft(qreal t, QBezier *left);
77 inline std::pair<QBezier, QBezier> split() const;
78
79 int shifted(QBezier *curveSegments, int maxSegmets,
80 qreal offset, float threshold) const;
81
82 QBezier bezierOnInterval(qreal t0, qreal t1) const;
83 QBezier getSubRange(qreal t0, qreal t1) const;
84
85 qreal x1, y1, x2, y2, x3, y3, x4, y4;
86};
87
89{
90 return QPointF((x1 + x4 + 3*(x2 + x3))/8., (y1 + y4 + 3*(y2 + y3))/8.);
91}
92
94{
95 QPointF mid = midPoint();
96 QLineF dir(QLineF(x1, y1, x2, y2).pointAt(0.5), QLineF(x3, y3, x4, y4).pointAt(0.5));
97 return QLineF(mid.x() - dir.dx(), mid.y() - dir.dy(),
98 mid.x() + dir.dx(), mid.y() + dir.dy());
99}
100
102{
103 QLineF tangent(pt1(), pt2());
104 if (tangent.isNull())
105 tangent = QLineF(pt1(), pt3());
106 if (tangent.isNull())
107 tangent = QLineF(pt1(), pt4());
108 return tangent;
109}
110
112{
113 QLineF tangent(pt4(), pt3());
114 if (tangent.isNull())
115 tangent = QLineF(pt4(), pt2());
116 if (tangent.isNull())
117 tangent = QLineF(pt4(), pt1());
118 return tangent;
119}
120
122{
123 qreal m_t = 1. - t;
124 b = m_t * m_t;
125 c = t * t;
126 d = c * t;
127 a = b * m_t;
128 b *= 3. * t;
129 c *= 3. * m_t;
130}
131
133{
134 // numerically more stable:
135 qreal x, y;
136
137 qreal m_t = 1. - t;
138 {
139 qreal a = x1*m_t + x2*t;
140 qreal b = x2*m_t + x3*t;
141 qreal c = x3*m_t + x4*t;
142 a = a*m_t + b*t;
143 b = b*m_t + c*t;
144 x = a*m_t + b*t;
145 }
146 {
147 qreal a = y1*m_t + y2*t;
148 qreal b = y2*m_t + y3*t;
149 qreal c = y3*m_t + y4*t;
150 a = a*m_t + b*t;
151 b = b*m_t + c*t;
152 y = a*m_t + b*t;
153 }
154 return QPointF(x, y);
155}
156
158{
159 qreal m_t = 1. - t;
160 qreal a = m_t * m_t;
161 qreal b = t * m_t;
162 qreal c = t * t;
163
164 return QPointF((y2-y1) * a + (y3-y2) * b + (y4-y3) * c, -(x2-x1) * a - (x3-x2) * b - (x4-x3) * c);
165}
166
168{
169 // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 * t^2) * p2 + t^2 * p3)
170
171 qreal m_t = 1. - t;
172
173 qreal d = t * t;
174 qreal a = -m_t * m_t;
175 qreal b = 1 - 4 * t + 3 * d;
176 qreal c = 2 * t - 3 * d;
177
178 return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4,
179 a * y1 + b * y2 + c * y3 + d * y4);
180}
181
183{
184 qreal a = 2. - 2. * t;
185 qreal b = -4 + 6 * t;
186 qreal c = 2 - 6 * t;
187 qreal d = 2 * t;
188
189 return 3 * QPointF(a * x1 + b * x2 + c * x3 + d * x4,
190 a * y1 + b * y2 + c * y3 + d * y4);
191}
192
193std::pair<QBezier, QBezier> QBezier::split() const
194{
195 const auto mid = [](QPointF lhs, QPointF rhs) { return (lhs + rhs) * .5; };
196
197 const QPointF mid_12 = mid(pt1(), pt2());
198 const QPointF mid_23 = mid(pt2(), pt3());
199 const QPointF mid_34 = mid(pt3(), pt4());
200 const QPointF mid_12_23 = mid(mid_12, mid_23);
201 const QPointF mid_23_34 = mid(mid_23, mid_34);
202 const QPointF mid_12_23__23_34 = mid(mid_12_23, mid_23_34);
203
204 return {
205 fromPoints(pt1(), mid_12, mid_12_23, mid_12_23__23_34),
206 fromPoints(mid_12_23__23_34, mid_23_34, mid_34, pt4()),
207 };
208}
209
211{
212 left->x1 = x1;
213 left->y1 = y1;
214
215 left->x2 = x1 + t * ( x2 - x1 );
216 left->y2 = y1 + t * ( y2 - y1 );
217
218 left->x3 = x2 + t * ( x3 - x2 ); // temporary holding spot
219 left->y3 = y2 + t * ( y3 - y2 ); // temporary holding spot
220
221 x3 = x3 + t * ( x4 - x3 );
222 y3 = y3 + t * ( y4 - y3 );
223
224 x2 = left->x3 + t * ( x3 - left->x3);
225 y2 = left->y3 + t * ( y3 - left->y3);
226
227 left->x3 = left->x2 + t * ( left->x3 - left->x2 );
228 left->y3 = left->y2 + t * ( left->y3 - left->y2 );
229
230 left->x4 = x1 = left->x3 + t * (x2 - left->x3);
231 left->y4 = y1 = left->y3 + t * (y2 - left->y3);
232}
233
235
236#endif // QBEZIER_P_H
QPointF pt1() const
Definition qbezier_p.h:63
QPointF pt3() const
Definition qbezier_p.h:65
qreal x4
Definition qbezier_p.h:85
static QBezier fromPoints(const QPointF &p1, const QPointF &p2, const QPointF &p3, const QPointF &p4)
Definition qbezier_p.h:34
QLineF midTangent() const
Definition qbezier_p.h:93
std::pair< QBezier, QBezier > split() const
Definition qbezier_p.h:193
QPointF pointAt(qreal t) const
Definition qbezier_p.h:132
void parameterSplitLeft(qreal t, QBezier *left)
Definition qbezier_p.h:210
qreal y1
Definition qbezier_p.h:85
qreal y4
Definition qbezier_p.h:85
QLineF startTangent() const
Definition qbezier_p.h:101
QPointF midPoint() const
Definition qbezier_p.h:88
static void coefficients(qreal t, qreal &a, qreal &b, qreal &c, qreal &d)
Definition qbezier_p.h:121
qreal x1
Definition qbezier_p.h:85
QPointF secondDerivedAt(qreal t) const
Definition qbezier_p.h:182
QPointF derivedAt(qreal t) const
Definition qbezier_p.h:167
QPointF pt4() const
Definition qbezier_p.h:66
qreal x3
Definition qbezier_p.h:85
qreal y3
Definition qbezier_p.h:85
QLineF endTangent() const
Definition qbezier_p.h:111
QPointF normalVector(qreal t) const
Definition qbezier_p.h:157
qreal x2
Definition qbezier_p.h:85
qreal y2
Definition qbezier_p.h:85
QPointF pt2() const
Definition qbezier_p.h:64
\inmodule QtCore
Definition qline.h:182
constexpr bool isNull() const
Returns true if the line does not have distinct start and end points; otherwise returns false.
Definition qline.h:284
\inmodule QtCore\reentrant
Definition qpoint.h:214
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:333
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:338
The QPolygonF class provides a list of points using floating point precision.
Definition qpolygon.h:96
\inmodule QtCore\reentrant
Definition qrect.h:483
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
GLenum GLsizei len
GLfixed GLfixed x2
GLdouble GLdouble t
Definition qopenglext.h:243
GLfloat GLfloat p
[1]
static void split(QT_FT_Vector *b)
double qreal
Definition qtypes.h:92
QString dir
[11]