65void QBezier::addToPolygon(QPolygonF *polygon, qreal bezier_flattening_threshold)
const
74 QBezier *b = &beziers[top];
76 qreal y4y1 = b->y4 - b->y1;
77 qreal x4x1 = b->x4 - b->x1;
78 qreal l = qAbs(x4x1) + qAbs(y4y1);
81 d = qAbs( (x4x1)*(b->y1 - b->y2) - (y4y1)*(b->x1 - b->x2) )
82 + qAbs( (x4x1)*(b->y1 - b->y3) - (y4y1)*(b->x1 - b->x3) );
84 d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) +
85 qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3);
88 if (d < bezier_flattening_threshold * l || levels[top] == 0) {
90 polygon->append(QPointF(b->x4, b->y4));
94 std::tie(b[1], b[0]) = b->split();
95 levels[top + 1] = --levels[top];
101void QBezier::addToPolygon(QDataBuffer<QPointF> &polygon, qreal bezier_flattening_threshold)
const
110 QBezier *b = &beziers[top];
112 qreal y4y1 = b->y4 - b->y1;
113 qreal x4x1 = b->x4 - b->x1;
114 qreal l = qAbs(x4x1) + qAbs(y4y1);
117 d = qAbs( (x4x1)*(b->y1 - b->y2) - (y4y1)*(b->x1 - b->x2) )
118 + qAbs( (x4x1)*(b->y1 - b->y3) - (y4y1)*(b->x1 - b->x3) );
120 d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) +
121 qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3);
124 if (d < bezier_flattening_threshold * l || levels[top] == 0) {
126 polygon.add(QPointF(b->x4, b->y4));
130 std::tie(b[1], b[0]) = b->split();
131 levels[top + 1] = --levels[top];
181 const qreal o2 = offset*offset;
182 const qreal max_dist_line = threshold*offset*offset;
183 const qreal max_dist_normal = threshold*offset;
184 const int divisions = 4;
185 const qreal spacing = qreal(1.0) / divisions;
187 for (
int i = 1; i < divisions; ++i, t += spacing) {
188 QPointF p1 = b1->pointAt(t);
189 QPointF p2 = b2->pointAt(t);
190 qreal d = (p1.x() - p2.x())*(p1.x() - p2.x()) + (p1.y() - p2.y())*(p1.y() - p2.y());
191 if (qAbs(d - o2) > max_dist_line)
194 QPointF normalPoint = b1->normalVector(t);
195 qreal l = qAbs(normalPoint.x()) + qAbs(normalPoint.y());
196 if (l != qreal(0.0)) {
197 d = qAbs( normalPoint.x()*(p1.y() - p2.y()) - normalPoint.y()*(p1.x() - p2.x()) ) / l;
198 if (d > max_dist_normal)
207static ShiftResult shift(
const QBezier *orig, QBezier *shifted, qreal offset, qreal threshold)
210 bool p1_p2_equal = qFuzzyCompare(orig->x1, orig->x2) && qFuzzyCompare(orig->y1, orig->y2);
211 bool p2_p3_equal = qFuzzyCompare(orig->x2, orig->x3) && qFuzzyCompare(orig->y2, orig->y3);
212 bool p3_p4_equal = qFuzzyCompare(orig->x3, orig->x4) && qFuzzyCompare(orig->y3, orig->y4);
216 points[np] = QPointF(orig->x1, orig->y1);
220 points[np] = QPointF(orig->x2, orig->y2);
225 points[np] = QPointF(orig->x3, orig->y3);
230 points[np] = QPointF(orig->x4, orig->y4);
237 QRectF b = orig->bounds();
238 if (np == 4 && b.width() < .1*offset && b.height() < .1*offset) {
239 qreal l = (orig->x1 - orig->x2)*(orig->x1 - orig->x2) +
240 (orig->y1 - orig->y2)*(orig->y1 - orig->y2) *
241 (orig->x3 - orig->x4)*(orig->x3 - orig->x4) +
242 (orig->y3 - orig->y4)*(orig->y3 - orig->y4);
243 qreal dot = (orig->x1 - orig->x2)*(orig->x3 - orig->x4) +
244 (orig->y1 - orig->y2)*(orig->y3 - orig->y4);
245 if (dot < 0 && dot*dot < 0.8*l)
251 QPointF points_shifted[4];
253 QLineF prev = QLineF(QPointF(), points[1] - points[0]);
256 QPointF prev_normal = prev.normalVector().unitVector().p2();
258 points_shifted[0] = points[0] + offset * prev_normal;
260 for (
int i = 1; i < np - 1; ++i) {
261 QLineF next = QLineF(QPointF(), points[i + 1] - points[i]);
262 QPointF next_normal = next.normalVector().unitVector().p2();
264 QPointF normal_sum = prev_normal + next_normal;
266 qreal r = qreal(1.0) + prev_normal.x() * next_normal.x()
267 + prev_normal.y() * next_normal.y();
269 if (qFuzzyIsNull(r)) {
270 points_shifted[i] = points[i] + offset * prev_normal;
272 qreal k = offset / r;
273 points_shifted[i] = points[i] + k * normal_sum;
276 prev_normal = next_normal;
279 points_shifted[np - 1] = points[np - 1] + offset * prev_normal;
281 *shifted = QBezier::fromPoints(points_shifted[map[0]], points_shifted[map[1]],
282 points_shifted[map[2]], points_shifted[map[3]]);
285 return good_offset(orig, shifted, offset, threshold);
295static bool addCircle(
const QBezier *b, qreal offset, QBezier *o)
299 normals[0] = QPointF(b->y2 - b->y1, b->x1 - b->x2);
300 qreal dist = qSqrt(normals[0].x()*normals[0].x() + normals[0].y()*normals[0].y());
301 if (qFuzzyIsNull(dist))
304 normals[2] = QPointF(b->y4 - b->y3, b->x3 - b->x4);
305 dist = qSqrt(normals[2].x()*normals[2].x() + normals[2].y()*normals[2].y());
306 if (qFuzzyIsNull(dist))
310 normals[1] = QPointF(b->x1 - b->x2 - b->x3 + b->x4, b->y1 - b->y2 - b->y3 + b->y4);
311 normals[1] /= -1*qSqrt(normals[1].x()*normals[1].x() + normals[1].y()*normals[1].y());
315 for (
int i = 0; i < 2; ++i) {
316 qreal cos_a = normals[i].x()*normals[i+1].x() + normals[i].y()*normals[i+1].y();
321 angles[i] = qAcos(cos_a) * qreal(
M_1_PI);
324 if (angles[0] + angles[1] > 1.) {
326 normals[1] = -normals[1];
327 angles[0] = 1. - angles[0];
328 angles[1] = 1. - angles[1];
334 circle[0] = QPointF(b->x1, b->y1) + normals[0]*offset;
335 circle[1] = QPointF(qreal(0.5)*(b->x1 + b->x4), qreal(0.5)*(b->y1 + b->y4)) + normals[1]*offset;
336 circle[2] = QPointF(b->x4, b->y4) + normals[2]*offset;
338 for (
int i = 0; i < 2; ++i) {
339 qreal kappa = qreal(2.0) *
KAPPA * sign * offset * angles[i];
341 o->x1 = circle[i].x();
342 o->y1 = circle[i].y();
343 o->x2 = circle[i].x() - normals[i].y()*kappa;
344 o->y2 = circle[i].y() + normals[i].x()*kappa;
345 o->x3 = circle[i+1].x() + normals[i+1].y()*kappa;
346 o->y3 = circle[i+1].y() - normals[i+1].x()*kappa;
347 o->x4 = circle[i+1].x();
348 o->y4 = circle[i+1].y();
355int QBezier::shifted(QBezier *curveSegments,
int maxSegments, qreal offset,
float threshold)
const
357 Q_ASSERT(curveSegments);
358 Q_ASSERT(maxSegments > 0);
360 if (qFuzzyCompare(x1, x2) && qFuzzyCompare(x1, x3) && qFuzzyCompare(x1, x4) &&
361 qFuzzyCompare(y1, y2) && qFuzzyCompare(y1, y3) && qFuzzyCompare(y1, y4))
368 QBezier *b = beziers;
369 QBezier *o = curveSegments;
371 while (b >= beziers) {
372 int stack_segments = b - beziers + 1;
373 if ((stack_segments == 10) || (o - curveSegments == maxSegments - stack_segments)) {
374 threshold *= qreal(1.5);
375 if (threshold > qreal(2.0))
379 ShiftResult res = shift(b, o, offset, threshold);
380 if (res == Discard) {
382 }
else if (res == Ok) {
385 }
else if (res == Circle && maxSegments - (o - curveSegments) >= 2) {
387 if (addCircle(b, offset, o))
391 std::tie(b[1], b[0]) = b->split();
397 while (b >= beziers) {
398 ShiftResult res = shift(b, o, offset, threshold);
401 if (res == Ok || res == Split)
407 Q_ASSERT(o - curveSegments <= maxSegments);
408 return o - curveSegments;
431void QBezier::addIfClose(qreal *length, qreal error)
const
433 qreal len = qreal(0.0);
436 len = len + QLineF(QPointF(x1, y1),QPointF(x2, y2)).length();
437 len = len + QLineF(QPointF(x2, y2),QPointF(x3, y3)).length();
438 len = len + QLineF(QPointF(x3, y3),QPointF(x4, y4)).length();
440 chord = QLineF(QPointF(x1, y1),QPointF(x4, y4)).length();
442 if ((len-chord) > error) {
443 const auto halves = split();
444 halves.first.addIfClose(length, error);
445 halves.second.addIfClose(length, error);
449 *length = *length + len;
454qreal QBezier::tForY(qreal t0, qreal t1, qreal y)
const
456 qreal py0 = pointAt(t0).y();
457 qreal py1 = pointAt(t1).y();
464 Q_ASSERT(py0 <= py1);
471 Q_ASSERT(py0 < y && y < py1);
476 qreal t = qreal(0.5) * (t0 + t1);
479 QBezier::coefficients(t, a, b, c, d);
480 qreal yt = a * y1 + b * y2 + c * y3 + d * y4;
491 }
while (qAbs(dt) > qreal(1e-7));
496int QBezier::stationaryYPoints(qreal &t0, qreal &t1)
const
502 const qreal a = -y1 + 3 * y2 - 3 * y3 + y4;
503 const qreal b = 2 * y1 - 4 * y2 + 2 * y3;
504 const qreal c = -y1 + y2;
506 if (qFuzzyIsNull(a)) {
511 return t0 > 0 && t0 < 1;
514 qreal reciprocal = b * b - 4 * a * c;
516 if (qFuzzyIsNull(reciprocal)) {
518 return t0 > 0 && t0 < 1;
519 }
else if (reciprocal > 0) {
520 qreal temp = qSqrt(reciprocal);
522 t0 = (-b - temp)/(2*a);
523 t1 = (-b + temp)/(2*a);
529 qreal t[2] = { 0, 1 };
531 if (t0 > 0 && t0 < 1)
533 if (t1 > 0 && t1 < 1)
545qreal QBezier::tAtLength(qreal l)
const
547 qreal len = length();
548 qreal t = qreal(1.0);
549 const qreal error = qreal(0.01);
550 if (l > len || qFuzzyCompare(l, len))
556 qreal lastBigger = qreal(1.0);
559 QBezier right = *
this;
561 right.parameterSplitLeft(t, &left);
562 qreal lLen = left.length();
563 if (qAbs(lLen - l) < error)
567 t += (lastBigger - t) * qreal(0.5);