lundi 14 juin 2021

How to interpolate a value in one range into another

I have 2 double values which can go from let's say start value 62.243 to end value 79.495. I have another 2 double values which can go from let's say start value 4.456 to end value 7.687.

double start_1 = 62.243;
double end_1 = 79.495;

double start_2 = 4.456;
double end_2 = 7.687;

Now if I a value picked between start_1 and end_1, I want to interpolate the same value to a value between start_2 and end_2.

Question:
How do implement the below function in C++ which can do what I described above?

double InterpolateAValue(const double start_1, const double end_1, const double start_2, const double end_2, double value_between_start_1_and_end_1) {
     // How to get a value between start_2 and end_2 which matches the position of value_between_start_1_and_end_1 in the range of `start_1` and `end_1`
};

std::lerp seems to be doing something like what I need but not exactly. I am on C++11. I have taken double as the type here. I could have chosen floats as well but a wish to see how doubles affect the math is also a part of the question.

Aucun commentaire:

Enregistrer un commentaire