From afb09784602fa92c64511ada3c39288fbd54ea31 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 14 Nov 2021 17:39:33 +0100 Subject: [PATCH] profile: shorten DiveCartesianAxis::posAtValue(qreal value) This function was just needlessly complicated. For one, it considered the position of the line, but that is never changed since redoing the positioning code. Moreover, it does in lots of lines what is a very idiomatic operation: a one-dimensional affine transformation. Let's shorten the actual calculation to two lines. Signed-off-by: Berthold Stoeger --- profile-widget/divecartesianaxis.cpp | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/profile-widget/divecartesianaxis.cpp b/profile-widget/divecartesianaxis.cpp index 753100521..1515b78d6 100644 --- a/profile-widget/divecartesianaxis.cpp +++ b/profile-widget/divecartesianaxis.cpp @@ -365,27 +365,15 @@ qreal DiveCartesianAxis::valueAt(const QPointF &p) const qreal DiveCartesianAxis::posAtValue(qreal value) const { QLineF m = line(); - QPointF p = pos(); - double size = max - min; - // unused for now: - // double distanceFromOrigin = value - min; - double percent = IS_FP_SAME(min, max) ? 0.0 : (value - min) / size; - - - double realSize = position == Position::Bottom ? - m.x2() - m.x1() : - m.y2() - m.y1(); - - // Inverted axis, just invert the percentage. + double screenFrom = position == Position::Bottom ? m.x1() : m.y1(); + double screenTo = position == Position::Bottom ? m.x2() : m.y2(); + if (IS_FP_SAME(min, max)) + return (screenFrom + screenTo) / 2.0; if ((position == Position::Bottom) == inverted) - percent = 1.0 - percent; - - double retValue = realSize * percent; - double adjusted = position == Position::Bottom ? - retValue + m.x1() + p.x() : - retValue + m.y1() + p.y(); - return adjusted; + std::swap(screenFrom, screenTo); + return (value - min) / (max - min) * + (screenTo - screenFrom) + screenFrom; } static std::pair getLineFromTo(const QLineF &l, bool horizontal)