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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-11-14 17:39:33 +01:00 committed by Dirk Hohndel
parent 8ff949650a
commit afb0978460

View file

@ -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<double, double> getLineFromTo(const QLineF &l, bool horizontal)