So I have this great number mapping function:
double map(double input, double input_start, double input_end, double output_start, double output_end)
{
/* Note, "slope" below is a constant for given numbers, so if you are calculating
a lot of output values, it makes sense to calculate it once. It also makes
understanding the code easier */
double slope = (output_end - output_start) / (input_end - input_start);
return output_start + slope * (input - input_start);
}
Courtesy of Alok Singhal: Mapping a numeric range onto another
And he is correct, I call the function many many times, but usually with the same input and output start/end arguments.
What do you think is the cleanest/simplest solution to having slope
calculated once, I can think of many ideas (such as just making a Map class and have slope calculated in the constructor) But I am new to programming, and usually my ideas are not the best! The most ultimate, probably would be that the function itself knows if slope needs to be recalculated, but that might get messy quickly. I dunno!