1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <QtLottie/private/qbeziereasing_p.h>
5
6QT_BEGIN_NAMESPACE
7
8void QBezierEasing::addCubicBezierSegment(const QPointF &c1, const QPointF &c2, const QPointF &endPoint)
9{
10 mBezier = QBezier::fromPoints(p1: QPointF(0.0, 0.0), p2: c1, p3: c2, p4: endPoint);
11}
12
13qreal QBezierEasing::valueForProgress(qreal progress) const
14{
15 return mBezier.pointAt(t: tForX(x: progress)).y();
16}
17
18qreal QBezierEasing::tForX(qreal x) const
19{
20 if (x <= 0.0)
21 return 0.0;
22 else if (x >= 1.0)
23 return 1.0;
24
25 qreal t0 = 0.0;
26 qreal t1 = 1.0;
27
28 for (int i = 0; i < 10; i++) { // 10 iterations gives error smaller than 0.001
29 qreal t = qreal(0.5) * (t0 + t1);
30 qreal a, b, c, d;
31 QBezier::coefficients(t, a, b, c, d);
32 qreal xt = a * mBezier.x1 + b * mBezier.x2 + c * mBezier.x3 + d * mBezier.x4;
33 if (xt < x)
34 t0 = t;
35 else
36 t1 = t;
37 }
38
39 return t0;
40}
41
42QT_END_NAMESPACE
43

source code of qtlottie/src/lottie/qbeziereasing.cpp