apply curve smoothing to drawn lines
Lint & Format (JS/TS) / lint-format (pull_request) Successful in 10s
Python / lint-format (pull_request) Successful in 17s
Python / test (pull_request) Successful in 29s
Lint & Format (Rust) / lint-format (pull_request) Successful in 1m10s

This commit is contained in:
2026-06-20 20:09:03 +02:00
parent e619632077
commit 8a87f86758
3 changed files with 46 additions and 10 deletions
+24 -2
View File
@@ -97,9 +97,31 @@ void StrokeCanvasItem::beginStroke(qreal x, qreal y) {
}
void StrokeCanvasItem::appendPoint(qreal x, qreal y) {
if (shouldAddPoint(m_currentStroke.points, {x, y}, 2.0))
m_currentStroke.points.append({x, y});
const QPointF incoming{x, y};
if (!shouldAddPoint(m_currentStroke.points, incoming, 2.0))
return;
QPointF smoothed;
if (m_currentStroke.points.isEmpty()) {
smoothed = incoming;
} else {
const QPointF last = m_currentStroke.points.last();
const QPointF delta = incoming - last;
const qreal dist = std::sqrt(QPointF::dotProduct(delta, delta));
constexpr qreal minDist = 6.0;
constexpr qreal maxDist = 20.0;
constexpr qreal minAlpha = 0.1;
constexpr qreal maxAlpha = 0.3;
const qreal t = std::clamp((dist - minDist) / (maxDist - minDist), 0.0, 1.0);
const qreal alpha = minAlpha + t * (maxAlpha - minAlpha);
smoothed = last * (1.0 - alpha) + incoming * alpha;
}
m_currentStroke.points.append(smoothed);
update();
}