From 5f0687ca48ea65d098538d126f8482c97bb8d2ee Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 30 Aug 2019 13:38:36 +0200 Subject: [PATCH] Replaces QPoint with QPointF, Uses .length instead of pythagoras, Uses setOpacity instead of pen.setColor --- osciwidget.cpp | 35 +++++++++++++---------------------- osciwidget.h | 2 +- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/osciwidget.cpp b/osciwidget.cpp index 3e7c07d..330a64a 100644 --- a/osciwidget.cpp +++ b/osciwidget.cpp @@ -4,19 +4,6 @@ #include -namespace { -template -auto pythagoras(const T &dx, const T &dy) -{ - return std::sqrt(dx * dx + dy * dy); -} - -auto pythagoras(const QLine &line) -{ - return pythagoras(line.dx(), line.dy()); -} -} - OsciWidget::OsciWidget(QWidget *parent) : QWidget(parent) { @@ -74,22 +61,26 @@ void OsciWidget::renderSamples(const SamplePair *begin, const SamplePair *end) QPen pen; pen.setWidth(2); + pen.setColor(QColor(0, 255, 0)); + painter.setPen(pen); + + // Paint from center + painter.translate(width()/2, height()/2); for (auto i = begin; i < end; i++) { - const qint32 x = (float(i->x) / std::numeric_limits::max() / 2 * width() / 2 * m_factor) + (width() / 2); - const qint32 y = (float(-i->y) / std::numeric_limits::max() / 2 * height() / 2 * m_factor) + (height() / 2); - const QPoint p{x,y}; + const qreal x = (qreal(i->x) / std::numeric_limits::max() / 2 * width() / 2 * m_factor); + const qreal y = (qreal(i->y) / std::numeric_limits::max() / 2 * height() / 2 * m_factor); + const QPointF p{x, y}; if (Q_LIKELY(m_lastPoint.has_value())) { - auto dist = pythagoras(QLine(*m_lastPoint, p)); - if (dist < 1) - dist = 1; - - pen.setColor(QColor(0, 1./dist*255, 0)); - painter.setPen(pen); + auto speed = QLineF(*m_lastPoint, p).length(); + if (speed < 1) + speed = 1; + auto brightness = 1./speed; + painter.setOpacity(brightness); painter.drawLine(*m_lastPoint, p); } diff --git a/osciwidget.h b/osciwidget.h index 98740d7..70641f1 100644 --- a/osciwidget.h +++ b/osciwidget.h @@ -49,5 +49,5 @@ private: QPixmap m_pixmap; - std::optional m_lastPoint; + std::optional m_lastPoint; };