我有一个在时间't'评估的路径,并根据路径类型返回方向和位置.
时间值受路径类型的影响:
switch (type)
{
case PathType.Closed:
time = ToolBox.Wrap(time, StartTime, EndTime);
break; // Wrap time around the path time to loop
case PathType.Open:
time = ToolBox.Min(time, EndTime);
break; // Clamp the time value to the max path time range
case PathType.Oscillating:
break;
}
Run Code Online (Sandbox Code Playgroud)
缺失的环节正在振荡.
我的问题是,在两个值之间振荡的有效方法是什么?
例如(2,7).如果时间达到7,则反转并向下递减至2,一旦达到2,则反转并向7增加.
算法应该知道是否根据原始值增加/减少值,所以如果值为9则知道答案是7 - (Abs(7 - 9).如果值为14,则值已经包围,因此它将导致增加1.
较高的值也会增加或减少该值,具体取决于它包围原始范围的次数.
我希望这是有道理的,因为我发现很难解释.
编辑:
不会以浮点值振荡:
for (float i = 0; i < 100; i += 0.1f)
{
Console.WriteLine("{0} {1}", i, Oscillate(2.5f, 7.5f, i));
}
private float Oscillate(float min, float max, float value)
{
float range = max - min;
float multiple = value / range;
bool ascending = multiple % 2 == 0;
float modulus = value % range;
return ascending ? modulus + min : max - modulus;
}
Run Code Online (Sandbox Code Playgroud)
这是我想出的:
public static int Oscillate(int input, int min, int max)
{
int range = max - min ;
return min + Math.Abs(((input + range) % (range * 2)) - range);
}
Run Code Online (Sandbox Code Playgroud)
我假设input将是一个从0开始的计数器.