From 9f7824cd17902686c812e4ad6d365d7e13dc76ec Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Mon, 10 Jan 2022 08:44:02 +0100 Subject: [PATCH] minor improvements --- src/common/QskScaleEngine.cpp | 35 ++++++++++++++++++++--------------- src/common/QskScaleEngine.h | 2 ++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/common/QskScaleEngine.cpp b/src/common/QskScaleEngine.cpp index 303e4e03..dfc686bb 100644 --- a/src/common/QskScaleEngine.cpp +++ b/src/common/QskScaleEngine.cpp @@ -64,21 +64,13 @@ namespace return std::floor( value ) * intervalSize; } - double divideEps( double intervalSize, double numSteps ) - { - if ( numSteps == 0.0 || intervalSize == 0.0 ) - return 0.0; - - return ( intervalSize - ( _eps * intervalSize ) ) / numSteps; - } - double divideInterval( double intervalSize, int numSteps ) { if ( numSteps <= 0 ) return 0.0; - const auto v = divideEps( intervalSize, numSteps ); - if ( v == 0.0 ) + const auto v = intervalSize / numSteps; + if ( qFuzzyIsNull( v ) ) return 0.0; constexpr double base = 10.0; @@ -89,14 +81,19 @@ namespace const double fraction = std::pow( base, lx - p ); - uint n = base; - while ( ( n > 1 ) && ( fraction <= n / 2 ) ) - n /= 2; - - double stepSize = n * std::pow( base, p ); + double stepSize = std::pow( base, p ); if ( v < 0 ) stepSize = -stepSize; + for ( const double f : { 2.0, 2.5, 5.0, 10.0 } ) + { + if ( fraction <= f || qFuzzyCompare( fraction, f ) ) + { + stepSize *= f; + break; + } + } + return stepSize; } } @@ -355,4 +352,12 @@ void QskScaleEngine::buildMinorTicks( } } +qreal QskScaleEngine::alignedStepSize( double intervalSize, int numSteps ) const +{ + if ( intervalSize <= 0.0 ) + return 0.0; + + return divideInterval( intervalSize, numSteps ); +} + #include "moc_QskScaleEngine.cpp" diff --git a/src/common/QskScaleEngine.h b/src/common/QskScaleEngine.h index 2111816f..10ae1418 100644 --- a/src/common/QskScaleEngine.h +++ b/src/common/QskScaleEngine.h @@ -40,6 +40,8 @@ class QSK_EXPORT QskScaleEngine void autoScale( int maxNumSteps, qreal& x1, qreal& x2, qreal& stepSize ) const; + qreal alignedStepSize( double intervalSize, int numSteps ) const; + private: QskIntervalF align( const QskIntervalF&, qreal stepSize ) const;