I've inherited maintenance of a function that takes as parameter a value between 0 and 65535 (inclusive):
MyClass::mappingFunction(unsigned short headingIndex);
headingIndex can be converted to degrees using the following formula: degrees = headingIndex * 360 / 65536
The role of this function is to translate the headingIndex into 1 of 36 symbols representing various degrees of rotation, i.e. there is a symbol for 10 degrees, a symbol for 20 degrees etc, up to 360 degrees in units of 10 degrees.
A headingIndex of 0 would translate to displaying the 0 (360) degree symbol.
The function performs the following which I can't seem to get my head around:
const int MAX_INTEGER = 65536;
const int NUM_SYMBOLS = 36;
int symbolRange = NUM_SYMBOLS - 1;
int roundAmount = MAX_INTEGER / (symbolRange + 1) - 1;
int roundedIndex = headingIndex + roundAmount;
int symbol = (symbolRange * roundedIndex) / MAX_INTEGER;
I'm confused about the algorithm that is being used here, specifically with regard to the following:
- The intention behind
roundAmount? I understand it is essentially dividing the maximum input range into discrete chunks but to then add it on to theheadingIndexseems a strange thing to do. roundedIndexis then the original value now offset or rotated by some offset in a clockwise direction?
The algorithm produces results such as:
headingIndex of 0 --> symbol 0
headingIndex of 100 --> symbol 1
headingIndex of 65500 --> symbol 35
I'm thinking there must be a better way of doing this?