/src/gui/painting/qbezier.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 702 lines · 511 code · 112 blank · 79 comment · 137 complexity · 2a6642bacff368a79f3ce389e85bc85e MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the QtGui module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qbezier_p.h"
  42. #include <qdebug.h>
  43. #include <qline.h>
  44. #include <qpolygon.h>
  45. #include <qvector.h>
  46. #include <qlist.h>
  47. #include <qmath.h>
  48. #include <private/qnumeric_p.h>
  49. #include <private/qmath_p.h>
  50. QT_BEGIN_NAMESPACE
  51. //#define QDEBUG_BEZIER
  52. #ifdef FLOAT_ACCURACY
  53. #define INV_EPS (1L<<23)
  54. #else
  55. /* The value of 1.0 / (1L<<14) is enough for most applications */
  56. #define INV_EPS (1L<<14)
  57. #endif
  58. #ifndef M_SQRT2
  59. #define M_SQRT2 1.41421356237309504880
  60. #endif
  61. /*!
  62. \internal
  63. */
  64. QBezier QBezier::fromPoints(const QPointF &p1, const QPointF &p2,
  65. const QPointF &p3, const QPointF &p4)
  66. {
  67. QBezier b;
  68. b.x1 = p1.x();
  69. b.y1 = p1.y();
  70. b.x2 = p2.x();
  71. b.y2 = p2.y();
  72. b.x3 = p3.x();
  73. b.y3 = p3.y();
  74. b.x4 = p4.x();
  75. b.y4 = p4.y();
  76. return b;
  77. }
  78. /*!
  79. \internal
  80. */
  81. QPolygonF QBezier::toPolygon(qreal bezier_flattening_threshold) const
  82. {
  83. // flattening is done by splitting the bezier until we can replace the segment by a straight
  84. // line. We split further until the control points are close enough to the line connecting the
  85. // boundary points.
  86. //
  87. // the Distance of a point p from a line given by the points (a,b) is given by:
  88. //
  89. // d = abs( (bx - ax)(ay - py) - (by - ay)(ax - px) ) / line_length
  90. //
  91. // We can stop splitting if both control points are close enough to the line.
  92. // To make the algorithm faster we use the manhattan length of the line.
  93. QPolygonF polygon;
  94. polygon.append(QPointF(x1, y1));
  95. addToPolygon(&polygon, bezier_flattening_threshold);
  96. return polygon;
  97. }
  98. QBezier QBezier::mapBy(const QTransform &transform) const
  99. {
  100. return QBezier::fromPoints(transform.map(pt1()), transform.map(pt2()), transform.map(pt3()), transform.map(pt4()));
  101. }
  102. QBezier QBezier::getSubRange(qreal t0, qreal t1) const
  103. {
  104. QBezier result;
  105. QBezier temp;
  106. // cut at t1
  107. if (qFuzzyIsNull(t1 - qreal(1.))) {
  108. result = *this;
  109. } else {
  110. temp = *this;
  111. temp.parameterSplitLeft(t1, &result);
  112. }
  113. // cut at t0
  114. if (!qFuzzyIsNull(t0))
  115. result.parameterSplitLeft(t0 / t1, &temp);
  116. return result;
  117. }
  118. static inline int quadraticRoots(qreal a, qreal b, qreal c,
  119. qreal *x1, qreal *x2)
  120. {
  121. if (qFuzzyIsNull(a)) {
  122. if (qFuzzyIsNull(b))
  123. return 0;
  124. *x1 = *x2 = (-c / b);
  125. return 1;
  126. } else {
  127. const qreal det = b * b - 4 * a * c;
  128. if (qFuzzyIsNull(det)) {
  129. *x1 = *x2 = -b / (2 * a);
  130. return 1;
  131. }
  132. if (det > 0) {
  133. if (qFuzzyIsNull(b)) {
  134. *x2 = qSqrt(-c / a);
  135. *x1 = -(*x2);
  136. return 2;
  137. }
  138. const qreal stableA = b / (2 * a);
  139. const qreal stableB = c / (a * stableA * stableA);
  140. const qreal stableC = -1 - qSqrt(1 - stableB);
  141. *x2 = stableA * stableC;
  142. *x1 = (stableA * stableB) / stableC;
  143. return 2;
  144. } else
  145. return 0;
  146. }
  147. }
  148. static inline bool findInflections(qreal a, qreal b, qreal c,
  149. qreal *t1 , qreal *t2, qreal *tCups)
  150. {
  151. qreal r1 = 0, r2 = 0;
  152. short rootsCount = quadraticRoots(a, b, c, &r1, &r2);
  153. if (rootsCount >= 1) {
  154. if (r1 < r2) {
  155. *t1 = r1;
  156. *t2 = r2;
  157. } else {
  158. *t1 = r2;
  159. *t2 = r1;
  160. }
  161. if (!qFuzzyIsNull(a))
  162. *tCups = qreal(0.5) * (-b / a);
  163. else
  164. *tCups = 2;
  165. return true;
  166. }
  167. return false;
  168. }
  169. void QBezier::addToPolygon(QPolygonF *polygon, qreal bezier_flattening_threshold) const
  170. {
  171. QBezier beziers[32];
  172. beziers[0] = *this;
  173. QBezier *b = beziers;
  174. while (b >= beziers) {
  175. // check if we can pop the top bezier curve from the stack
  176. qreal y4y1 = b->y4 - b->y1;
  177. qreal x4x1 = b->x4 - b->x1;
  178. qreal l = qAbs(x4x1) + qAbs(y4y1);
  179. qreal d;
  180. if (l > 1.) {
  181. d = qAbs( (x4x1)*(b->y1 - b->y2) - (y4y1)*(b->x1 - b->x2) )
  182. + qAbs( (x4x1)*(b->y1 - b->y3) - (y4y1)*(b->x1 - b->x3) );
  183. } else {
  184. d = qAbs(b->x1 - b->x2) + qAbs(b->y1 - b->y2) +
  185. qAbs(b->x1 - b->x3) + qAbs(b->y1 - b->y3);
  186. l = 1.;
  187. }
  188. if (d < bezier_flattening_threshold*l || b == beziers + 31) {
  189. // good enough, we pop it off and add the endpoint
  190. polygon->append(QPointF(b->x4, b->y4));
  191. --b;
  192. } else {
  193. // split, second half of the polygon goes lower into the stack
  194. b->split(b+1, b);
  195. ++b;
  196. }
  197. }
  198. }
  199. QRectF QBezier::bounds() const
  200. {
  201. qreal xmin = x1;
  202. qreal xmax = x1;
  203. if (x2 < xmin)
  204. xmin = x2;
  205. else if (x2 > xmax)
  206. xmax = x2;
  207. if (x3 < xmin)
  208. xmin = x3;
  209. else if (x3 > xmax)
  210. xmax = x3;
  211. if (x4 < xmin)
  212. xmin = x4;
  213. else if (x4 > xmax)
  214. xmax = x4;
  215. qreal ymin = y1;
  216. qreal ymax = y1;
  217. if (y2 < ymin)
  218. ymin = y2;
  219. else if (y2 > ymax)
  220. ymax = y2;
  221. if (y3 < ymin)
  222. ymin = y3;
  223. else if (y3 > ymax)
  224. ymax = y3;
  225. if (y4 < ymin)
  226. ymin = y4;
  227. else if (y4 > ymax)
  228. ymax = y4;
  229. return QRectF(xmin, ymin, xmax-xmin, ymax-ymin);
  230. }
  231. enum ShiftResult {
  232. Ok,
  233. Discard,
  234. Split,
  235. Circle
  236. };
  237. static ShiftResult good_offset(const QBezier *b1, const QBezier *b2, qreal offset, qreal threshold)
  238. {
  239. const qreal o2 = offset*offset;
  240. const qreal max_dist_line = threshold*offset*offset;
  241. const qreal max_dist_normal = threshold*offset;
  242. const qreal spacing = qreal(0.25);
  243. for (qreal i = spacing; i < qreal(0.99); i += spacing) {
  244. QPointF p1 = b1->pointAt(i);
  245. QPointF p2 = b2->pointAt(i);
  246. qreal d = (p1.x() - p2.x())*(p1.x() - p2.x()) + (p1.y() - p2.y())*(p1.y() - p2.y());
  247. if (qAbs(d - o2) > max_dist_line)
  248. return Split;
  249. QPointF normalPoint = b1->normalVector(i);
  250. qreal l = qAbs(normalPoint.x()) + qAbs(normalPoint.y());
  251. if (l != qreal(0.0)) {
  252. d = qAbs( normalPoint.x()*(p1.y() - p2.y()) - normalPoint.y()*(p1.x() - p2.x()) ) / l;
  253. if (d > max_dist_normal)
  254. return Split;
  255. }
  256. }
  257. return Ok;
  258. }
  259. static ShiftResult shift(const QBezier *orig, QBezier *shifted, qreal offset, qreal threshold)
  260. {
  261. int map[4];
  262. bool p1_p2_equal = (orig->x1 == orig->x2 && orig->y1 == orig->y2);
  263. bool p2_p3_equal = (orig->x2 == orig->x3 && orig->y2 == orig->y3);
  264. bool p3_p4_equal = (orig->x3 == orig->x4 && orig->y3 == orig->y4);
  265. QPointF points[4];
  266. int np = 0;
  267. points[np] = QPointF(orig->x1, orig->y1);
  268. map[0] = 0;
  269. ++np;
  270. if (!p1_p2_equal) {
  271. points[np] = QPointF(orig->x2, orig->y2);
  272. ++np;
  273. }
  274. map[1] = np - 1;
  275. if (!p2_p3_equal) {
  276. points[np] = QPointF(orig->x3, orig->y3);
  277. ++np;
  278. }
  279. map[2] = np - 1;
  280. if (!p3_p4_equal) {
  281. points[np] = QPointF(orig->x4, orig->y4);
  282. ++np;
  283. }
  284. map[3] = np - 1;
  285. if (np == 1)
  286. return Discard;
  287. QRectF b = orig->bounds();
  288. if (np == 4 && b.width() < .1*offset && b.height() < .1*offset) {
  289. qreal l = (orig->x1 - orig->x2)*(orig->x1 - orig->x2) +
  290. (orig->y1 - orig->y2)*(orig->y1 - orig->y1) *
  291. (orig->x3 - orig->x4)*(orig->x3 - orig->x4) +
  292. (orig->y3 - orig->y4)*(orig->y3 - orig->y4);
  293. qreal dot = (orig->x1 - orig->x2)*(orig->x3 - orig->x4) +
  294. (orig->y1 - orig->y2)*(orig->y3 - orig->y4);
  295. if (dot < 0 && dot*dot < 0.8*l)
  296. // the points are close and reverse dirction. Approximate the whole
  297. // thing by a semi circle
  298. return Circle;
  299. }
  300. QPointF points_shifted[4];
  301. QLineF prev = QLineF(QPointF(), points[1] - points[0]);
  302. QPointF prev_normal = prev.normalVector().unitVector().p2();
  303. points_shifted[0] = points[0] + offset * prev_normal;
  304. for (int i = 1; i < np - 1; ++i) {
  305. QLineF next = QLineF(QPointF(), points[i + 1] - points[i]);
  306. QPointF next_normal = next.normalVector().unitVector().p2();
  307. QPointF normal_sum = prev_normal + next_normal;
  308. qreal r = qreal(1.0) + prev_normal.x() * next_normal.x()
  309. + prev_normal.y() * next_normal.y();
  310. if (qFuzzyIsNull(r)) {
  311. points_shifted[i] = points[i] + offset * prev_normal;
  312. } else {
  313. qreal k = offset / r;
  314. points_shifted[i] = points[i] + k * normal_sum;
  315. }
  316. prev_normal = next_normal;
  317. }
  318. points_shifted[np - 1] = points[np - 1] + offset * prev_normal;
  319. *shifted = QBezier::fromPoints(points_shifted[map[0]], points_shifted[map[1]],
  320. points_shifted[map[2]], points_shifted[map[3]]);
  321. return good_offset(orig, shifted, offset, threshold);
  322. }
  323. // This value is used to determine the length of control point vectors
  324. // when approximating arc segments as curves. The factor is multiplied
  325. // with the radius of the circle.
  326. #define KAPPA qreal(0.5522847498)
  327. static bool addCircle(const QBezier *b, qreal offset, QBezier *o)
  328. {
  329. QPointF normals[3];
  330. normals[0] = QPointF(b->y2 - b->y1, b->x1 - b->x2);
  331. qreal dist = qSqrt(normals[0].x()*normals[0].x() + normals[0].y()*normals[0].y());
  332. if (qFuzzyIsNull(dist))
  333. return false;
  334. normals[0] /= dist;
  335. normals[2] = QPointF(b->y4 - b->y3, b->x3 - b->x4);
  336. dist = qSqrt(normals[2].x()*normals[2].x() + normals[2].y()*normals[2].y());
  337. if (qFuzzyIsNull(dist))
  338. return false;
  339. normals[2] /= dist;
  340. normals[1] = QPointF(b->x1 - b->x2 - b->x3 + b->x4, b->y1 - b->y2 - b->y3 + b->y4);
  341. normals[1] /= -1*qSqrt(normals[1].x()*normals[1].x() + normals[1].y()*normals[1].y());
  342. qreal angles[2];
  343. qreal sign = 1.;
  344. for (int i = 0; i < 2; ++i) {
  345. qreal cos_a = normals[i].x()*normals[i+1].x() + normals[i].y()*normals[i+1].y();
  346. if (cos_a > 1.)
  347. cos_a = 1.;
  348. if (cos_a < -1.)
  349. cos_a = -1;
  350. angles[i] = qAcos(cos_a)/Q_PI;
  351. }
  352. if (angles[0] + angles[1] > 1.) {
  353. // more than 180 degrees
  354. normals[1] = -normals[1];
  355. angles[0] = 1. - angles[0];
  356. angles[1] = 1. - angles[1];
  357. sign = -1.;
  358. }
  359. QPointF circle[3];
  360. circle[0] = QPointF(b->x1, b->y1) + normals[0]*offset;
  361. circle[1] = QPointF(qreal(0.5)*(b->x1 + b->x4), qreal(0.5)*(b->y1 + b->y4)) + normals[1]*offset;
  362. circle[2] = QPointF(b->x4, b->y4) + normals[2]*offset;
  363. for (int i = 0; i < 2; ++i) {
  364. qreal kappa = qreal(2.0) * KAPPA * sign * offset * angles[i];
  365. o->x1 = circle[i].x();
  366. o->y1 = circle[i].y();
  367. o->x2 = circle[i].x() - normals[i].y()*kappa;
  368. o->y2 = circle[i].y() + normals[i].x()*kappa;
  369. o->x3 = circle[i+1].x() + normals[i+1].y()*kappa;
  370. o->y3 = circle[i+1].y() - normals[i+1].x()*kappa;
  371. o->x4 = circle[i+1].x();
  372. o->y4 = circle[i+1].y();
  373. ++o;
  374. }
  375. return true;
  376. }
  377. int QBezier::shifted(QBezier *curveSegments, int maxSegments, qreal offset, float threshold) const
  378. {
  379. Q_ASSERT(curveSegments);
  380. Q_ASSERT(maxSegments > 0);
  381. if (x1 == x2 && x1 == x3 && x1 == x4 &&
  382. y1 == y2 && y1 == y3 && y1 == y4)
  383. return 0;
  384. --maxSegments;
  385. QBezier beziers[10];
  386. redo:
  387. beziers[0] = *this;
  388. QBezier *b = beziers;
  389. QBezier *o = curveSegments;
  390. while (b >= beziers) {
  391. int stack_segments = b - beziers + 1;
  392. if ((stack_segments == 10) || (o - curveSegments == maxSegments - stack_segments)) {
  393. threshold *= qreal(1.5);
  394. if (threshold > qreal(2.0))
  395. goto give_up;
  396. goto redo;
  397. }
  398. ShiftResult res = shift(b, o, offset, threshold);
  399. if (res == Discard) {
  400. --b;
  401. } else if (res == Ok) {
  402. ++o;
  403. --b;
  404. continue;
  405. } else if (res == Circle && maxSegments - (o - curveSegments) >= 2) {
  406. // add semi circle
  407. if (addCircle(b, offset, o))
  408. o += 2;
  409. --b;
  410. } else {
  411. b->split(b+1, b);
  412. ++b;
  413. }
  414. }
  415. give_up:
  416. while (b >= beziers) {
  417. ShiftResult res = shift(b, o, offset, threshold);
  418. // if res isn't Ok or Split then *o is undefined
  419. if (res == Ok || res == Split)
  420. ++o;
  421. --b;
  422. }
  423. Q_ASSERT(o - curveSegments <= maxSegments);
  424. return o - curveSegments;
  425. }
  426. #ifdef QDEBUG_BEZIER
  427. static QDebug operator<<(QDebug dbg, const QBezier &bz)
  428. {
  429. dbg << '[' << bz.x1<< ", " << bz.y1 << "], "
  430. << '[' << bz.x2 <<", " << bz.y2 << "], "
  431. << '[' << bz.x3 <<", " << bz.y3 << "], "
  432. << '[' << bz.x4 <<", " << bz.y4 << ']';
  433. return dbg;
  434. }
  435. #endif
  436. static inline void splitBezierAt(const QBezier &bez, qreal t,
  437. QBezier *left, QBezier *right)
  438. {
  439. left->x1 = bez.x1;
  440. left->y1 = bez.y1;
  441. left->x2 = bez.x1 + t * ( bez.x2 - bez.x1 );
  442. left->y2 = bez.y1 + t * ( bez.y2 - bez.y1 );
  443. left->x3 = bez.x2 + t * ( bez.x3 - bez.x2 ); // temporary holding spot
  444. left->y3 = bez.y2 + t * ( bez.y3 - bez.y2 ); // temporary holding spot
  445. right->x3 = bez.x3 + t * ( bez.x4 - bez.x3 );
  446. right->y3 = bez.y3 + t * ( bez.y4 - bez.y3 );
  447. right->x2 = left->x3 + t * ( right->x3 - left->x3);
  448. right->y2 = left->y3 + t * ( right->y3 - left->y3);
  449. left->x3 = left->x2 + t * ( left->x3 - left->x2 );
  450. left->y3 = left->y2 + t * ( left->y3 - left->y2 );
  451. left->x4 = right->x1 = left->x3 + t * (right->x2 - left->x3);
  452. left->y4 = right->y1 = left->y3 + t * (right->y2 - left->y3);
  453. right->x4 = bez.x4;
  454. right->y4 = bez.y4;
  455. }
  456. qreal QBezier::length(qreal error) const
  457. {
  458. qreal length = qreal(0.0);
  459. addIfClose(&length, error);
  460. return length;
  461. }
  462. void QBezier::addIfClose(qreal *length, qreal error) const
  463. {
  464. QBezier left, right; /* bez poly splits */
  465. qreal len = qreal(0.0); /* arc length */
  466. qreal chord; /* chord length */
  467. len = len + QLineF(QPointF(x1, y1),QPointF(x2, y2)).length();
  468. len = len + QLineF(QPointF(x2, y2),QPointF(x3, y3)).length();
  469. len = len + QLineF(QPointF(x3, y3),QPointF(x4, y4)).length();
  470. chord = QLineF(QPointF(x1, y1),QPointF(x4, y4)).length();
  471. if((len-chord) > error) {
  472. split(&left, &right); /* split in two */
  473. left.addIfClose(length, error); /* try left side */
  474. right.addIfClose(length, error); /* try right side */
  475. return;
  476. }
  477. *length = *length + len;
  478. return;
  479. }
  480. qreal QBezier::tForY(qreal t0, qreal t1, qreal y) const
  481. {
  482. qreal py0 = pointAt(t0).y();
  483. qreal py1 = pointAt(t1).y();
  484. if (py0 > py1) {
  485. qSwap(py0, py1);
  486. qSwap(t0, t1);
  487. }
  488. Q_ASSERT(py0 <= py1);
  489. if (py0 >= y)
  490. return t0;
  491. else if (py1 <= y)
  492. return t1;
  493. Q_ASSERT(py0 < y && y < py1);
  494. qreal lt = t0;
  495. qreal dt;
  496. do {
  497. qreal t = qreal(0.5) * (t0 + t1);
  498. qreal a, b, c, d;
  499. QBezier::coefficients(t, a, b, c, d);
  500. qreal yt = a * y1 + b * y2 + c * y3 + d * y4;
  501. if (yt < y) {
  502. t0 = t;
  503. py0 = yt;
  504. } else {
  505. t1 = t;
  506. py1 = yt;
  507. }
  508. dt = lt - t;
  509. lt = t;
  510. } while (qAbs(dt) > qreal(1e-7));
  511. return t0;
  512. }
  513. int QBezier::stationaryYPoints(qreal &t0, qreal &t1) const
  514. {
  515. // y(t) = (1 - t)^3 * y1 + 3 * (1 - t)^2 * t * y2 + 3 * (1 - t) * t^2 * y3 + t^3 * y4
  516. // y'(t) = 3 * (-(1-2t+t^2) * y1 + (1 - 4 * t + 3 * t^2) * y2 + (2 * t - 3 * t^2) * y3 + t^2 * y4)
  517. // y'(t) = 3 * ((-y1 + 3 * y2 - 3 * y3 + y4)t^2 + (2 * y1 - 4 * y2 + 2 * y3)t + (-y1 + y2))
  518. const qreal a = -y1 + 3 * y2 - 3 * y3 + y4;
  519. const qreal b = 2 * y1 - 4 * y2 + 2 * y3;
  520. const qreal c = -y1 + y2;
  521. if (qFuzzyIsNull(a)) {
  522. if (qFuzzyIsNull(b))
  523. return 0;
  524. t0 = -c / b;
  525. return t0 > 0 && t0 < 1;
  526. }
  527. qreal reciprocal = b * b - 4 * a * c;
  528. if (qFuzzyIsNull(reciprocal)) {
  529. t0 = -b / (2 * a);
  530. return t0 > 0 && t0 < 1;
  531. } else if (reciprocal > 0) {
  532. qreal temp = qSqrt(reciprocal);
  533. t0 = (-b - temp)/(2*a);
  534. t1 = (-b + temp)/(2*a);
  535. if (t1 < t0)
  536. qSwap(t0, t1);
  537. int count = 0;
  538. qreal t[2] = { 0, 1 };
  539. if (t0 > 0 && t0 < 1)
  540. t[count++] = t0;
  541. if (t1 > 0 && t1 < 1)
  542. t[count++] = t1;
  543. t0 = t[0];
  544. t1 = t[1];
  545. return count;
  546. }
  547. return 0;
  548. }
  549. qreal QBezier::tAtLength(qreal l) const
  550. {
  551. qreal len = length();
  552. qreal t = qreal(1.0);
  553. const qreal error = qreal(0.01);
  554. if (l > len || qFuzzyCompare(l, len))
  555. return t;
  556. t *= qreal(0.5);
  557. //int iters = 0;
  558. //qDebug()<<"LEN is "<<l<<len;
  559. qreal lastBigger = qreal(1.0);
  560. while (1) {
  561. //qDebug()<<"\tt is "<<t;
  562. QBezier right = *this;
  563. QBezier left;
  564. right.parameterSplitLeft(t, &left);
  565. qreal lLen = left.length();
  566. if (qAbs(lLen - l) < error)
  567. break;
  568. if (lLen < l) {
  569. t += (lastBigger - t) * qreal(0.5);
  570. } else {
  571. lastBigger = t;
  572. t -= t * qreal(0.5);
  573. }
  574. //++iters;
  575. }
  576. //qDebug()<<"number of iters is "<<iters;
  577. return t;
  578. }
  579. QBezier QBezier::bezierOnInterval(qreal t0, qreal t1) const
  580. {
  581. if (t0 == 0 && t1 == 1)
  582. return *this;
  583. QBezier bezier = *this;
  584. QBezier result;
  585. bezier.parameterSplitLeft(t0, &result);
  586. qreal trueT = (t1-t0)/(1-t0);
  587. bezier.parameterSplitLeft(trueT, &result);
  588. return result;
  589. }
  590. QT_END_NAMESPACE