#include "cubicbezierspline.h"
-#include <KDebug>
+/** @brief For sorting a Bezier spline. Whether a is before b. */
static bool pointLessThan(const BPoint &a, const BPoint &b)
{
return a.p.x() < b.p.x();
m_validSpline(false),
m_precision(100)
{
- BPoint start;
- start.p.setX(0);
- start.p.setY(0);
- start.h1.setX(0);
- start.h1.setY(0);
- start.h2.setX(0.1);
- start.h2.setY(0.1);
- m_points.append(start);
-
- BPoint end;
- end.p.setX(1);
- end.p.setY(1);
- end.h1.setX(0.9);
- end.h1.setY(0.9);
- end.h2.setX(1);
- end.h2.setY(1);
- m_points.append(end);
+ m_points.append(BPoint(QPointF(0, 0), QPointF(0, 0), QPointF(.1, .1)));
+ m_points.append(BPoint(QPointF(.9, .9), QPointF(1, 1), QPointF(1, 1)));
}
CubicBezierSpline::CubicBezierSpline(const CubicBezierSpline& spline, QObject* parent) :
{
m_precision = spline.m_precision;
m_points = spline.m_points;
+ m_validSpline = false;
return *this;
}
values.append(QPointF(xy.at(0).toDouble(), xy.at(1).toDouble()));
}
if (values.count() == 3) {
- BPoint bp;
- bp.h1 = values.at(0);
- bp.p = values.at(1);
- bp.h2 = values.at(2);
- m_points.append(bp);
+ m_points.append(BPoint(values.at(0), values.at(1), values.at(2)));
}
}
{
QStringList spline;
foreach(const BPoint &p, m_points) {
- spline << (QString::number(p.h1.x()) + ";" + QString::number(p.h1.y())
- + "#" + QString::number(p.p.x()) + ";" + QString::number(p.p.y())
- + "#" + QString::number(p.h2.x()) + ";" + QString::number(p.h2.y()));
+ spline << QString("%1;%2#%3;%4#%5;%6").arg(p.h1.x()).arg(p.h1.y())
+ .arg(p.p.x()).arg(p.p.y())
+ .arg(p.h2.x()).arg(p.h2.y());
}
return spline.join("|");
}
keepSorted();
validatePoints();
m_validSpline = false;
- return m_points.indexOf(point); // in case it changed
+ return indexOf(point); // in case it changed
}
QList <BPoint> CubicBezierSpline::points()
keepSorted();
validatePoints();
m_validSpline = false;
- if (m_points.indexOf(point) == -1) {
- // point changed during validation process
- for (int i = 0; i < m_points.count(); ++i) {
- // this condition should be sufficient, too
- if (m_points.at(i).p == point.p)
- return i;
- }
- } else {
- return m_points.indexOf(point);
- }
+ return indexOf(point);
}
void CubicBezierSpline::setPrecision(int pre)
{
update();
- //x = qBound(m_spline.constBegin().key(), x, m_spline.constEnd().key());
- //kDebug() << "....x" << x<<"bounddown"<<m_spline.constBegin().key()<<"up"<<m_spline.constEnd().key();
-
if (!cont)
m_i = m_spline.constBegin();
if (m_i != m_spline.constBegin())
<< m_points.at(i+1).h1
<< m_points.at(i+1).p;
- int numberOfValues = (int)((points[3].x() - points[0].x()) * m_precision * 2);
+ int numberOfValues = (int)((points[3].x() - points[0].x()) * m_precision * 5);
if (numberOfValues == 0)
numberOfValues = 1;
double step = 1 / (double)numberOfValues;
t += step;
}
}
- /*QMap<double, double>::const_iterator i = m_spline.constBegin();
- kDebug()<<"////////////spline/////////////";
- while (i != m_spline.constEnd()) {
- kDebug() << i.key() << i.value();
- ++i;
+}
+
+int CubicBezierSpline::indexOf(const BPoint& p)
+{
+ if (m_points.indexOf(p) == -1) {
+ // point changed during validation process
+ for (int i = 0; i < m_points.count(); ++i) {
+ // this condition should be sufficient, too
+ if (m_points.at(i).p == p.p)
+ return i;
+ }
+ } else {
+ return m_points.indexOf(p);
}
- kDebug()<<"////////////spline/////////////end";*/
+ return -1;
}
#include "cubicbezierspline.moc"