-QPointF CubicBezierSpline::point(double t, const QList< QPointF >& points)
-{
- /*
- * Calculating a point on the bezier curve using the coefficients from Bernstein basis polynomial of degree 3.
- * Using the De Casteljau algorithm would be slightly faster for when calculating a lot of values
- * but the difference is far from noticable in this needcase
- */
- double c1 = (1-t) * (1-t) * (1-t);
- double c2 = 3 * t * (1-t) * (1-t);
- double c3 = 3 * t * t * (1-t);
- double c4 = t * t * t;
-
- return QPointF(points[0].x()*c1 + points[1].x()*c2 + points[2].x()*c3 + points[3].x()*c4,
- points[0].y()*c1 + points[1].y()*c2 + points[2].y()*c3 + points[3].y()*c4);
-}
-
-void CubicBezierSpline::update()
-{
- if (m_validSpline)
- return;
-
- m_validSpline = true;
- m_spline.clear();
-
- QList <QPointF> points;
- QPointF p;
- for (int i = 0; i < m_points.count() - 1; ++i) {
- points.clear();
- points << m_points.at(i).p
- << m_points.at(i).h2
- << m_points.at(i+1).h1
- << m_points.at(i+1).p;
-
- int numberOfValues = (int)((points[3].x() - points[0].x()) * m_precision * 10);
- if (numberOfValues == 0)
- numberOfValues = 1;
- double step = 1 / (double)numberOfValues;
- double t = 0;
- while (t <= 1) {
- p = point(t, points);
- m_spline.insert(p.x(), p.y());
- t += step;
- }
- }
-}
-