Scroom 0.14-49-gb7ae7a6d
Loading...
Searching...
No Matches
RulerCalculations Class Reference

#include <ruler.hh>

Collaboration diagram for RulerCalculations:
Collaboration graph

Static Public Member Functions

static int calculateInterval (double lower, double upper, double allocatedSize)
 
static int intervalPixelSpacing (double interval, double lower, double upper, double allocatedSize)
 
static int firstTick (double lower, int interval)
 
static double scaleToRange (double x, double src_lower, double src_upper, double dest_lower, double dest_upper)
 

Static Public Attributes

static constexpr int MIN_SPACE_MAJORTICKS {80}
 

Static Private Attributes

static constexpr std::array< int, 4 > VALID_INTERVALS {1, 5, 10, 25}
 

Detailed Description

This class contains the functions a Ruler uses to calculate the interval between major ticks.

Member Function Documentation

◆ calculateInterval()

int RulerCalculations::calculateInterval ( double  lower,
double  upper,
double  allocatedSize 
)
static

Calculates an appropriate interval between major ticks on a ruler.

Parameters
lowerLower limit of the ruler range. Must be strictly less than upper.
upperUpper limit of the ruler range. Must be strictly greater than lower.
allocatedSizeThe allocated width/height in pixels for the ruler.
Returns
The interval between ticks, or -1 if the given range is invalid.
245{
246 // We need to calculate the distance between the largest ticks on the ruler
247 // We will try each interval x * 10^n for x in VALID_INTERVALS and integer n >= 0
248 // from smallest to largest until we find an interval which will produce a
249 // spacing of a large enough width/height when drawn
250
251 require(upper > lower);
252 require(upper - lower >= 1);
253 require(allocatedSize > 0);
254
255 // Index in the ruler's VALID_INTERVALS array
256 int intervalIndex = 0;
257 // Each interval is multiplied by 10 raised to a power n
258 const int INTERVAL_BASE = 10;
259 int intervalN = 0;
260
261 // The interval to be returned
262 int interval = 1;
263
264 while(true)
265 {
266 interval = floor(VALID_INTERVALS.at(intervalIndex) * pow(INTERVAL_BASE, intervalN));
267
268 // Calculate the drawn size for this interval by mapping from the ruler range
269 // to the ruler size on the screen
270 const double spacing = intervalPixelSpacing(interval, lower, upper, allocatedSize);
271 if(spacing < 0)
272 {
273 return -1;
274 }
275 // If we've found a segment of appropriate size, we can stop
276 if(spacing >= MIN_SPACE_MAJORTICKS)
277 {
278 break;
279 }
280
281 // Otherwise, try the next interval
282 intervalIndex++;
283 if(static_cast<unsigned int>(intervalIndex) == VALID_INTERVALS.size())
284 {
285 // We tried all intervals for the current n, increment n
286 intervalIndex = 0;
287 intervalN++;
288 }
289 }
290
291 return interval;
292}
#define require(expr)
Definition assertions.hh:30
static constexpr std::array< int, 4 > VALID_INTERVALS
Definition ruler.hh:181
static constexpr int MIN_SPACE_MAJORTICKS
Definition ruler.hh:185
static int intervalPixelSpacing(double interval, double lower, double upper, double allocatedSize)
Definition ruler.cc:294

Referenced by testCorrectIntervalForMinWidth(), and Ruler::updateMajorTickInterval().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ firstTick()

int RulerCalculations::firstTick ( double  lower,
int  interval 
)
static

Returns the position in the ruler range to start drawing from.

Parameters
lowerThe lower limit of the ruler range.
intervalThe interval between major ticks that the ruler will be drawn with.
Returns
The position of in the ruler to start drawing from.
303{ return static_cast<int>(floor(lower / interval)) * interval; }

Referenced by Ruler::draw().

Here is the caller graph for this function:

◆ intervalPixelSpacing()

int RulerCalculations::intervalPixelSpacing ( double  interval,
double  lower,
double  upper,
double  allocatedSize 
)
static

Calculates the spacing in pixels between tick marks for a given interval.

Parameters
intervalThe interval to calculate the spacing for.
lowerLower limit of the ruler range. Must be strictly less than upper.
upperUpper limit of the ruler range. Must be strictly greater than lower.
allocatedSizeThe allocated width/height in pixels for the ruler. Must be greater than 0.
Returns
The spacing in pixels between tick marks for a given interval, or -1 if the given range is invalid.
295{
296 require(upper > lower);
297 require(allocatedSize > 0);
298
299 const double RANGE_SIZE = upper - lower;
300 return floor((allocatedSize / RANGE_SIZE) * interval);
301}

Referenced by calculateInterval(), and Ruler::updateMajorTickInterval().

Here is the caller graph for this function:

◆ scaleToRange()

double RulerCalculations::scaleToRange ( double  x,
double  src_lower,
double  src_upper,
double  dest_lower,
double  dest_upper 
)
static

Scales a number x in the range [src_lower, src_upper] to the range [dest_lower, dest_upper]. Used to scale from the ruler range to the drawing space.

Parameters
xThe number to scale.
src_lowerThe lower limit of the source range. Inclusive.
src_upperThe upper limit of the source range. Inclusive.
dest_lowerThe lower limit of the destination range. Inclusive.
dest_upperThe upper limit of the destination range. Inclusive.
Returns
The result of x scaled from range source to dest.
236{
237 const double src_size = src_upper - src_lower;
238 const double dest_size = dest_upper - dest_lower;
239 const double scale = dest_size / src_size;
240
241 return dest_lower + round(scale * (x - src_lower));
242}

Referenced by Ruler::drawTicks().

Here is the caller graph for this function:

Member Data Documentation

◆ MIN_SPACE_MAJORTICKS

constexpr int RulerCalculations::MIN_SPACE_MAJORTICKS {80}
staticconstexpr

The minimum space between major ticks.

185{80};

Referenced by calculateInterval(), and testCorrectIntervalForMinWidth().

◆ VALID_INTERVALS

constexpr std::array<int, 4> RulerCalculations::VALID_INTERVALS {1, 5, 10, 25}
staticconstexprprivate

Valid intervals between major ticks.

181{1, 5, 10, 25};

Referenced by calculateInterval().


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