MathUtils: Add tangential interpolation

Reuse it in TaskProgress and in ProgressTimer.
Rename MathUtils::interpolate() into interpolateLinear()

Change-Id: Iff4cda1e3b8782cd26277ec75046ca5526be92c0
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Jarek Kobus
2022-11-25 18:34:28 +01:00
parent 6f299f19ac
commit 23f53dcbda
11 changed files with 124 additions and 23 deletions

View File

@@ -12,7 +12,7 @@ namespace Utils::MathUtils {
For x = x1 it returns y1.
For x = x2 it returns y2.
*/
int interpolate(int x, int x1, int x2, int y1, int y2)
int interpolateLinear(int x, int x1, int x2, int y1, int y2)
{
if (x1 == x2)
return y1; // or the middle point between y1 and y2?
@@ -27,4 +27,21 @@ int interpolate(int x, int x1, int x2, int y1, int y2)
return qRound((double)numerator / denominator);
}
/*!
Tangential interpolation:
For x = 0 it returns y1.
For x = xHalfLife it returns 50 % of the distance between y1 and y2.
For x = infinity it returns y2.
*/
int interpolateTangential(int x, int xHalfLife, int y1, int y2)
{
if (x == 0)
return y1;
if (y1 == y2)
return y1;
const double mapped = atan2(double(x), double(xHalfLife));
const double progress = y1 + (y2 - y1) * mapped * 2 / M_PI;
return qRound(progress);
}
} // namespace Utils::Math