From 22cd533a07921d23ae7a0044b124a13179221083 Mon Sep 17 00:00:00 2001 From: Peter Hartmann Date: Thu, 29 Jun 2023 13:37:16 +0200 Subject: [PATCH] scale engine: Try harder to find a proper step size If the maxSteps don't fit into the interval, we immediately returned a base value. However, we could try more often with less steps before falling back to that base value, giving better results when the original steps value doesnt' fit. --- src/common/QskScaleEngine.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/common/QskScaleEngine.cpp b/src/common/QskScaleEngine.cpp index 57742c79..dd8efc08 100644 --- a/src/common/QskScaleEngine.cpp +++ b/src/common/QskScaleEngine.cpp @@ -102,23 +102,29 @@ namespace { double minorStepSize( double intervalSize, int maxSteps ) { - const double minStep = divideInterval( intervalSize, maxSteps ); + int steps = maxSteps; - if ( minStep != 0.0 ) + while( steps > 0 ) { - // # ticks per interval - const int numTicks = qCeil( qAbs( intervalSize / minStep ) ) - 1; + const double minStep = divideInterval( intervalSize, steps ); - // Do the minor steps fit into the interval? - if ( fuzzyCompare( ( numTicks + 1 ) * qAbs( minStep ), - qAbs( intervalSize ), intervalSize ) > 0 ) + if ( minStep != 0.0 ) { - // The minor steps doesn't fit into the interval - return 0.5 * intervalSize; + // # ticks per interval + const int numTicks = qCeil( qAbs( intervalSize / minStep ) ) - 1; + + // Do the minor steps fit into the interval? + if ( fuzzyCompare( ( numTicks + 1 ) * qAbs( minStep ), + qAbs( intervalSize ), intervalSize ) <= 0 ) + { + return minStep; + } } + + steps--; } - return minStep; + return 0.5 * intervalSize; } }