gpt4 book ai didi

c# - 在两个值之间振荡或 "ping pong"?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:10:34 25 4
gpt4 key购买 nike

我有一条路径在时间“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;
}

缺失的环节在振荡。

我的问题是什么是在两个值之间摆动的良好、有效的方法?

例如 (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;
}

最佳答案

这是我想出的:

public static int Oscillate(int input, int min, int max)
{
int range = max - min ;
return min + Math.Abs(((input + range) % (range * 2)) - range);
}

我假设 input 是一个从 0 开始的计数器。

关于c# - 在两个值之间振荡或 "ping pong"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11543685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com