From ce5ee8f82f70df2d7aaf2d84298f61db28234d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20Y=C4=B1lmaz?= <32938453+ismail-yilmaz@users.noreply.github.com> Date: Mon, 9 Dec 2024 10:36:27 +0300 Subject: [PATCH] Core: Linear interpolation (Lerp) templates and reference example for (numbers, Point Size and Rect) (#215) --- .../LinearInterpolation.cpp | 19 ++++++++++++ .../LinearInterpolation.upp | 11 +++++++ uppsrc/Core/Gtypes.h | 30 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 reference/LinearInterpolation/LinearInterpolation.cpp create mode 100644 reference/LinearInterpolation/LinearInterpolation.upp diff --git a/reference/LinearInterpolation/LinearInterpolation.cpp b/reference/LinearInterpolation/LinearInterpolation.cpp new file mode 100644 index 000000000..f0c60bf4b --- /dev/null +++ b/reference/LinearInterpolation/LinearInterpolation.cpp @@ -0,0 +1,19 @@ +#include + +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)); +} + + + diff --git a/reference/LinearInterpolation/LinearInterpolation.upp b/reference/LinearInterpolation/LinearInterpolation.upp new file mode 100644 index 000000000..f41efe19c --- /dev/null +++ b/reference/LinearInterpolation/LinearInterpolation.upp @@ -0,0 +1,11 @@ +description "Demonstrates the linear interpolation templates.\377"; + +uses + Core; + +file + LinearInterpolation.cpp; + +mainconfig + "" = ""; + diff --git a/uppsrc/Core/Gtypes.h b/uppsrc/Core/Gtypes.h index d08678c3f..973c1b9a3 100644 --- a/uppsrc/Core/Gtypes.h +++ b/uppsrc/Core/Gtypes.h @@ -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 +inline T Lerp(T a, T b, double t) +{ + return a + t * (b - a); +} + +template +inline Point_ Lerp(Point_ a, Point_ b, double t) +{ + return Point_(Lerp(a.x, b.x, t), Lerp(a.y, b.y, t)); +} + +template +inline Size_ Lerp(Size_ a, Size_ b, double t) +{ + return Size_(Lerp(a.cx, b.cx, t), Lerp(a.cy, b.cy, t)); +} + +template +inline Rect_ Lerp(Rect_ a, Rect_ b, double t) +{ + return Rect_( + 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: