Core: Linear interpolation (Lerp) templates and reference example for (numbers, Point Size and Rect) (#215)

This commit is contained in:
İsmail Yılmaz 2024-12-09 10:36:27 +03:00 committed by GitHub
parent b99c26b267
commit ce5ee8f82f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 60 additions and 0 deletions

View file

@ -0,0 +1,19 @@
#include <Core/Core.h>
using namespace Upp;
// Generic Lerp and specializations
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_COUT);
DUMP(Lerp(10, 20, 0.5));
DUMP(Lerp(10.0, 20.0, 0.25));
DUMP(Lerp(Point(10, 20), Point(30, 40), 0.5));
DUMP(Lerp(Size(100, 200), Size(300, 400), 0.5));
DUMP(Lerp(Rect(0, 0, 100, 100), Rect(100, 100, 200, 200), 0.5));
}

View file

@ -0,0 +1,11 @@
description "Demonstrates the linear interpolation templates.\377";
uses
Core;
file
LinearInterpolation.cpp;
mainconfig
"" = "";

View file

@ -720,6 +720,36 @@ Pointf Normalize(const Pointf& p);
Pointf Polar(double a);
Pointf Polar(const Pointf& p, double r, double a);
// Linear interopolation templates
template <typename T>
inline T Lerp(T a, T b, double t)
{
return a + t * (b - a);
}
template <typename T>
inline Point_<T> Lerp(Point_<T> a, Point_<T> b, double t)
{
return Point_<T>(Lerp(a.x, b.x, t), Lerp(a.y, b.y, t));
}
template <typename T>
inline Size_<T> Lerp(Size_<T> a, Size_<T> b, double t)
{
return Size_<T>(Lerp(a.cx, b.cx, t), Lerp(a.cy, b.cy, t));
}
template <typename T>
inline Rect_<T> Lerp(Rect_<T> a, Rect_<T> b, double t)
{
return Rect_<T>(
Lerp(a.left, b.left, t),
Lerp(a.top, b.top, t),
Lerp(a.right, b.right, t),
Lerp(a.bottom, b.bottom, t)
);
}
// deprecated because of confusing name: