我正在寻找一个在柔和曲线之后插值两个值之间的值的函数。
以下是一个例子:
float xLerp(float mMin, float mMax, float mFactor) { ... }
mFactor
应介于1和0之间。
如何创建类似于我绘制的功能?
答案 0 :(得分:2)
正如我在评论中所说,指数很合适:
double mBase = 5; // higher = more "curvy"
double minY = pow(mBase, mMin - mBase);
double maxY = pow(mBase, mMax - mBase);
double scale = (mMax - mMin) / (maxY - minY);
double shift = mMin - minY;
return pow(mBase, mFactor - mBase) * scale + shift;
呃,斜坡都错了,丑陋的黑客......
答案 1 :(得分:2)
抛物线会起作用。
#define ASSERT (cond) // Some assertion macro
/**
* f(x) = a(x)^2 + 0x + c // b is zero, because no x shift.
* f(0) = mMin == c = mMin
* f(1) = mMax == a + mMin = mMax == a = mMax - mMin
*/
float xLerp (float mMin, float mMax, float mFactor) {
ASSERT(0 <= mFactor && mFactor <= 1);
float a = mMax - mMin;
return a * mFactor * mFactor + mMin;
}
答案 2 :(得分:2)
正弦波会起作用。
#include <math.h>
#define ASSERT (cond) // some assertion macro
/**
* f(x) = a * sin(x / t * PI) + b
* f(0) = mMin == b = mMin
* f(1) = mMax == a * sin(1/t * pi) + mMin == a * sin(pi/t) = mMax - mMin
* a = (mMax - mMin) / sin(pi/t)
* (Let t == 1 for "normal" periodicity. 0 < t <= 1)
* a = (mMax - mMin) / sin(pi/t) == a = mMax - mMin
*/
float xLerp (float mMin, float mMax, float mFactor){
ASSERT(0 <= mFactor && mFactor <= 1);
float a = mMax - mMin;
return a * sin(mFactor * PI) + mMin;
}