mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-15 14:16:07 -06:00
Core: Linear interpolation (Lerp) templates and reference example for (numbers, Point Size and Rect) (#215)
This commit is contained in:
parent
b99c26b267
commit
ce5ee8f82f
3 changed files with 60 additions and 0 deletions
19
reference/LinearInterpolation/LinearInterpolation.cpp
Normal file
19
reference/LinearInterpolation/LinearInterpolation.cpp
Normal 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));
|
||||
}
|
||||
|
||||
|
||||
|
||||
11
reference/LinearInterpolation/LinearInterpolation.upp
Normal file
11
reference/LinearInterpolation/LinearInterpolation.upp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
description "Demonstrates the linear interpolation templates.\377";
|
||||
|
||||
uses
|
||||
Core;
|
||||
|
||||
file
|
||||
LinearInterpolation.cpp;
|
||||
|
||||
mainconfig
|
||||
"" = "";
|
||||
|
||||
|
|
@ -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:
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue