Engauge Digitizer 2
Loading...
Searching...
No Matches
ExportAlignLinear Class Reference

Pick first simplest x value between specified min and max, for linear scaling. More...

#include <ExportAlignLinear.h>

Collaboration diagram for ExportAlignLinear:
Collaboration graph

Public Member Functions

 ExportAlignLinear (double xMin, double xMax)
 Single constructor.
double firstSimplestNumber () const
 Result.

Detailed Description

Pick first simplest x value between specified min and max, for linear scaling.

A simplest value is defined here as one having the smallest number of significant digits, and is used for aligning periodic values on simple numbers.

A comprehensive set of examples is given in TestExportAlign

Definition at line 15 of file ExportAlignLinear.h.

Constructor & Destructor Documentation

◆ ExportAlignLinear()

ExportAlignLinear::ExportAlignLinear ( double xMin,
double xMax )

Single constructor.

Definition at line 13 of file ExportAlignLinear.cpp.

14 :
15 m_firstSimplestNumber (0)
16{
17 ENGAUGE_ASSERT (xMin <= xMax);
18
19 const int MAX_S = 7; // This algorithm is probably constrained to the 7 digits of precision of real numbers
20
21 // Start by identifying the first digit D for the larger magnitude (=unsigned) of xMin and xMax. Examples
22 // are D=0 for 0.1 to 0.99..., D=1 for 1 to 9.99..., D=2 for 10 to 99.999...
23 double absMin = qAbs (xMin);
24 double absMax = qAbs (xMax);
25 int D = qFloor (log10 (qMax (absMin, absMax)) + EPSILON);
26
27 // Number of significant digits S will start at 1 and then get increased until
28 // rounded(xMin) + 10^R <= xMax
29 // where rounded(xMin) is xMin rounded off to the the Rth digit, where R=D-S
30 int S = 1;
31 double roundedMin = 0.0;
32 while (S < MAX_S) {
33 int R = D - S + 1;
34 roundedMin = roundToDigit (xMin, R);
35 double nominalStep = qPow (10.0, R);
36
37 if (xMin < 0 && xMin < roundedMin) {
38 // For negative xMin we often or always have to jump over by nominalStep to keep
39 // xMin in the range
40 roundedMin -= nominalStep;
41 }
42
43 // Need at least one step between roundedMin and xMax, otherwise ranges
44 if (roundedMin + 2 * nominalStep <= xMax) {
45 break;
46 }
47
48 S = S + 1;
49 }
50
51 m_firstSimplestNumber = roundedMin;
52}
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT.
const double EPSILON

Member Function Documentation

◆ firstSimplestNumber()

double ExportAlignLinear::firstSimplestNumber ( ) const

Result.

Definition at line 54 of file ExportAlignLinear.cpp.

55{
56 return m_firstSimplestNumber;
57}

The documentation for this class was generated from the following files: