您将 Easing 的结果视为 new x
。这很奇怪!
另外,x
应该是linearStep
. linearStep
是一个double
,但你x
是一个int
。制作您x
的 adouble
并将其增加适当的数量。
const double step = 1.5; // Choose an appropriate value here!
for (double x = 2.0; x <= 200.0; x += step) {
double y = Ease(x, 1.0f, EasingType.Quadratic);
SetSomething(y);
}
更新
你的设计非常程序化。我更喜欢面向对象的方法。switch
-语句通常可以用多态(面向对象)方法代替。
public abstract class Curve
{
public float EaseIn(double s);
public float EaseOut(double s);
public static float EaseInOut(double s);
}
public class StepCurve : Curve
{
public override float EaseIn(double s)
{
return s < 0.5 ? 0.0f : 1.0f;
}
public override float EaseOut(double s)
{
return s < 0.5 ? 0.0f : 1.0f;
}
public override float EaseInOut(double s)
{
return s < 0.5 ? 0.0f : 1.0f;
}
}
public class LinearCurve : Curve
{
public override float EaseIn(double s)
{
return (float)s;
}
public override float EaseOut(double s)
{
return (float)s;
}
public override float EaseInOut(double s)
{
return (float)s;
}
}
public class SineCurve : Curve
{
public override float EaseIn(double s)
{
return (float)Math.Sin(s * MathHelper.HalfPi - MathHelper.HalfPi) + 1;
}
public override float EaseOut(double s)
{
return (float)Math.Sin(s * MathHelper.HalfPi);
}
public override float EaseInOut(double s)
{
return (float)(Math.Sin(s * MathHelper.Pi - MathHelper.HalfPi) + 1) / 2;
}
}
public class PowerCurve : Curve
{
int _power;
public PowerCurve(int power)
{
_power = power;
}
public override float EaseIn(double s)
{
return (float)Math.Pow(s, _power);
}
public override float EaseOut(double s)
{
var sign = _power % 2 == 0 ? -1 : 1;
return (float)(sign * (Math.Pow(s - 1, _power) + sign));
}
public override float EaseInOut(double s)
{
s *= 2;
if (s < 1) return EaseIn(s, _power) / 2;
var sign = _power % 2 == 0 ? -1 : 1;
return (float)(sign / 2.0 * (Math.Pow(s - 2, _power) + sign * 2));
}
}
使用这些定义,您可以将Ease
方法更改为
public static float Ease(double linearStep, float acceleration, Curve curve)
{
float easedStep = acceleration > 0 ? curve.EaseIn(linearStep) :
acceleration < 0 ? curve.EaseOut(linearStep) :
(float)linearStep;
return MathHelper.Lerp(linearStep, easedStep, Math.Abs(acceleration));
}
您可以使用switch
-statements 完全删除方法。你会画一条三次曲线
var curve = new PowerCurve(3);
for (double x = 2.0; x <= 200.0; x += step) {
double y = Ease(x, 1.0f, curve);
SetSomething(y);
}