66void QBezier::addToPolygon(QPolygonF *polygon, qreal bezier_flattening_threshold)
const
75 QBezier *b = &beziers[top];
77 qreal y4y1 = b->y4 - b->y1;
78 qreal x4x1 = b->x4 - b->x1;
79 qreal l = qAbs(x4x1) + qAbs(y4y1);
82 d = qAbs( (x4x1)*(b->y1 - b->y2) - (y4y1)*(b->x1 - b->x2) )
83 + qAbs( (x4x1)*(b->y1 - b->y3) - (y4y1)*(b->x1 - b->x3) );
85 d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) +
86 qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3);
89 if (d < bezier_flattening_threshold * l || levels[top] == 0) {
91 polygon->append(QPointF(b->x4, b->y4));
95 std::tie(b[1], b[0]) = b->split();
96 levels[top + 1] = --levels[top];
102void QBezier::addToPolygon(QDataBuffer<QPointF> &polygon, qreal bezier_flattening_threshold)
const
111 QBezier *b = &beziers[top];
113 qreal y4y1 = b->y4 - b->y1;
114 qreal x4x1 = b->x4 - b->x1;
115 qreal l = qAbs(x4x1) + qAbs(y4y1);
118 d = qAbs( (x4x1)*(b->y1 - b->y2) - (y4y1)*(b->x1 - b->x2) )
119 + qAbs( (x4x1)*(b->y1 - b->y3) - (y4y1)*(b->x1 - b->x3) );
121 d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) +
122 qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3);
125 if (d < bezier_flattening_threshold * l || levels[top] == 0) {
127 polygon.add(QPointF(b->x4, b->y4));
131 std::tie(b[1], b[0]) = b->split();
132 levels[top + 1] = --levels[top];
182 const qreal o2 = offset*offset;
183 const qreal max_dist_line = threshold*offset*offset;
184 const qreal max_dist_normal = threshold*offset;
185 const int divisions = 4;
186 const qreal spacing = qreal(1.0) / divisions;
188 for (
int i = 1; i < divisions; ++i, t += spacing) {
189 QPointF p1 = b1->pointAt(t);
190 QPointF p2 = b2->pointAt(t);
191 qreal d = (p1.x() - p2.x())*(p1.x() - p2.x()) + (p1.y() - p2.y())*(p1.y() - p2.y());
192 if (qAbs(d - o2) > max_dist_line)
195 QPointF normalPoint = b1->normalVector(t);
196 qreal l = qAbs(normalPoint.x()) + qAbs(normalPoint.y());
197 if (l != qreal(0.0)) {
198 d = qAbs( normalPoint.x()*(p1.y() - p2.y()) - normalPoint.y()*(p1.x() - p2.x()) ) / l;
199 if (d > max_dist_normal)
208static ShiftResult shift(
const QBezier *orig, QBezier *shifted, qreal offset, qreal threshold)
211 bool p1_p2_equal = qFuzzyCompare(orig->x1, orig->x2) && qFuzzyCompare(orig->y1, orig->y2);
212 bool p2_p3_equal = qFuzzyCompare(orig->x2, orig->x3) && qFuzzyCompare(orig->y2, orig->y3);
213 bool p3_p4_equal = qFuzzyCompare(orig->x3, orig->x4) && qFuzzyCompare(orig->y3, orig->y4);
217 points[np] = QPointF(orig->x1, orig->y1);
221 points[np] = QPointF(orig->x2, orig->y2);
226 points[np] = QPointF(orig->x3, orig->y3);
231 points[np] = QPointF(orig->x4, orig->y4);
238 QRectF b = orig->bounds();
239 if (np == 4 && b.width() < .1*offset && b.height() < .1*offset) {
240 qreal l = (orig->x1 - orig->x2)*(orig->x1 - orig->x2) +
241 (orig->y1 - orig->y2)*(orig->y1 - orig->y2) *
242 (orig->x3 - orig->x4)*(orig->x3 - orig->x4) +
243 (orig->y3 - orig->y4)*(orig->y3 - orig->y4);
244 qreal dot = (orig->x1 - orig->x2)*(orig->x3 - orig->x4) +
245 (orig->y1 - orig->y2)*(orig->y3 - orig->y4);
246 if (dot < 0 && dot*dot < 0.8*l)
252 QPointF points_shifted[4];
254 QLineF prev = QLineF(QPointF(), points[1] - points[0]);
257 QPointF prev_normal = prev.normalVector().unitVector().p2();
259 points_shifted[0] = points[0] + offset * prev_normal;
261 for (
int i = 1; i < np - 1; ++i) {
262 QLineF next = QLineF(QPointF(), points[i + 1] - points[i]);
263 QPointF next_normal = next.normalVector().unitVector().p2();
265 QPointF normal_sum = prev_normal + next_normal;
267 qreal r = qreal(1.0) + prev_normal.x() * next_normal.x()
268 + prev_normal.y() * next_normal.y();
270 if (qFuzzyIsNull(r)) {
271 points_shifted[i] = points[i] + offset * prev_normal;
273 qreal k = offset / r;
274 points_shifted[i] = points[i] + k * normal_sum;
277 prev_normal = next_normal;
280 points_shifted[np - 1] = points[np - 1] + offset * prev_normal;
282 *shifted = QBezier::fromPoints(points_shifted[map[0]], points_shifted[map[1]],
283 points_shifted[map[2]], points_shifted[map[3]]);
286 return good_offset(orig, shifted, offset, threshold);
296static bool addCircle(
const QBezier *b, qreal offset, QBezier *o)
300 normals[0] = QPointF(b->y2 - b->y1, b->x1 - b->x2);
301 qreal dist = qSqrt(normals[0].x()*normals[0].x() + normals[0].y()*normals[0].y());
302 if (qFuzzyIsNull(dist))
305 normals[2] = QPointF(b->y4 - b->y3, b->x3 - b->x4);
306 dist = qSqrt(normals[2].x()*normals[2].x() + normals[2].y()*normals[2].y());
307 if (qFuzzyIsNull(dist))
311 normals[1] = QPointF(b->x1 - b->x2 - b->x3 + b->x4, b->y1 - b->y2 - b->y3 + b->y4);
312 normals[1] /= -1*qSqrt(normals[1].x()*normals[1].x() + normals[1].y()*normals[1].y());
316 for (
int i = 0; i < 2; ++i) {
317 qreal cos_a = normals[i].x()*normals[i+1].x() + normals[i].y()*normals[i+1].y();
322 angles[i] = qAcos(cos_a) * qreal(
M_1_PI);
325 if (angles[0] + angles[1] > 1.) {
327 normals[1] = -normals[1];
328 angles[0] = 1. - angles[0];
329 angles[1] = 1. - angles[1];
335 circle[0] = QPointF(b->x1, b->y1) + normals[0]*offset;
336 circle[1] = QPointF(qreal(0.5)*(b->x1 + b->x4), qreal(0.5)*(b->y1 + b->y4)) + normals[1]*offset;
337 circle[2] = QPointF(b->x4, b->y4) + normals[2]*offset;
339 for (
int i = 0; i < 2; ++i) {
340 qreal kappa = qreal(2.0) *
KAPPA * sign * offset * angles[i];
342 o->x1 = circle[i].x();
343 o->y1 = circle[i].y();
344 o->x2 = circle[i].x() - normals[i].y()*kappa;
345 o->y2 = circle[i].y() + normals[i].x()*kappa;
346 o->x3 = circle[i+1].x() + normals[i+1].y()*kappa;
347 o->y3 = circle[i+1].y() - normals[i+1].x()*kappa;
348 o->x4 = circle[i+1].x();
349 o->y4 = circle[i+1].y();
356int QBezier::shifted(QBezier *curveSegments,
int maxSegments, qreal offset,
float threshold)
const
358 Q_ASSERT(curveSegments);
359 Q_ASSERT(maxSegments > 0);
361 if (qFuzzyCompare(x1, x2) && qFuzzyCompare(x1, x3) && qFuzzyCompare(x1, x4) &&
362 qFuzzyCompare(y1, y2) && qFuzzyCompare(y1, y3) && qFuzzyCompare(y1, y4))
369 QBezier *b = beziers;
370 QBezier *o = curveSegments;
372 while (b >= beziers) {
373 int stack_segments = b - beziers + 1;
374 if ((stack_segments == 10) || (o - curveSegments == maxSegments - stack_segments)) {
375 threshold *= qreal(1.5);
376 if (threshold > qreal(2.0))
380 ShiftResult res = shift(b, o, offset, threshold);
381 if (res == Discard) {
383 }
else if (res == Ok) {
386 }
else if (res == Circle && maxSegments - (o - curveSegments) >= 2) {
388 if (addCircle(b, offset, o))
392 std::tie(b[1], b[0]) = b->split();
398 while (b >= beziers) {
399 ShiftResult res = shift(b, o, offset, threshold);
402 if (res == Ok || res == Split)
408 Q_ASSERT(o - curveSegments <= maxSegments);
409 return o - curveSegments;
432void QBezier::addIfClose(qreal *length, qreal error)
const
434 qreal len = qreal(0.0);
437 len = len + QLineF(QPointF(x1, y1),QPointF(x2, y2)).length();
438 len = len + QLineF(QPointF(x2, y2),QPointF(x3, y3)).length();
439 len = len + QLineF(QPointF(x3, y3),QPointF(x4, y4)).length();
441 chord = QLineF(QPointF(x1, y1),QPointF(x4, y4)).length();
443 if ((len-chord) > error) {
444 const auto halves = split();
445 halves.first.addIfClose(length, error);
446 halves.second.addIfClose(length, error);
450 *length = *length + len;
455qreal QBezier::tForY(qreal t0, qreal t1, qreal y)
const
457 qreal py0 = pointAt(t0).y();
458 qreal py1 = pointAt(t1).y();
465 Q_ASSERT(py0 <= py1);
472 Q_ASSERT(py0 < y && y < py1);
477 qreal t = qreal(0.5) * (t0 + t1);
480 QBezier::coefficients(t, a, b, c, d);
481 qreal yt = a * y1 + b * y2 + c * y3 + d * y4;
492 }
while (qAbs(dt) > qreal(1e-7));
497int QBezier::stationaryYPoints(qreal &t0, qreal &t1)
const
503 const qreal a = -y1 + 3 * y2 - 3 * y3 + y4;
504 const qreal b = 2 * y1 - 4 * y2 + 2 * y3;
505 const qreal c = -y1 + y2;
507 if (qFuzzyIsNull(a)) {
512 return t0 > 0 && t0 < 1;
515 qreal reciprocal = b * b - 4 * a * c;
517 if (qFuzzyIsNull(reciprocal)) {
519 return t0 > 0 && t0 < 1;
520 }
else if (reciprocal > 0) {
521 qreal temp = qSqrt(reciprocal);
523 t0 = (-b - temp)/(2*a);
524 t1 = (-b + temp)/(2*a);
530 qreal t[2] = { 0, 1 };
532 if (t0 > 0 && t0 < 1)
534 if (t1 > 0 && t1 < 1)
546qreal QBezier::tAtLength(qreal l)
const
548 qreal len = length();
549 qreal t = qreal(1.0);
550 const qreal error = qreal(0.01);
551 if (l > len || qFuzzyCompare(l, len))
557 qreal lastBigger = qreal(1.0);
560 QBezier right = *
this;
562 right.parameterSplitLeft(t, &left);
563 qreal lLen = left.length();
564 if (qAbs(lLen - l) < error)
568 t += (lastBigger - t) * qreal(0.5);