1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| #include "SmoothCurveGenerator.h" #include <QtMath>
QPainterPath SmoothCurveGenerator::generateSmoothCurve(QList<QPointF> points, bool closed, double tension, int numberOfSegments) { QList<double> ps;
foreach (QPointF p, points) { ps << p.x() << p.y(); }
return SmoothCurveGenerator::generateSmoothCurve(ps, closed, tension, numberOfSegments); }
QPainterPath SmoothCurveGenerator::generateSmoothCurve(QList<double> points, bool closed, double tension, int numberOfSegments) { QList<double> ps(points); QList<double> result; double x, y; double t1x, t2x, t1y, t2y; double c1, c2, c3, c4; double st;
if (closed) { ps.prepend(points[points.length() - 1]); ps.prepend(points[points.length() - 2]); ps.prepend(points[points.length() - 1]); ps.prepend(points[points.length() - 2]); ps.append(points[0]); ps.append(points[1]); } else { ps.prepend(points[1]); ps.prepend(points[0]); ps.append(points[points.length() - 2]); ps.append(points[points.length() - 1]); }
for (int i = 2; i < (ps.length() - 4); i += 2) { t1x = (ps[i + 2] - ps[i - 2]) * tension; t2x = (ps[i + 4] - ps[i - 0]) * tension; t1y = (ps[i + 3] - ps[i - 1]) * tension; t2y = (ps[i + 5] - ps[i + 1]) * tension;
for (int t = 0; t <= numberOfSegments; t++) { st = (double)t / (double)numberOfSegments;
c1 = 2 * qPow(st, 3) - 3 * qPow(st, 2) + 1; c2 = -2 * qPow(st, 3) + 3 * qPow(st, 2); c3 = qPow(st, 3) - 2 * qPow(st, 2) + st; c4 = qPow(st, 3) - qPow(st, 2);
x = c1 * ps[i] + c2 * ps[i + 2] + c3 * t1x + c4 * t2x; y = c1 * ps[i + 1] + c2 * ps[i + 3] + c3 * t1y + c4 * t2y;
result << x << y; } }
QPainterPath path; path.moveTo(result[0], result[1]); for (int i = 2; i < result.length() - 2; i += 2) { path.lineTo(result[i+0], result[i+1]); }
if (closed) { path.closeSubpath(); }
return path; }
|