如何在给定的持续时间内创建 0..1..0..1..0 等的脉动值?

发布于 2024-09-05 08:33:52 字数 263 浏览 5 评论 0原文

我正在编写一些代码,其中有一个带有成员 timeTime 对象。 Time.time 给出 我的应用程序启动以来的时间(以秒为单位)(浮点值)。现在我想创建一个介于 0 和 1 之间的脉动值,然后再次从 1 到 0,这将继续进行,直到应用程序停止。

我正在考虑使用 sin() 但不知道将什么作为参数传递给它来创建这个脉冲值。

我将如何创造这种脉动的价值?

亲切的问候, 波勒克斯

I'm working on some code where I have a Time object with a member time. Time.time gives
me the time since my application started in seconds (float value). Now I want to create a pulsating value between 0 and 1 and then from 1 to 0 again, which continues doing thins untill the application stops.

I was thinking to use sin() but don't know what to pass to it as paramters to create this pulsing value.

How would I create this pulsating value?

Kind regards,
Pollux

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半步萧音过轻尘 2024-09-12 08:33:52

你提到使用 sin(),所以我猜你希望它在 0 和 1 之间连续脉冲。

这样就可以了:

float pulse(float time) {
    const float pi = 3.14;
    const float frequency = 10; // Frequency in Hz
    return 0.5*(1+sin(2 * pi * frequency * time));
}

1/频率 = 0.1 秒 是周期,即 1 之间的时间。

You mention using sin(), so I guess you want it to pulse continuously between 0 and 1.

Something like this will do:

float pulse(float time) {
    const float pi = 3.14;
    const float frequency = 10; // Frequency in Hz
    return 0.5*(1+sin(2 * pi * frequency * time));
}

1/frequency = 0.1 second is the period, which is the time between 1's.

征棹 2024-09-12 08:33:52

x = 1 - x 怎么样?
或者,如果您希望它基于时间,请使用 Timer % 2

哦,您也想要 0 到 1 之间的值。
Math.Abs​​(100 - (计时器 % 200)) / 100 怎么样
其中计时器类似于 DateTime.Now.TimeOfDay.TotalMilliseconds

编辑:
我的测试表明,这比 Sin 方法快两倍多。对于 100 万次迭代,sin 方法需要 0.048 秒,而 Abs 方法大约需要 0.023 秒。当然,您还可以从两者中得到不同的波形。 Sin 产生正弦波,而 Abs 产生三角波。

static void Main(string[] args)
{
   System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
   sw.Start();
   const int count = 1000000;
   float[] results = new float[count];
   for (int i = 0; i < count; i++)
   {
      results[i] = AbsPulse(i/1000000F);
      //results[i] = SinPulse(i / 1000000F);
   }
   sw.Stop();
   Console.WriteLine("Time Elapsed: {0} seconds", sw.Elapsed.TotalSeconds);
   char[,] graph = new char[80, 20];
   for (int y = 0; y <= graph.GetUpperBound(1); y++)
      for (int x = 0; x <= graph.GetUpperBound(0); x++)
         graph[x, y] = ' ';
   for (int x = 0; x < count; x++)
   {
      int col = x * 80 / count;
      graph[col, (int)(results[x] * graph.GetUpperBound(1))] = 'o';
   }
   for (int y = 0; y <= graph.GetUpperBound(1); y++)
   {
      for (int x = 0; x < graph.GetUpperBound(0); x++)
         Console.Write(graph[x, y]);
      Console.WriteLine();
   }
}

static float AbsPulse(float time)
{
   const int frequency = 10; // Frequency in Hz
   const int resolution = 1000; // How many steps are there between 0 and 1
   return Math.Abs(resolution - ((int)(time * frequency * 2 * resolution) % (resolution * 2))) / (float)resolution;
}

static float SinPulse(float time)
{
   const float pi = 3.14F;
   const float frequency = 10; // Frequency in Hz
   return 0.5F * (1 + (float)Math.Sin(2 * pi * frequency * time));
}

How about x = 1 - x?
Or if you want it to be time based use Timer % 2

Oh, you wanted the values between 0 and 1 as well.
how about Math.Abs(100 - (Timer % 200)) / 100
Where timer is something like DateTime.Now.TimeOfDay.TotalMilliseconds

Edit:
My tests indicate that this is more than twice as fast as the Sin method. For 1 million iterations, the sin method takes .048 seconds while the Abs method takes about .023 seconds. Also, you get different waveforms out of the two, of course. Sin produces a Sine wave, while Abs produces a triangular wave.

static void Main(string[] args)
{
   System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
   sw.Start();
   const int count = 1000000;
   float[] results = new float[count];
   for (int i = 0; i < count; i++)
   {
      results[i] = AbsPulse(i/1000000F);
      //results[i] = SinPulse(i / 1000000F);
   }
   sw.Stop();
   Console.WriteLine("Time Elapsed: {0} seconds", sw.Elapsed.TotalSeconds);
   char[,] graph = new char[80, 20];
   for (int y = 0; y <= graph.GetUpperBound(1); y++)
      for (int x = 0; x <= graph.GetUpperBound(0); x++)
         graph[x, y] = ' ';
   for (int x = 0; x < count; x++)
   {
      int col = x * 80 / count;
      graph[col, (int)(results[x] * graph.GetUpperBound(1))] = 'o';
   }
   for (int y = 0; y <= graph.GetUpperBound(1); y++)
   {
      for (int x = 0; x < graph.GetUpperBound(0); x++)
         Console.Write(graph[x, y]);
      Console.WriteLine();
   }
}

static float AbsPulse(float time)
{
   const int frequency = 10; // Frequency in Hz
   const int resolution = 1000; // How many steps are there between 0 and 1
   return Math.Abs(resolution - ((int)(time * frequency * 2 * resolution) % (resolution * 2))) / (float)resolution;
}

static float SinPulse(float time)
{
   const float pi = 3.14F;
   const float frequency = 10; // Frequency in Hz
   return 0.5F * (1 + (float)Math.Sin(2 * pi * frequency * time));
}
眼泪淡了忧伤 2024-09-12 08:33:52

我认为正弦函数是理想的,但您需要调整周期和比例。

正弦函数产生 -1 和 1 之间的结果,但您想要在 0 和 1 之间。要正确缩放它,您需要 (sin(x)+1)/2

正弦函数从零开始,在 pi/2 处变为 1,在 pi 处再次为零,在 3*pi/2 处变为 -1,并在 2*pi 处返回到零。按比例缩放,第一个零将出现在 3*pi/2 处,之后的第一个最大值将出现在 5/2*pi 处。因此,上一个公式中的 x 为 (2*time + 3) * pi/2。

把它们放在一起:(sin((2*time.time + 3) * pi/2) + 1) / 2

A sine function would be ideal I think, but you need to adjust the period and the scale.

The sine function produces results between -1 and 1, but you want to go between 0 and 1. To scale it properly you want (sin(x)+1)/2.

The sine function starts at zero, goes to 1 at pi/2, zero again at pi, -1 at 3*pi/2, and back to zero at 2*pi. Scaled, the first zero will happen at 3*pi/2 and the first maximum after that will be at 5/2*pi. So x in the previous formula is (2*time + 3) * pi/2.

Putting it all together: (sin((2*time.time + 3) * pi/2) + 1) / 2

小傻瓜 2024-09-12 08:33:52

您希望它多久脉冲一次?

假设您想在 10 秒内从 0 变为 1。

float pulseValueForTime(int sec) {
    int pulsePoint = sec % 10;
    float pulsePercent = (float)pulsePoint / (float)10;
    float pulseInTermsOfPI = (pulsePercent * 2 * PI) - PI;
    float sinVal = MagicalSinFunction(pulseInTermsOfPI); // what framework you use to compute sin is up to you... I'm sure you can google that!
    return (sinVal + 1) / 2; // sin is between 1 and -1, translate to between 0 and 1
}

How often do you want it to pulse?

Let's say you want to go from 0 to 1 over 10 seconds.

float pulseValueForTime(int sec) {
    int pulsePoint = sec % 10;
    float pulsePercent = (float)pulsePoint / (float)10;
    float pulseInTermsOfPI = (pulsePercent * 2 * PI) - PI;
    float sinVal = MagicalSinFunction(pulseInTermsOfPI); // what framework you use to compute sin is up to you... I'm sure you can google that!
    return (sinVal + 1) / 2; // sin is between 1 and -1, translate to between 0 and 1
}
如此安好 2024-09-12 08:33:52

查看 Ease 函数。他们以各种方式做这种事情——线性、多项式、指数、正弦等等。

Look into Ease functions. They do this sort of thing in all sorts of manners - linear, poly, exp, sin, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文